2

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

Paul Stanley
  • 4,018
  • 6
  • 35
  • 56
mesibo
  • 3,970
  • 6
  • 25
  • 43
  • There is probably nothing to fix. How many rows do you expect to be in the result set? Don't worry about sorting 100 rows. – Paul Spiegel May 29 '16 at 16:51

1 Answers1

0

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

Community
  • 1
  • 1
Paul Stanley
  • 4,018
  • 6
  • 35
  • 56
  • Your query should produce the same execution plan. You need to use STRAIGHT_JOIN or FORCE INDEX(updated) if you want to force MySQL first to read from vendors table. – Paul Spiegel May 29 '16 at 17:11