Search code examples
mysqlfilesort

How to avoid FileSort when using LEFT JOIN + ORDER BY?


mysql-5.6.24-win32.1432006610

I have two simple tables, TUser(id, name) and TMessage(id, uid, message). TUser holds the users, and TMessage holds messages of users.

The SQL is as below, also see: 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 some data:

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');

When I run the following LEFT JOIN + ORDER BY query, FileSort shows in the EXPLAIN result:

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

Is there something wrong?


Solution

  • The filesort is introduced by the 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)
    

    Since the order by is operating on a LEFT JOIN result, I have no idea on how to avoid this filesort.

    If changing LEFT JOIN to INNER JOIN is acceptable, that maybe a way to bypass, while missing some User info with no match with 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)