Optimizing ORDER BY

前端 未结 5 1624
野趣味
野趣味 2021-01-21 08:21

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

5条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-21 08:58

    Here's your problem:

    • "ORDER BY expression": the expression has to be computed for each row in the table, then the sort done on the whole table, then the results go through the LIMIT.
    • No index use: "ORDER BY col" when "col" is part of an index can eliminate the sort by going through the index in-order. This is very efficient when using LIMIT. However, it will not work here.

    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.

提交回复
热议问题