Inefficient SQL Query

前端 未结 5 813
温柔的废话
温柔的废话 2021-02-19 08:08

I\'m building a simple web app at the moment that I\'ll one day open source. As it stands at the moment, the nav is generated on every page load (which will change to be cached

5条回答
  •  無奈伤痛
    2021-02-19 08:59

    I'd try this one:

    SELECT
        c.slug,c.name,s.name
    FROM
        categories c
    LEFT JOIN snippets s
        ON s.category = c.id 
    WHERE live=1 ORDER BY c.name, s.name
    

    I didnt test it, though. Also check the indexes using the EXPLAIN statement so MySQL doesnt do a full scan of the table.

    With these results, you can loop the results in PHP and check when the category name changes, and build your output as you wish.

提交回复
热议问题