Select count (*) is it too long?

mysql> select  count(*) from num;
+----------+
| count(*) |
+----------+
|  9918530 |
+----------+
1 row in set (59.18 sec)

isn"t that scary?
I switched to sqlite3 and got it in 2 seconds.

May.24,2021

Don't listen to some of the misleading above, let you count the primary key, the effect is the same. Because mysql found that the inside of your count primary key will directly replace it with count (*).

because I don't know your table structure and index status, I can only guess and give advice:

1. I guess you are not using the myisam engine, because the count () under the mysaim engine is recorded in the schema table and you can get the numbers directly. If your count () is conditional, then myisam will also slow down.

2. It seems as if you should be using the innodb engine, you use count (*) is the right choice, but it is slow. For better optimization, please refer to adding redis or memcache cache. If you do not want to introduce a new storage structure, you can record the total by creating a redundant field in mysql.

you can read the relevant chapter of "High performance mysql".


MySQL High performance third Edition
Chapter 6, page 236
there are already tens of millions of pieces of data. This should not be counted in this way.


MyISAM engine will have automatic maintenance of this, it can be faster.


< table > < thead > < tr > < th > mysql > describe num; < / th > < / tr > < / thead > < tbody > < tr > < td > Field < / td > < td > Type < / td > < td > Null < / td > < td > Key < / td > < td > Default < / td > < td > Extra < / td > < / tr > < tr > < td > id < / td > < td > int (11) < / td > < td > NO < / td > < td > PRI < / td > < td > NULL < / td > < td > auto_increment < / td > < / tr > < tr > < td > adsh < / td > < td > varchar (20) < / td > < td > NO < / td > < td > < / td > < td > NULL < / td > < td > < / td > < / tr > < tr > < td > tag < / td > < td > varchar < / td > < td > NO < / td > < td > < / td > < td > NULL < / td > < td > < / td > < / tr > < / tbody > < / table >

10 rows in set (0.49 sec)

< table > < thead > < tr > < th > mysql > select count (id) from num; < / th > < / tr > < / thead > < tbody > < tr > < td > count (id) < / td > < / tr > < tr > < td > 9918530 < / td > < / tr > < / tbody > < / table >

1 row in set (46.95 sec)

< table > < thead > < tr > < th > mysql > select count (adsh) from num; < / th > < / tr > < / thead > < tbody > < tr > < td > count (adsh) < / td > < / tr > < tr > < td > 9918530 < / td > < / tr > < / tbody > < / table >

1 row in set (30.91 sec)

< table > < thead > < tr > < th > mysql > select count (tag) from num; < / th > < / tr > < / thead > < tbody > < tr > < td > count (tag) < / td > < / tr > < tr > < td > 9918530 < / td > < / tr > < / tbody > < / table >

1 row in set (27.54 sec)

mysql > ALTER TABLE num ENGINE = 'MYISAM';
Query OK, 9918530 rows affected (3 min 5.07 sec)
Records: 9918530 Duplicates: 0 Warnings: 0

mysql> select count(*) from num;
+----------+
| count(*) |
+----------+
|  9918530 |
+----------+
1 row in set (0.02 sec)

Why is the variable length type query faster than the primary key?


mysql turns on query caching, which is definitely less than 2 seconds. If writes are rare, turn on the query cache


memory library is faster than the database to read IO. Is there a problem? It is strange that sqlite is slower than mysql

Menu