所以我是SQL的新手,并尝试构建一个查询来加入和检索DB2 Warehouse on Cloud中不同表的数据。我想提高效率,以便更快地检索数据。这是我的SQL语句:
SELECT ML_ANOMALY_DETECTION.DATETIME,
ML_ANOMALY_DETECTION.TAG_NAME,
ML_ANOMALY_DETECTION.MLAD_VALUE AS INDEX,
PARENT.MLAD_VALUE AS SCORE,
ML_MEASURE.MLAD_VALUE AS VALUE,
DATA_CONFIG.TAG_DESCRIPTION AS TAG_DESCRIPTION,
DATA_CONFIG.UNITS AS UNITS
FROM ML_ANOMALY_DETECTION AS ML_ANOMALY_DETECTION
LEFT JOIN ML_ANOMALY_DETECTION AS PARENT
ON (ML_ANOMALY_DETECTION.DATETIME = PARENT.DATETIME AND ML_ANOMALY_DETECTION.TAG_NAME = PARENT.TAG_NAME)
INNER JOIN ML_MEASURE
ON (ML_ANOMALY_DETECTION.DATETIME = ML_MEASURE.DATETIME
AND ML_ANOMALY_DETECTION.TAG_NAME = ML_MEASURE.TAG_NAME)
INNER JOIN DATA_CONFIG
ON (ML_ANOMALY_DETECTION.TAG_NAME =DATA_CONFIG.TAG_NAME)
WHERE (ML_ANOMALY_DETECTION.DATETIME BETWEEN '2017-11-25 06:57:00'
AND '2017-11-25 07:36:00')
AND (ML_ANOMALY_DETECTION.MLAD_METRIC='ANOMALY_INDEX'
AND PARENT.MLAD_METRIC='ANOMALY_SCORE')
AND (ML_ANOMALY_DETECTION.TAG_NAME='VAR1'
OR ML_ANOMALY_DETECTION.TAG_NAME='VAR2')
希望我解释得足够好。是否有可能提高效率?
答案 0 :(得分:3)
首先,您需要用于加入的列的索引。
其次,这是你的where
条款:
WHERE (ad.DATETIME BETWEEN '2017-11-25 06:57:00' AND '2017-11-25 07:36:00') AND
ad.MLAD_METRIC = 'ANOMALY_INDEX' AND
p.MLAD_METRIC = 'ANOMALY_SCORE' AND
ad.TAG_NAME IN ('VAR1', 'VAR2')
(为了便于阅读,我添加了表别名并使用IN
而不是OR
。)
对于这些条件,我建议在ML_ANOMALY_DETECTION(mlad_metric, datetime, tag_name)
上添加一个附加索引。