使用LEFT JOIN + ORDER BY时如何避免FileSort?

时间:2016-03-10 03:25:14

标签: mysql filesort

的MySQL-5.6.24-win32.1432006610

我有两个简单的表,TUser(id, name)TMessage(id, uid, message)TUser保留用户,TMessage保留用户的消息。

SQL如下所示,另请参阅:http://sqlfiddle.com/#!9/7f099

CREATE TABLE TUser(
    id     INT UNSIGNED     PRIMARY KEY    NOT NULL    AUTO_INCREMENT,
    name   VARCHAR(128) NOT NULL
);


CREATE TABLE TMessage(
    id      INT UNSIGNED    PRIMARY KEY    NOT NULL    AUTO_INCREMENT,
    uid     INT UNSIGNED    NOT NULL,
    message VARCHAR(256)    NOT NULL
);


CREATE INDEX TMessageIndexUid ON TMessage(uid);

插入一些数据:

INSERT INTO TUser (name) VALUES 
     ('jack')
    ,('rose')
    ,('peter');


INSERT INTO TMessage(uid, message) VALUES
     (1, 'Hello jack')
    , (1, 'Jack, how are you')
    , (1, 'Good morning jack')
    , (2, 'I love you, rose')
    , (3, 'Peter, please call back')
    , (3, 'What are you doing, Peter');

当我运行以下LEFT JOIN + ORDER BY查询时,FileSort显示在EXPLAIN结果中:

EXPLAIN
SELECT *
  FROM        TUser
  LEFT JOIN   TMessage 
  ON          TUser.id=TMessage.uid
  WHERE       TUser.id=3
  ORDER BY    TMessage.id DESC;



id  select_type table    type  possible_keys    key              key_len ref   rows Extra
1   SIMPLE      TUser    const PRIMARY;         PRIMARY          4       const 1    Using temporary; Using filesort
1   SIMPLE      TMessage ref   TMessageIndexUid TMessageIndexUid 4       const 2 \N

有什么不对吗?

2 个答案:

答案 0 :(得分:1)

文件排序由ORDER BY引入:

mysql> EXPLAIN
    -> SELECT *
    ->   FROM        TUser
    ->   LEFT JOIN   TMessage
    ->   ON          TUser.id=TMessage.uid
    ->   WHERE       TUser.id=3
    ->   ORDER BY    TMessage.id DESC;
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+---------------------------------+
| id | select_type | table    | partitions | type  | possible_keys    | key              | key_len | ref   | rows | filtered | Extra                           |
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+---------------------------------+
|  1 | SIMPLE      | TUser    | NULL       | const | PRIMARY          | PRIMARY          | 4       | const |    1 |   100.00 | Using temporary; Using filesort |
|  1 | SIMPLE      | TMessage | NULL       | ref   | TMessageIndexUid | TMessageIndexUid | 4       | const |    4 |   100.00 | NULL                            |
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+---------------------------------+
2 rows in set, 1 warning (0.00 sec)

mysql> EXPLAIN
    -> SELECT *
    ->   FROM        TUser
    ->   LEFT JOIN   TMessage
    ->   ON          TUser.id=TMessage.uid
    ->   WHERE       TUser.id=3;
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+-------+
| id | select_type | table    | partitions | type  | possible_keys    | key              | key_len | ref   | rows | filtered | Extra |
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+-------+
|  1 | SIMPLE      | TUser    | NULL       | const | PRIMARY          | PRIMARY          | 4       | const |    1 |   100.00 | NULL  |
|  1 | SIMPLE      | TMessage | NULL       | ref   | TMessageIndexUid | TMessageIndexUid | 4       | const |    4 |   100.00 | NULL  |
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+-------+
2 rows in set, 1 warning (0.00 sec)

由于order by是在LEFT JOIN结果上运行的,所以我不知道如何避免这个文件。

如果将LEFT JOIN更改为INNER JOIN是可以接受的,那么这可能是一种绕过的方式,同时缺少一些与TMessages不匹配的用户信息。

mysql> EXPLAIN
    -> SELECT *
    ->   FROM        TUser
    ->   INNER JOIN   TMessage
    ->   ON          TUser.id=TMessage.uid
    ->   WHERE       TUser.id=3
    ->   ORDER BY    TMessage.id DESC;
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+-------------+
| id | select_type | table    | partitions | type  | possible_keys    | key              | key_len | ref   | rows | filtered | Extra       |
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+-------------+
|  1 | SIMPLE      | TUser    | NULL       | const | PRIMARY          | PRIMARY          | 4       | const |    1 |   100.00 | NULL        |
|  1 | SIMPLE      | TMessage | NULL       | ref   | TMessageIndexUid | TMessageIndexUid | 4       | const |    2 |   100.00 | Using where |
+----+-------------+----------+------------+-------+------------------+------------------+---------+-------+------+----------+-------------+
2 rows in set, 1 warning (0.00 sec)

答案 1 :(得分:0)

我也不知道'filesort'。然后我发现了类似的东西,

"Anytime a sort can’t be performed from an index, it’s a filesort."

以下是要参考的链接: