Home > OS >  SQL query gets stuck if adding ORDER BY
SQL query gets stuck if adding ORDER BY

Time:04-20

I am working with a query, which looks like this

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

Which works just fine, however if I add ORDER BY s.id, then the query gets totally stuck, can someone guide me on why? the id field is primary, so it should be indexed no?

CodePudding user response:

Add an index on the column temperature.did so that the WHERE clause can be implemented efficiently.

  • Related