索引应该有什么序列?

pn9klfpd  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(438)

我有这样一个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 接线员,但我不知道如何正确地使用它们在我的情况。

axkjgtzd

axkjgtzd1#

对于您的查询,最好的索引可能是: 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)

相关问题