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

前端 未结 4 1889
说谎
说谎 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 07:57

    For the time being I've solved the problem by using this approximation:

    EXPLAIN SELECT COUNT(id) FROM data USE INDEX (PRIMARY)
    

    The approximate number of rows can be read from the rows column of the explain plan when using InnoDB as shown above. When using MyISAM this will remain EMPTY as the table reference isbeing optimized away- so if empty fallback to traditional SELECT COUNT instead.

提交回复
热议问题