Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

analyze query mysql

Tags:

mysql

I have a question about, how to analyze a query to know performance of its (good or bad). I searched a lot and got something like below:

SELECT count(*) FROM users; => Many experts said it's bad.

SELECT count(id) FROM users; => Many experts said it's good.

Please see the table:

+---------------+-------------+------+-----+---------+----------------+
| Field         | Type        | Null | Key | Default | Extra          |
+---------------+-------------+------+-----+---------+----------------+
| userId        | int(11)     | NO   | PRI | NULL    | auto_increment |
| f_name        | varchar(50) | YES  |     | NULL    |                |
| l_name        | varchar(50) | YES  |     | NULL    |                |
| user_name     | varchar(50) | NO   |     | NULL    |                |
| password      | varchar(50) | YES  |     | NULL    |                |
| email         | varchar(50) | YES  |     | NULL    |                |
| active        | char(1)     | NO   |     | Y       |                |
| groupId       | smallint(4) | YES  | MUL | NULL    |                |
| created_date  | datetime    | YES  |     | NULL    |                |
| modified_date | datetime    | YES  |     | NULL    |                |
+---------------+-------------+------+-----+---------+----------------+

But when I try to using EXPLAIN command for that, I got the results:

EXPLAIN SELECT count(*) FROM `user`;

+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | user  | index | NULL          | groupId | 3       | NULL |   83 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

EXPLAIN SELECT count(userId) FROM user;

+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
| id | select_type | table | type  | possible_keys | key     | key_len | ref  | rows | Extra       |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
|  1 | SIMPLE      | user  | index | NULL          | groupId | 3       | NULL |   83 | Using index |
+----+-------------+-------+-------+---------------+---------+---------+------+------+-------------+
1 row in set (0.00 sec)

So, the first thing for me: Can I understand it's the same performance?


P/S: MySQL version is 5.5.8.

like image 636
vietean Avatar asked Dec 12 '22 11:12

vietean


2 Answers

No, you cannot. Explain doesn't reflect all the work done by mysql, it just gives you a plan of how it will be performed.

What about specifically count(*) vs count(id). The first one is always not slower than the second, and in some cases it is faster.

count(col) semantic is amount of not null values, while count(*) is - amount of rows.

Probably mysql can optimize count(col) by rewriting into count(*) as well as id is a PK thus cannot be NULL (if not - it looks up for NULLS, which is not fast), but I still propose you to use COUNT(*) in such cases.

Also - the internall processes depend on used storage engine. For myisam the precalculated number of rows returned in both cases (as long as you don't use WHERE).

like image 161
zerkms Avatar answered Jan 01 '23 17:01

zerkms


In the example you give the performance is identical.

The execution plan shows you that the optimiser is clever enough to know that it should use the Primary key to find the total number of records when you use count(*).

like image 34
Kevin Burton Avatar answered Jan 01 '23 17:01

Kevin Burton