Count (*) Why is an "empty table" very slow? Why does it take 300 ms to select xxx from table limit 1 with dozens of rows of data? Will select min (pk) fromtable be slow?
See the following example:
Dba @ localhost: test 18:14:32> show create table test_hmw \ G
* *************************** 1. row ***************************
Table: test_hmw
Create Table: create table 'test _ hmw '(
'Mail' varchar (1024) default null,
'User _ id' bigint (20) default null,
'Id' int (10) unsigned NOTNULL AUTO_INCREMENT,
Primary key ('id ')
) ENGINE = InnoDB AUTO_INCREMENT = 3997636 default charset = utf8
1 row in set (0.00 sec)
Dba @ localhost: test 18:14:45> select count (*) from test_hmw;
+ ---------- +
| Count (*) |
+ ---------- +
| 1, 3813472 |
+ ---------- +
1 row in set (0.69 sec)
Dba @ localhost: test 18:14:56> show status like '% last_query % ';
+ ----------------- + --------------- +
| Variable_name | Value |
+ ----------------- + --------------- +
| Last_query_cost |776677.599000|
+ ----------------- + --------------- +
1 row in set (0.00 sec)
Dba @ localhost: test 18:15:14> delete from test_hmw;
Query OK, 3813472 rows affected (15.11 sec)
Dba @ localhost: test 18:15:56> select count (*) fromtest_hmw;
+ ---------- +
| Count (*) |
+ ---------- +
| 0 |
+ ---------- +
1 row in set(0.31 sec)
Dba @ localhost: test 18:15:59> show status like '% last_query % ';
+ ----------------- + --------------- +
| Variable_name | Value |
+ ----------------- + --------------- +
| Last_query_cost |757879.799000|
+ ----------------- + --------------- +
1 row in set (0.00 sec)
We found that the cost of an empty table in count is the same as that before deletion because it is cached.) In fact, this phenomenon exists in Oracle, operations such as select xx from table where rownum <xxx or select count (*) are performed by scanning from the starting block to HMW, but not by using HMW in MySQL, mySQL multi-version is directly stored in the Table. Therefore, MySQL deletes a large amount of data, and a large number of "blank" pages will be scanned before the purge thread recycles the data, therefore, this phenomenon exists;
Now that you know the cause, you can repeat cost in MySQL by reorganizing the table:
Dba @ localhost: test 18:16:01> optimize table test_hmw;
2 rows in set (1.85 sec)
Dba @ localhost: test 18:16:14> select count (*) fromtest_hmw;
+ ---------- +
| Count (*) |
+ ---------- +
| 0 |
+ ---------- +
1 row in set(0.00 sec)
Dba @ localhost: test 18:16:16> show status like '% last_query % ';
+ ----------------- + ---------- +
| Variable_name | Value |
+ ----------------- + ---------- +
| Last_query_cost |1.199000|
+ ----------------- + ---------- +
1 row in set (0.00 sec)
This article is from "MIKE's old blog" blog, please be sure to keep this source http://boylook.blog.51cto.com/7934327/1299866