Mysql improve SELECT speed

后端 未结 8 921
借酒劲吻你
借酒劲吻你 2021-01-02 05:24

I\'m currently trying to improve the speed of SELECTS for a MySQL table and would appreciate any suggestions on ways to improve it.

We have over 300 million records

8条回答
  •  死守一世寂寞
    2021-01-02 05:44

    I would do two things - first throw some indexes on there around tag and date as suggested above:

    alter table table add index (tag, date);
    

    Next break your query into a main query and sub-select in which you are narrowing your results down when you get into your main query:

    SELECT date, value
    FROM table
    WHERE date BETWEEN 'x' and 'y'
    AND tag IN ( SELECT tag FROM table WHERE tag = 'a' )
    ORDER BY date
    

提交回复
热议问题