How to make JOIN query use index?

前端 未结 6 1786
无人共我
无人共我 2021-02-07 04:49

I have two tables:

CREATE TABLE `articles` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(1000) DEFAULT NULL,
  `last_updated` datetime DEFAULT NULL         


        
6条回答
  •  滥情空心
    2021-02-07 05:39

    I would have the following indexes available

    articles table -- INDEX ( deleted, last_updated, comment_cnt )

    article_categories table -- INDEX ( article_id, category_id ) -- you already have this index

    then add Straight_Join to force doing the query as listed instead of it trying to use the article_categories table via whatever statistics it may have to help the query.

    SELECT STRAIGHT_JOIN
          a.*
       FROM
          articles AS a
             JOIN article_categories AS c
                ON a.id = c.article_id
                AND c.category_id = 78
       WHERE
              a.deleted = 0
          AND a.comment_cnt > 0
       ORDER BY 
          a.last_updated
       LIMIT 
          100, 20
    

    As per comment / feedback, I would consider reversing based on set if category records is much smaller basis... such as

    SELECT STRAIGHT_JOIN
          a.*
       FROM
          article_categories AS c
             JOIN articles as a
                ON c.article_id = a.id
               AND a.deleted = 0
               AND a.Comment_cnt > 0
       WHERE
          c.category_id = 78
       ORDER BY 
          a.last_updated
       LIMIT 
          100, 20
    

    In this case, I would ensure an index on the articles table by

    index -- (id, deleted, last_updated)

提交回复
热议问题