Speeding up GROUP BY, SUM and AVG queries

前端 未结 1 407
走了就别回头了
走了就别回头了 2021-02-11 03:37

The table arg_rec contains 800K rows on my test machine, normally this table will hold over 15M rows. I want to run the following query :

SELECT STE_ID, PNT_NO,          


        
1条回答
  •  误落风尘
    2021-02-11 03:55

    For best performance in GROUP BY queries you must add covering index as:

    ALTER TABLE arg_rec ADD KEY ix1(STE_ID, PNT_NO, YR, MN,AVR_WS, AVR_PW );
    

    For covering index you add:

    1. columns used in where clauses first, then
    2. columns used in group by, then
    3. columns used in order by, and then
    4. columns used in select.

    Visit for details: Group By Optmization in MySQL

    1. You can cache queries in MySQL by enabling query cache loot at Query Cache Configuration

    2. You can store YR, MN, DY, HR, MI, SC in a single column having data type as TIMESTAMP which will increase performance of indexing and group by operation.

    0 讨论(0)
提交回复
热议问题