count (*) Why slow? And optimization

count (*) Why slow?

MyISAM

MyISAM engine, the number of rows in a table exists on the disk, so when the execution count (*) will return the number of direct, high efficiency;

InnoDB

The InnoDB engine trouble, when it is executed count (*), you need to read data line by line from the engine inside, then the cumulative count.

Because, innoDB to support concurrency and transaction

Optimization of small mysql

InnoDB tissue is an index table index tree leaf node is the primary key data, and the ordinary index tree leaf node is the master key. Therefore, the ordinary index tree Tree smaller than a lot of the primary key index for the count (*) such an operation, the result is the same logic which traverse the index tree. Therefore, MySQL optimizer will find the smallest tree to traverse. Under the premise to ensure correct logic, minimize the amount of scanned data, the database system is a universal law design.

optimization

You can add a count field in the table or counter designed to store a single table

Search record number of efficiency

Sorted efficiency, then, count (field) <count (primary key id) <count (1) ≈count (*), it is possible to use count (*).

Reference links

https://time.geekbang.org/column/article/72775

Guess you like

Origin www.cnblogs.com/zx125/p/12164221.html