This query hangs the web server

It looks innocent enough, but apparently I’m structuring something incorrectly.

SELECT u.firstname, u.lastname, n.nID, n.date, n.priority, n.type, n.sender, n.recipient, n.subject, n.message FROM users u, notifications n WHERE n.sender = u.email order by priority limit 5

Thanks!

There’s not much to go on here, but you should at least drop that carthesian product in favor of a JOIN:

SELECT
   u.firstname
 , u.lastname
 , n.nID
 , n.date
 , n.priority
 , n.type
 , n.sender
 , n.recipient
 , n.subject
 , n.message
 FROM users u
INNER
 JOIN notifications n
   ON n.sender = u.email
ORDER
   BY priority
LIMIT 5

Please post the SHOW CREATE TABLE for both the users and notificiation tables as well as the result of the EXPLAIN of the query above.

you’re asking the database engine to retrieve all notifications forall users, and then sort all the rows thus obtained by priority

is there an index on the priority column? on the sender column?