有人可以向我解释为什么第二个查询比第一个查询要快得多吗? (第一个需要大约40秒,第二个小于0.1)
SELECT releases.feature_code, releases.platform_id, version, release_date, general_available, uri, filesize, display_name, feature_name.full_name,
IF ((SELECT EXISTS(SELECT 1 FROM release_exceptions WHERE
releases.feature_code = release_exceptions.feature_code
AND releases.platform_id = release_exceptions.platform_id
AND releases.version = release_exceptions.version) = 1),
1, 0) as release_exceptions
FROM releases
LEFT OUTER JOIN platform USING(platform_id)
LEFT OUTER JOIN feature_name USING(feature_code)
ORDER BY version DESC;
~~~
QUERY 2 ------------------
~~~
SELECT feature_code, version, release_date, general_available, platform_id, uri, filesize, display_name, feature_name.full_name, COALESCE(release_exceptions, 0) AS release_exceptions
FROM mus.releases
LEFT OUTER JOIN (
SELECT DISTINCT feature_code, version, platform_id, 1 AS release_exceptions
FROM mus.release_exceptions
) release_exceptions
USING (feature_code, version, platform_id)
LEFT OUTER JOIN platform USING(platform_id)
LEFT OUTER JOIN feature_name USING(feature_code)
;
它生成release_exceptions列的方式似乎是速度的巨大差异。我的同事写下了第二个,但无法解释为什么它的速度要快得多。
http://imgur.com/dXLuo6S,noRYV7v#0
第一张图片是第一个查询 (注意 - 在发布后,feature_code,platform_id和版本被添加为索引,这导致第一个稍快一点)
答案 0 :(得分:0)
它不是导致加速的煤炭。这是删除子查询。第一个运行每行查询
第二个执行单个子查询