简体   繁体   English

分析查询 mysql

[英]analyze query 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命令时,我得到了结果:

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 ;解释 SELECT 计数( userId )来自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. P/S:MySQL 版本为 5.5.8。

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.解释并没有反映 mysql 所做的所有工作,它只是为您提供了如何执行的计划。

What about specifically count(*) vs count(id) .具体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. count(col)语义是非amount of not null values ,而count(*)是 - 行数。

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.可能 mysql 可以通过重写为count(*)来优化count(col)以及id是一个 PK 因此不能是NULL (如果不是 - 它查找NULLS ,这并不快),但我仍然建议你使用COUNT(*)在这种情况下。

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 ).对于myisam ,在两种情况下都返回预先计算的行数(只要您不使用WHERE )。

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(*).执行计划向您展示了优化器足够聪明,知道在您使用 count(*) 时它应该使用主键来查找记录总数。

There is not significant difference when it comes on counting.计数时没有显着差异。 The reason is that most optimizers will figure out the best way to count rows by themselves.原因是大多数优化器会自己找出计算行数的最佳方法。

The performance difference comes to searching for values and lack of indexing.性能差异在于搜索值和缺乏索引。 So if you search for a field that has no index assigned {f_name,l_name} and a field that has{userID(mysql automatically use index on primary keys),groupID(seems like foraign key)} then you will see the difference in performance.因此,如果您搜索一个没有分配索引的字段 {f_name,l_name} 和一个具有 {userID(mysql 自动在主键上使用索引),groupID(似乎是外键)} 的字段,那么您将看到性能上的差异.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM