optimize mysql count query

后端 未结 10 679
忘了有多久
忘了有多久 2020-12-30 05:54

Is there a way to optimize this further or should I just be satisfied that it takes 9 seconds to count 11M rows ?

devuser@xcmst > mysql --user=user --pass         


        
10条回答
  •  鱼传尺愫
    2020-12-30 06:06

    If the historical data is not volatile, create a summary table. There are various approaches, the one to choose will depend on how your table is updated, and how often.

    For example, assuming old data is rarely/never changed, but recent data is, create a monthly summary table, populated for the previous month at the end of each month (eg insert January's count at the end of February). Once you have your summary table, you can add up the full months and the part months at the beginning and end of the range:

    select count(*) 
    from record_updates 
    where date_updated >= '2009-10-11 15:33:22' and date_updated < '2009-11-01';
    
    select count(*) 
    from record_updates 
    where date_updated >= '2010-12-00';
    
    select sum(row_count) 
    from record_updates_summary 
    where date_updated >= '2009-11-01' and date_updated < '2010-12-00';
    

    I've left it split out above for clarity but you can do this in one query:

    select ( select count(*)
             from record_updates 
             where date_updated >= '2010-12-00'
                   or ( date_updated>='2009-10-11 15:33:22' 
                        and date_updated < '2009-11-01' ) ) +
           ( select count(*) 
             from record_updates 
             where date_updated >= '2010-12-00' );
    

    You can adapt this approach for make the summary table based on whole weeks or whole days.

提交回复
热议问题