In MySQL 5, SELECT COUNT(1) FROM table_name is very slow -


i have mysql 5.0 database few tables containing on 50m rows. how know this? running "select count(1) foo", of course. query on 1 table containing 58.8m rows took 10 minutes complete!

mysql> select count(1) large_table; +----------+ | count(1) | +----------+ | 58778494 |  +----------+ 1 row in set (10 min 23.88 sec)  mysql> explain select count(1) large_table; +----+-------------+-------------------+-------+---------------+----------------------------------------+---------+------+-----------+-------------+ | id | select_type | table             | type  | possible_keys | key                                    | key_len | ref  | rows      |       | +----+-------------+-------------------+-------+---------------+----------------------------------------+---------+------+-----------+-------------+ |  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 |          | +-------------------+---------------------+------+-----+---------+----------------+ | 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) 

all of tables in question innodb.

any ideas why slow, , how can speed up?

if need have result instantly , don't care if it's 58.8m or 51.7m, can find out approximate number of rows calling

show table status 'large_table'; 

see column rows
more information result take @ manual @ http://dev.mysql.com/doc/refman/5.1/en/show-table-status.html


Comments

Popular posts from this blog

linux - Mailx and Gmail nss config dir -

c# - Is it possible to remove an existing registration from Autofac container builder? -

php - Mysql PK and FK char(36) vs int(10) -