I'm fairly dense when it comes to MySQL optimization mostly because I never had to do it. But now it seems to be required as I got a DB that's bigger than anything I've managed (which hasn't been randomly populated with a bunch of rand() calls). So, basically I got a table called bills which has only one primary key and index as id_bill
I run this query
Code: [Select] And it causes the entire table to be copied into tmp, causing timeouts as the table contains roughly ~40k rows and takes a lot of time to process, whereas dividing it into 2 queries
Code: [Select] It comes insanely fast. Can anyone explain why?
I run this query
SELECT <columns>
FROM bhd_bills AS b
INNER JOIN bhd_accounts AS a ON (a.id_account = b.id_account)
INNER JOIN bhd_users AS u ON (u.id_user = b.id_raised)
LEFT JOIN bhd_accounts AS ua ON (ua.id_user = u.id_user)
WHERE cancelled = 0
AND 1=1
ORDER BY b.id_bill DESC
LIMIT 0, 20 SELECT b.id_bill
FROM {prefix}bills AS b
WHERE cancelled = 0
AND 1=1
ORDER BY b.id_bill DESC
LIMIT 0, 20;
SELECT <columns>
FROM bhd_bills AS b
INNER JOIN bhd_accounts AS a ON (a.id_account = b.id_account)
INNER JOIN bhd_users AS u ON (u.id_user = b.id_raised)
LEFT JOIN bhd_accounts AS ua ON (ua.id_user = u.id_user)
WHERE id_bill IN (<result from above>)



