mysql> select count(*)
-> from ip_address_varchar20;
+----------+
| count(*) |
+----------+
| 2764687 |
+----------+
1 row in set (1 min 28.80 sec)
我认为应该有一个字段来存储任何表的大小,但我发现 count(*) 太慢了。
为什么 DBMS 不对此进行优化?或者我在这方面做了一些不好的做法?
最佳答案
One operation that PostgreSQL is known to be slow performing is doing a full count of rows in a table, typically using this SQL:
SELECT COUNT(*) FROM table
The reason why this is slow is related to the MVCC implementation in PostgreSQL. The fact that multiple transactions can see different states of the data means that there can be no straightforward way for "COUNT(*)" to summarize data across the whole table; PostgreSQL must walk through all rows, in some sense. This normally results in a sequential scan reading information about every row in the table.
上面对 Postgres 的解释也适用于 MySQL 的 InnoDB。由于InnoDB使用MVCC。
有一些approaches使用 InnoDB 进行快速计数。例如,您可以使用估计,也可以使用触发器来维护表的行数。
关于mysql - 为什么 select count(*) from table_name 这么慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55507132/