Optimizing ORDER BY

前端 未结 5 1666
野趣味
野趣味 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:47

    • Inflate-deflate -- LEFT JOIN inflates the number of rows, GROUP BY then deflates. The inflated number of rows is costly. Instead, focus on getting the ids for the desired rows before doing any JOINing. With luck, you can get rid of the GROUP BY.

    • WP schema -- This is an EAV schema which sucks when it comes to performance and scaling.

    • What indexes do you have? See this for how to improve the meta table.

    • Complex ORDER BY. This leads to gathering all the rows (after filtering) before sorting and doing the LIMIT. Rethink the ORDER BY clause, if possible.

    After you have done what you can with my suggestions, start another Question to continue the refinement. Be sure to include EXPLAIN SELECT ... and SHOW CREATE TABLE.

提交回复
热议问题