I am trying to optimize this query that sorts posts by reputation field (1st) and then id field (2nd). Without 1st field query takes ~0.25
select *
from (
SELECT -- everything is ok here
, CASE
WHEN p.created_at >= unix_timestamp(now() - INTERVAL p.reputation DAY)
THEN + p.reputation ELSE NULL END order_col
FROM posts AS p
WHERE p.status = 'published' -- the only thing for filter
LIMIT 0,10 -- limit provided as well
) a
ORDER BY
a.order_col desc
,a.id DESC