1

假设我有下面的查询,它运行的时间长得令人无法接受……我如何从查询中确定应该创建哪些索引来优化它?

从 SELECT 子句中的字段创建索引有帮助吗?

还是我应该只根据 WHERE 条件中的字段创建索引?

考虑到我在 CASE 子句中也有条件,为这些字段创建索引也会有帮助吗?

SELECT A.ALPHA,
       C.BETA, 
       A.KAPPA AS DELTA,
       A.ECHO,
       B.FOXTROT,
       D.GAMMA,
       CASE WHEN (D.THETA IN ('B', '3', '4', 'F', 'D', 'H') OR (D.THETA = 'E' AND D.EPSILON <> '9'))
            THEN D.MU
            WHEN D.THETA = 'E' AND D.EPSILON = '9'
            THEN D.IOTA
            ELSE D.PHI END AS PHI,
       D.CHI,
       CASE WHEN D.THETA LIKE '1%' 
            THEN '1'
            WHEN D.THETA LIKE 'P%' 
            THEN '2'
            WHEN (THETA IN ('B', '3', '4') OR (THETA = 'E' AND PSI <> 'S'))
            THEN '3' 
            WHEN (THETA in ('F', 'D', 'H') OR (THETA = 'E' AND PSI = 'S'))
            THEN '4'
            END AS OMEGA,
       CASE WHEN B.FOXTROT IN (SELECT DISTINCT FOXTROT FROM TAPPLE)
            THEN 'Y'
            ELSE 'N' END AS ZETA,
       CASE WHEN D.THETA LIKE 'E%' AND D.PSI <> 'S'
            THEN D.TAU
            WHEN D.THETA LIKE 'B%' 
            THEN D.TAU
            WHEN D.THETA LIKE '3%' 
            THEN DATE(D.SIGMA)
            WHEN D.THETA LIKE '4%' 
            THEN DATE(D.SIGMA)
            ELSE NULL END AS RHO
FROM TORANGE A,
     TLIME B,
     (SELECT FOXTROT,BETA FROM TLIME, TLEMON WHERE OMICRON='L' AND ECHO = BETA AND LAMBDA = 'M') C,
     TGRAPE D
WHERE A.ECHO = B.ECHO
  AND B.FOXTROT = C.FOXTROT
  AND B.OMICRON = 'O'
  AND B.FOXTROT = D.FOXTROT
  AND D.THETA IN ('1', 'B', '3', '4', 'E', 'F', 'D', 'H')
;
4

1 回答 1

0

如果您使用 DB2 for LUW,您可以将您的实际查询提供给DB2 Design Advisor,它可能会建议索引和其他方法来提高预期的查询性能。

于 2013-09-13T12:17:10.527 回答