SQL 如果添加 ORDER BY,查询会卡住

SQL query gets stuck if adding ORDER BY

我正在处理一个查询,它看起来像这样

SELECT s.c1, s.t, s.u, s.dt, t.temp, t.dt AS dt2
FROM `systemusage` AS s
    INNER JOIN temperature AS t ON s.did=t.did
WHERE t.did = (SELECT id FROM devices WHERE m = 1)
LIMIT 1

效果很好,但是如果我添加 ORDER BY s.id,那么查询就会完全卡住,有人可以指导我解释为什么吗? id 字段是主要的,所以它应该被索引 no?

在列temperature.did上添加索引,以便WHERE子句可以高效地执行。

JOIN 替换 WHERE t.did = (SELECT ...) 也可能有帮助。

SELECT s.c1, s.t, s.u, t.temp 
FROM `systemusage` AS s 
INNER JOIN temperature AS t ON s.did=t.did 
INNER JOIN devices AS d ON d.id = t.did
WHERE d.m = 1
ORDER BY s.id DESC, t.id DESC 
LIMIT 1