optimize mysql count query

后端 未结 10 680
忘了有多久
忘了有多久 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:12

    It depends on a few things but something like this may work for you

    im assuming this count never changes as it is in the past so the result can be cached somehow

    count1 = "select count(*) from record_updates where date_updated <= '2009-10-11 15:33:22'"
    

    gives you the total count of records in the table, this is an approximate value in innodb table so BEWARE, depends on engine

    count2 = "select table_rows from information_schema.`TABLES` where table_schema = 'marctoxctransformation' and TABLE_NAME = 'record_updates'"
    

    your answer

    result = count2 - count1

提交回复
热议问题