我有这样的sql语句,该语句聚集了 MySQL 数据库的3个表中的数据。该查询需要很长时间才能完成。我正在尝试使用索引来加快该过程。
SELECT
A.ID_SITE AS OBJECT_ID,
B.SITE_NAME AS OBJECT_NAME,
A.POLYGON,
C.TIME_KEY AS DATE_TIME_KEY,
B.ADDRESS,
B.REGION,
B.DISTRICT,
B.LOCATION,
B.LOCATION_TYPE
FROM TABLE_C AS C
INNER JOIN TABLE_A AS A
ON C.ID_OBJECT = A.ID_SITE
INNER JOIN TABLE_B B
ON A.ID_SITE = B.SITE_ID AND TRACK_IND != 1
WHERE
(C.TIME_KEY BETWEEN '2018-10-01 00:00:00' AND '2018-10-31 23:59:59')
AND
C.ID_TIME_MODE = 3
AND
C.ID_SUBOBJECT_TYPE = 1
AND (
C.CONG_POWER >= 1 OR
C.DIGITAL_POWER >= 3
)
AND
C.ID_OBJECT NOT IN (20158, 26875)
AND
A.MONTH_KEY = '2018-10-01'
我需要一些建议。在我的情况下,按什么顺序创建和使用索引的最佳方法是什么?
我现在所做的:
CREATE INDEX index_a ON TABLE_A (ID_SITE);
CREATE INDEX index_b ON TABLE_B (SITE_ID, TRACK_IND);
CREATE INDEX index_c ON TABLE_C (TIME_KEY, ID_TIME_MODE, ID_SUBOBJECT_TYPE, CONG_POWER, DIGITAL_POWER, ID_OBJECT)
CREATE INDEX index_a_month_key ON TABLE_A (MONTH_KEY)
;
另外,我认为最好使用FORCE INDEX
运算符,但对于我如何正确使用它们感到困惑。
答案 0 :(得分:1)
对于您的查询,最佳索引可能是:
TABLE_C(ID_TIME_MODE, ID_SUBOBJECT_TYPE, TIME_KEY, CONG_POWER, DIGITAL_POWER, ID_OBJECT)
TABLE_A(ID_SITE, MONTH_KEY)
TABLE_B(SITE_ID)