Optimize MySQL query to avoid “Using where; Using temporary; Using filesort”

前端 未结 2 1826
春和景丽
春和景丽 2021-02-02 04:09

I built a custom forum for my site using MySQL. The listing page is essentially a table with the following columns: Topic, Last Updated, and

2条回答
  •  日久生厌
    2021-02-02 04:38

    You may want to break it up into a set of subqueries (as inner queries). I'd need the schema to really play, but if you

    SELECT t.id, t.name, MAX(COALESCE(r.date, t.date)) AS date, COUNT(r.id) AS replies  
    FROM (
       SELECT (id, name, date)
       FROM wp_pod_tbl_forum
       WHERE topic_id = 0  
    ) as t 
    LEFT OUTER JOIN
       wp_pod_tbl_forum r
    WHERE
       r.topic_id = t.id
    GROUP BY
        t.id
    ORDER BY
        date DESC LIMIT 0,20;
    

    that may help speed it up a little, it may not even be the best answer (errors may exist).

    There are tons of ways to do it, but the most important thing to do when SQL tuning is to reduce each set as much as possible before performing an operation.

提交回复
热议问题