简体   繁体   中英

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. As you can see that ORDER BY field has index.

mysql fiddle here : 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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