mysql-count(14)

1, different engines have different implementations. myisam directly on disk can be read directly. innodb need to read line by line, and then accumulated.

2 , InnoDB because of mvcc (multi-version concurrency control), the same time data may be inconsistent different answer queries,

3, Show the Table Status can display the number of rows in the table, but not accurate.

4, so how it counts: First, using the cache, insert a row, key to plus one, minus one to delete it, but there is redis may lead to loss due to restart updates, but even redis work also appears the problem of inaccurate data : because redis with db not support distributed transactions, so write in the middle part of two operations come in a read thread, whether the first or the first read read db redis are likely to arise is not accurate.

                                 Second, another database tables is counted. Rely on the previous transaction isolation. Update and add a new record number of records in a transaction, if uncommitted transactions, which operate outside of the transaction are not visible. Note that the order code, first add a new record, and then update the number of records, number of records because the update will trigger row locks, row locks are released only when the transaction is committed, if placed end of the program, it will greatly improve concurrency.

5, comparing the efficiency of count in several ways. count (*) ~ = count ( 1)> count ( primary key)> count (one field)

Guess you like

Origin www.cnblogs.com/enchaolee/p/11671533.html