Forum Moderators: open
SELECT l.f_primaryKey, l.f_tinyint, UNIX_TIMESTAMP(l.f_datetime) dt
FROM table l
WHERE
(l.f_primaryKey NOT LIKE "%STR")
AND (
(f_tinyint = 1)
OR (f_tinyint = 2)
OR (f_tinyint = 3 AND f_integer < UNIX_TIMESTAMP() - 3600)
OR (f_tinyint = 4 AND (f_integer < UNIX_TIMESTAMP() - 3600 * 24 * 7))
)
ORDER BY (f_tinyint = 1) DESC, (f_integer!= 3) DESC, f_integer ASC
LIMIT 10000
This query takes around 5 - 10 minutes to complete, which is a bit too long for my taste. From looking at the MySQL process list, it seems like the server takes the most time sorting the result.
Does anybody know how I could optimize this further? Was making a varchar the primary key a mistake?