简体   繁体   English

Mysql-使用临时 使用文件排序

[英]Mysql - Using temporary; Using filesort

I have two tables like this 我有两个这样的桌子

CREATE TABLE `vendors` (
  vid int(10) unsigned NOT NULL AUTO_INCREMENT,
  updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (vid),
  key(updated)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `products` (
  vid int(10) unsigned NOT NULL default 0,
  pid int unsigned default 0,
  flag int(11) unsigned DEFAULT '0',
  PRIMARY KEY (vid),
  KEY (pid)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

This is a simple query 这是一个简单的查询

> explain select vendors.vid, pid from products, vendors where pid=1 and vendors.vid=products.vid order by updated;
+------+-------------+----------+--------+---------------+---------+---------+---------------------+------+----------------------------------------------+
| id   | select_type | table    | type   | possible_keys | key     | key_len | ref                 | rows | Extra                                        |
+------+-------------+----------+--------+---------------+---------+---------+---------------------+------+----------------------------------------------+
|    1 | SIMPLE      | products | ref    | PRIMARY,pid   | pid     | 5       | const               |    1 | Using index; Using temporary; Using filesort |
|    1 | SIMPLE      | vendors  | eq_ref | PRIMARY       | PRIMARY | 4       | social.products.vid |    1 |                                              |
+------+-------------+----------+--------+---------------+---------+---------+---------------------+------+----------------------------------------------+

I am wondering why mysql need to use temporary table and filesort for such a simple query. 我想知道为什么mysql需要使用临时表和文件排序进行这样的简单查询。 As you can see that ORDER BY field has index. 如您所见,ORDER BY字段具有索引。

mysql fiddle here : http://sqlfiddle.com/#!9/3d9be/30 mysql小提琴在这里: http ://sqlfiddle.com/#!9/ 3d9be/30

That will be the optimum query in that case, doesn't always have to go to the index for the fastest result. 在这种情况下,这将是最佳查询,而不必总是去索引以获得最快的结果。 The optimiser may choose to use the index when the record count goes up. 当记录计数增加时,优化器可以选择使用索引。 You can try inserting 10,000 dummy records and seeing if this is the case. 您可以尝试插入10,000个虚拟记录,看看是否是这种情况。

If I flip the conditions here, you will find it will use the index, since I have supplied the table where the where condition is joined on later in the query. 如果在这里翻转条件,您会发现它将使用索引,因为稍后在查询中提供了连接条件所在的表。 We need to look at records in table products after the join is made, so in essence I've made it harder work, so the index is used. 进行连接后,我们需要查看表产品中的记录,因此从本质上讲,我已经使工作更加艰苦,因此使用了索引。 It'll still run in the same time. 它仍然会同时运行。 You can try pitting the 2 queries against each other to see what happens. 您可以尝试使两个查询相互竞争,以查看会发生什么。 Here it is: 这里是:

EXPLAIN
SELECT vendors.vid, products.pid 
FROM vendors 
INNER JOIN products ON vendors.vid = products.vid
WHERE pid = 1
ORDER BY vendors.updated DESC

You can find a detailed explanation here: Fix Using where; 你可以在这里找到一个详细的解释: 固定使用其中; Using temporary; 使用临时; Using filesort 使用文件排序

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

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