Mysql count performance on very big tables

前端 未结 6 683
说谎
说谎 2020-11-30 00:29

I have a table with more than 100 millions rows in Innodb.

I have to know if there is more than 5000 rows where the foreign key = 1. I don\'t need the exact number.<

6条回答
  •  情歌与酒
    2020-11-30 01:23

    Counter tables or other caching mechanism is the solution:

    InnoDB does not keep an internal count of rows in a table because concurrent transactions might “see” different numbers of rows at the same time. To process a SELECT COUNT(*) FROM t statement, InnoDB scans an index of the table, which takes some time if the index is not entirely in the buffer pool. If your table does not change often, using the MySQL query cache is a good solution. To get a fast count, you have to use a counter table you create yourself and let your application update it according to the inserts and deletes it does. If an approximate row count is sufficient, SHOW TABLE STATUS can be used. See Section 14.3.14.1, “InnoDB Performance Tuning Tips”.

    • http://dev.mysql.com/doc/refman/5.5/en/innodb-restrictions.html

提交回复
热议问题