|
原帖由 ubuntuhk 于 15-6-2009 21:18 发表
SELECT p.subject AS re_subject, p.message, t.* FROM cdb_threads t LEFT JOIN cdb_posts p ON (t.tid=p.tid AND t.lastpost=p.dateline)
WHERE t.fid IN (7053, 200, 7033, 7062, 7061, 7036, 7035, 7067) AND t.displayorder IN (0)
ORDER BY t.lastpost DESC
LIMIT 25798, 30;
索引好办,不过我觉得这个查询应该这样写
- SELECT p.subject AS re_subject, p.message, t.* FROM (select * from cdb_threads WHERE t.fid IN (7053, 200, 7033, 7062, 7061, 7036, 7035, 7067) AND t.displayorder IN (0) ORDER BY t.lastpost DESC) t LEFT JOIN cdb_posts p ON (t.tid=p.tid AND t.lastpost=p.dateline)LIMIT 25798, 30;
复制代码
这样如果cdb_threads很大的话,一般会先得到一个较小的结果集,然后再用这个小的临时表LEFT JOIN (LEFT JOIN很费资源的), 这是我n年前用ORACLE的经验,Mysql和现在的ORACLE能否自动优化这个查询不好说。一般而言用ORACLE的话会先看看实际的SQL执行计划是什么样的,在决定如何优化。 |
|