首页 > 其他 > 详细

查询语句优化

时间:2014-03-24 16:06:25      阅读:496      评论:0      收藏:0      [点我收藏+]


原查询:select tid, fid, subject, author, authorid from dz_forum_thread where authorid in (select uid from dz_common_member where groupid in (1, 3, 23)) and dateline > 1395663240 and highlight <> 40 (用时10s)


优化后:select tid, fid, subject, author, authorid from dz_common_member as m, dz_forum_thread as t where m.uid=t.authorid and m.groupid in (1,3,23) and t.dateline > 1395663240 and t.highlight <> 40

在同时取1000条记录的情况下


SELECT tid, fid, subject, author, authorid
FROM dz_common_member AS m, dz_forum_thread AS t
WHERE m.uid = t.authorid
AND m.groupid
IN ( 1, 3, 23 )
AND t.dateline > 1305663240
AND t.highlight <>40
LIMIT 1000

用时0.0148s



SELECT tid, fid, subject, author, authorid
FROM dz_common_member AS m, dz_forum_thread AS t
WHERE m.uid = t.authorid
AND (m.groupid=1 or m.groupid=3 or m.groupid=23)
AND t.dateline >1305663240
AND t.highlight <>40
LIMIT 1000

用时0.0122s

总结:
1. 尽量不要使用子查询

2. 如果in的数目固定且比较少,可以用or替换

查询语句优化,布布扣,bubuko.com

查询语句优化

原文:http://www.cnblogs.com/zhaozhilu/p/3620428.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!