mysql count performance

后端 未结 2 1257
长情又很酷
长情又很酷 2020-12-06 08:10
select count(*) from mytable;
select count(table_id) from mytable; //table_id is the primary_key

both query were running slow on a table with 10 million rows.

2条回答
  •  广开言路
    2020-12-06 08:57

    As cherouvim pointed out in the comments, it depends on the storage engine.

    MyISAM does keep a count of the table rows, and can keep it accurate since the only locks MyISAM supports is a table lock.

    InnoDB however supports transactions, and needs to do a table scan to count the rows.

    http://www.mysqlperformanceblog.com/2006/12/01/count-for-innodb-tables/

提交回复
热议问题