且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

mysql count(*)需要很长时间?还有更好的选择吗?

更新时间:2023-02-19 21:48:34

使用InnoDB COUNT()对于具有百万行的表来说运行缓慢.但是,如果您不使用WHERE,则可以使用hack来查看表中有多少行-使用EXPLAIN.

With InnoDB COUNT() works slowly for tables with million rows. But you can use a hack to see how many rows in table, if you aren't using WHERE - use EXPLAIN.

mysql> explain select count(1) from history;
+----+-------------+---------+-------+---------------+-----------+---------+------+----------+-------------+
| id | select_type | table   | type  | possible_keys | key       | key_len | ref  | rows     | Extra       |
+----+-------------+---------+-------+---------------+-----------+---------+------+----------+-------------+
|  1 | SIMPLE      | history | index | NULL          | history_1 | 12      | NULL | 17227419 | Using index |
+----+-------------+---------+-------+---------------+-----------+---------+------+----------+-------------+
1 row in set (0.01 sec)

在行"列中,您可以查看行数.

In column 'rows' you can see the number of rows.