更新:
只是在更明显的地方提到它。当我将IN更改为=时,查询执行时间从180缩短为0.00008秒。速度差可笑。
此SQL查询需要180秒才能完成!那怎么可能?有没有一种方法可以优化它更快?
SELECT IdLawVersionValidFrom FROM question_law_version WHERE IdQuestionLawVersion IN ( SELECT MAX(IdQuestionLawVersion) FROM question_law_version WHERE IdQuestionLaw IN ( SELECT MIN(IdQuestionLaw) FROM question_law WHERE IdQuestion=236 AND IdQuestionLaw>63 ) )
每个表中只有大约5000行,因此它应该不会太慢。
(发布我的评论作为答案,显然确实有所作为!)
如果将更改为,IN 有=什么区别?
IN
=
如果有人想进一步调查,我刚刚进行了测试,发现它很容易复制。
建立表格
CREATE TABLE `filler` ( `id` int(11) NOT NULL AUTO_INCREMENT, PRIMARY KEY (`id`) )
建立程序
CREATE PROCEDURE `prc_filler`(cnt INT) BEGIN DECLARE _cnt INT; SET _cnt = 1; WHILE _cnt <= cnt DO INSERT INTO filler SELECT _cnt; SET _cnt = _cnt + 1; END WHILE; END
填充表
call prc_filler(5000)
查询1
SELECT id FROM filler WHERE id = (SELECT MAX(id) FROM filler WHERE id = ( SELECT MIN(id) FROM filler WHERE id between 2000 and 3000 ) )
等于说明输出http://img689.imageshack.us/img689/5592/equals.png
查询2(相同的问题)
SELECT id FROM filler WHERE id in (SELECT MAX(id) FROM filler WHERE id in ( SELECT MIN(id) FROM filler WHERE id between 2000 and 3000 ) )
在说明输出中http://img291.imageshack.us/img291/8129/52037513.png