我有一個包含超過50M行的幾個表的MySQL 5.0數據庫。但我怎麼知道這一點?當然,通過運行「SELECT COUNT(1)FROM foo」。包含58.8M行的一個表上的這個查詢需要10分鐘才能完成!在MySQL 5中,SELECT COUNT(1)FROM table_name非常慢
mysql> SELECT COUNT(1) FROM large_table;
+----------+
| count(1) |
+----------+
| 58778494 |
+----------+
1 row in set (10 min 23.88 sec)
mysql> EXPLAIN SELECT COUNT(1) FROM large_table;
+----+-------------+-------------------+-------+---------------+----------------------------------------+---------+------+-----------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------------------+-------+---------------+----------------------------------------+---------+------+-----------+-------------+
| 1 | SIMPLE | large_table | index | NULL | fk_large_table_other_table_id | 5 | NULL | 167567567 | Using index |
+----+-------------+-------------------+-------+---------------+----------------------------------------+---------+------+-----------+-------------+
1 row in set (0.00 sec)
mysql> DESC large_table;
+-------------------+---------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+---------------------+------+-----+---------+----------------+
| id | bigint(20) unsigned | NO | PRI | NULL | auto_increment |
| created_on | datetime | YES | | NULL | |
| updated_on | datetime | YES | | NULL | |
| other_table_id | int(11) | YES | MUL | NULL | |
| parent_id | bigint(20) unsigned | YES | MUL | NULL | |
| name | varchar(255) | YES | | NULL | |
| property_type | varchar(64) | YES | | NULL | |
+-------------------+---------------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
所有問題的表都是InnoDB。
任何想法,爲什麼這是如此緩慢,我如何加快速度?
的手冊。也許您應該發佈「SHOW CREATE TABLE」的輸出並給我們一些關於您的問題域的背景信息? – MarkR
馬克,在這種情況下,問題域並不重要,因爲我所要做的就是對行進行計數。 :) –