Why is the mysql count (*) empty table slow?

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.