How to optimize COUNT(*) performance on InnoDB by using index

前端 未结 4 1896
说谎
说谎 2020-11-28 07:32

I have a largish but narrow InnoDB table with ~9m records. Doing count(*) or count(id) on the table is extremely slow (6+ seconds):



        
4条回答
  •  天涯浪人
    2020-11-28 08:09

    select max(id) - min(id) from xxx_table where ....

    This will use "Select tables optimized away", and is very fast!!!

    Note max(id) - min(id) is actually bigger than count(1).

提交回复
热议问题