MySQL INNER与LEFT JOIN不同的顺序

时间:2014-07-09 15:57:55

标签: php mysql

为什么这两个查询在具有相同的ORDER BY时会返回不同的结果集。

只有查询的区别在于我第一次使用INNER JOIN时需要大约5秒钟。 我第二次使用LEFT JOIN,耗时0.05秒。在这两种情况下,它们都返回了43.000行,但是tck.id顺序是不同的,我无法弄清楚为什么或以哪种方式?

SELECT tck.*, acc.ac_name
FROM support_tickets tck  
INNER JOIN support_ticket_accounts acc USING (id_support_ticket_account)
WHERE tck.id_company = 2 AND tck.st_status = 1 ORDER BY tck.st_priority DESC

编辑:

SELECT tck.*, acc.ac_name
FROM support_tickets tck
    LEFT JOIN support_ticket_accounts acc ON tck.id_support_ticket_account = acc.id_support_ticket_account
WHERE tck.id_company = 2 AND tck.st_status = 1
ORDER BY tck.st_priority DESC;
+----+-------------+-------+--------+---------------+------------+---------+-------------------------------+-------+-----------------------------+
| id | select_type | table | type   | possible_keys | key        | key_len | ref                           | rows  | Extra                       |
+----+-------------+-------+--------+---------------+------------+---------+-------------------------------+-------+-----------------------------+
|  1 | SIMPLE      | tck   | ref    | id_company    | id_company | 5       | const                         | 37586 | Using where; Using filesort |
|  1 | SIMPLE      | acc   | eq_ref | PRIMARY       | PRIMARY    | 4       | tck.id_support_ticket_account |     1 |                             |
+----+-------------+-------+--------+---------------+------------+---------+-------------------------------+-------+-----------------------------+

SELECT tck.*, acc.ac_name
FROM support_tickets tck
    INNER JOIN support_ticket_accounts acc ON tck.id_support_ticket_account = acc.id_support_ticket_account
WHERE tck.id_company = 2 AND tck.st_status = 1
ORDER BY tck.st_priority DESC;
+----+-------------+-------+------+-------------------------------------+----------------------------+---------+-------------------------------+------+---------------------------------+
| id | select_type | table | type | possible_keys                        | key                       | key_len | ref                           | rows | Extra                           |
+----+-------------+-------+------+--------------------------------------+---------------------------+---------+-------------------------------+------+---------------------------------+
|  1 | SIMPLE      | acc   | ALL  | PRIMARY                              | NULL                      | NULL    | NULL                          |    5 | Using temporary; Using filesort |
|  1 | SIMPLE      | tck   | ref  | id_company,id_support_ticket_account | id_support_ticket_account | 5       | acc.id_support_ticket_account | 2085 | Using where                     |
+----+-------------+-------+------+--------------------------------------+---------------------------+---------+-------------------------------+------+---------------------------------+

2 个答案:

答案 0 :(得分:1)

如果你只是ORDER BY tck.st_priority DESC多个不同的记录集是可能的并且可以返回,对于两种情况(左或内)中的每一种。那是因为你必须有很多具有相同st_priority的记录,所以它们中的任何一个都没有特定的顺序 在order by子句中添加更多字段,以便为任何记录提供唯一可能的位置,并且您将在两个查询中具有相同的顺序。

答案 1 :(得分:1)

我认为using temporary负责延迟(但我不明白为什么一个查询而不是另一个查询是必要的)。我认为创建multi-column index应该会有所帮助:

CREATE INDEX filter 
    ON support_tickets(id_company, st_status, st_priority)
    USING BTREE;