比较MySQL中两个查询的性能

时间:2014-03-04 10:04:37

标签: mysql database-performance

我正在尝试优化我创建的mysql表上的查询。我希望表中会有很多行。查看this question接受的答案和最高投票答案表明了两种不同的方法。 我写了这两个查询,想知道哪一个更有效。

SELECT uv.* 
FROM UserVisit uv INNER JOIN 
    (SELECT ID,MAX(visitDate) visitDate 
     FROM UserVisit GROUP BY ID) last 
    ON (uv.ID = last.ID AND uv.visitDate = last.visitDate);

使用EXPLAIN运行此产品:

+----+-------------+------------+--------+---------------+---------+---------+--------------------------------+------+-------------+
| id | select_type | table      | type   | possible_keys | key     | key_len | ref                            | rows | Extra       |
+----+-------------+------------+--------+---------------+---------+---------+--------------------------------+------+-------------+
|  1 | PRIMARY     | <derived2> | ALL    | NULL          | NULL    | NULL    | NULL                           |    2 |             |
|  1 | PRIMARY     | uv         | eq_ref | PRIMARY       | PRIMARY | 11      | last.playscanID,last.visitDate |    1 |             |
|  2 | DERIVED     | UserVisit  | index  | NULL          | PRIMARY | 11      | NULL                           |    4 | Using index |
+----+-------------+------------+--------+---------------+---------+---------+--------------------------------+------+-------------+
3 rows in set (0.01 sec)

另一个问题是:

SELECT lastVisits.* 
FROM ( SELECT * FROM UserVisit ORDER BY visitDate DESC ) lastVisits 
GROUP BY lastVisits.ID

使用EXPLAIN运行该产品:

+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
| id | select_type | table      | type | possible_keys | key  | key_len | ref  | rows | Extra                           |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
|  1 | PRIMARY     | <derived2> | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using temporary; Using filesort |
|  2 | DERIVED     | UserVisit  | ALL  | NULL          | NULL | NULL    | NULL |    4 | Using filesort                  |
+----+-------------+------------+------+---------------+------+---------+------+------+---------------------------------+
2 rows in set (0.00 sec)

我不确定如何解释这两个EXPLAIN的结果。
我希望哪些查询更快,为什么?

修改 这是UserVisit表的显示方式:

+----------------+---------------------+------+-----+---------+-------+
| Field          | Type                | Null | Key | Default | Extra |
+----------------+---------------------+------+-----+---------+-------+
| ID             | bigint(20) unsigned | NO   | PRI | NULL    |       |
| visitDate      | date                | NO   | PRI | NULL    |       |
| visitTime      | time                | NO   |     | NULL    |       |
| analysisResult | decimal(3,2)        | NO   |     | NULL    |       |
+----------------+---------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

1 个答案:

答案 0 :(得分:2)

首先,您可能需要阅读EXPLAIN上的manual。这是一个密集的阅读。但它应该提供你想要的大部分信息。

其次,正如Strawberry所说,第二个查询是偶然的。在将来的版本中,行为可能会更改,并且您的查询不会返回错误,只会返回不同的数据。这几乎总是一件坏事......

最后,EXPLAIN表明版本1会更快。在EXTRA中,它说它使用索引,这比filesort快得多。如果没有架构,很难确定,但我认为您还将从ID和visitdate上的复合键中受益。