我正在尝试使查询运行更快。查询量很大,但我认为主要重点应放在复杂的select CASE查询部分。
(CASE
WHEN (SELECT
g2.montant_ht_actualise_echeance
FROM
base_gid g2
WHERE
g.num_contrat = g2.num_contrat
AND g.code_nidt = g2.code_nidt
AND g.libelle_rubrique_echeance = g2.libelle_rubrique_echeance
AND g.nom_tiers = g2.nom_tiers
AND (
g.year_echeance - g2.year_echeance
) = 1 LIMIT 1) IS NULL THEN g.montant_ht_actualise_echeance
WHEN g.montant_ht_actualise_echeance > ((SELECT
g4.montant_ht_actualise_echeance
FROM
base_gid g4
WHERE
g.num_contrat = g4.num_contrat
AND g.code_nidt = g4.code_nidt
AND g.libelle_rubrique_echeance = g4.libelle_rubrique_echeance
AND g.nom_tiers = g4.nom_tiers
AND (
g.year_echeance - g4.year_echeance
) = 1 LIMIT 1) * 1.1) THEN g.montant_ht_actualise_echeance
WHEN g.code_indice LIKE 'ICC%'
OR g.code_indice = '' THEN CASE
WHEN g.periode_courante_indice_echeance IN (SELECT
indice_icc.icc_periode
FROM
indice_icc) THEN ROUND(g.montant_ht_actualise_echeance,
2)
ELSE ROUND((g.montant_ht_actualise_echeance * :filtre_indice_icc),
2)
END
ELSE g.montant_ht_actualise_echeance
END) montant_ht_actualise_echeance
case语句的逻辑如下:
我正在尝试编辑“ montant_actualise_echeance”列
情况1:去年没有帐单->'montant_actualise_echeance'不变
情况2:上一年的帐单* 1.1等于或小于montant_actualise_echeance->更改为'montant_actualise_echeance'
情况3:如果icc列类似于ICC ...或为空:
如果icc表具有当前行的时间戳->更改为“ montant_actualise_echeance”
其他-> montant_ht_actualise_echeance * icc(用户输入)
ELSE->'montant_actualise_echeance'不变
我正在使用MariaDB 10.1
答案 0 :(得分:0)
首先,请确保您的子查询可以使用“会聚索引”。我要确保存在的索引应该类似于:
create index ix1 on base_gid (
num_contrat, code_nidt, libelle_rubrique_echeance,
nom_tiers, year_echeance, montant_ht_actualise_echeance
);
另一个有用的索引(如果您还没有的话)是:
create index ix2 on indice_icc (icc_periode);