开发者社区 问答 正文

下面的sql连接查询如何优化一下?

sql连接查询求优化
SELECT a.id FROM news` a LEFT
JOINnews_data b ON a.id=b.id LEFT JOIN hits h ON a.id=h.id
AND a.catid=h.catid WHERE a.catid=1 AND a.status IN(1,2,3,4,98,99)
ORDER BY a.id DESC LIMIT 0,15`

展开
收起
落地花开啦 2016-02-14 11:54:26 2151 分享 版权
1 条回答
写回答
取消 提交回答
  • 喜欢技术,喜欢努力的人

    建议不用join了, 因为你的where里的两个条件应该可以过滤大部分数据了,
    再在这基础上判断是否在news_data, hits两个表中是否存在即可.
    你可试试.
    `select a.id
    from news m
    where m.catid = 1

    and m.status in  (1,2,3,4,98,99)
    and exists ( select 1 from  news_data t1 where t1.id = m.id)
    and exists ( select 1 from hits t2 where t2.id = m.id and t2.catid = m.catid )

    order by a.id desc
    limit 0, 15;`

    2019-07-17 18:42:32
    赞同 展开评论
问答分类:
SQL
问答地址: