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
Here's your problem:
There are ways out of this mess, but you will need to tell how many different levels of "reputation" you have (like 3, or like "a lot") and how they are statistically distributed (like, 1 user with reputation 100 and the rest all have zero, or evenly distributed).
EDIT
Hmm, no information on the statistical distribution of "reputation" or its possible range of values. In this case, let's go with the blunt approach:
Let's add a column "repdate" which contains:
repdate = p.created_at + INTERVAL p.reputation DAY
This corresponds to shifting posts one day into the future for each reputation point they have. They will then sort accordingly. Adjust to taste if p.created_at is not a DATETIME.
Now, we can simply "ORDER BY repdate DESC" and with an index on it, it will be fast.