[英]SQL query 'not in' clause execution takes too long
the following query takes too much time, most likely because of a 'not in' use. 以下查询花费太多时间,很可能是因为“不在”使用。
Can you suggest any improvement ? 你能建议改进吗?
SELECT vcode,
vname,
1014 AS fid
FROM testcodes co
WHERE co.vcode NOT IN (SELECT dict.vcode
FROM datadictionary dict
WHERE dict.fid = 1014)
one thing about structure is . 关于结构的一件事是。 vCode,vName is varchar and testCodes and DataDictionary have same structure.
vCode,vName是varchar,testCodes和DataDictionary具有相同的结构。
I searched this problem and found that the left join can possibly resolve this? 我搜索了这个问题,发现左连接可以解决这个问题? (WHY does it do better and how can it be done)?
(为什么它做得更好,如何做到)?
can someone guide if it can be improved ??? 有人可以指导它是否可以改进???
SELECT vcode,
vname,
1014 AS fid
FROM testcodes co
LEFT JOIN datadictionary dict
ON co.vcode = dict.vcode
AND dict.fid = 1014
WHERE dict.vcode IS NULL
You need to have indexes created on: 您需要创建索引:
Both do a single index scan on each table, but the IN has a Merge Join and the INNER JOIN has a Hash Match. 两者都在每个表上执行单个索引扫描,但IN具有合并连接,INNER JOIN具有哈希匹配。
If dict.fid is a unique key (sounds so), then your query should be equivalent to 如果dict.fid是唯一键(听起来如此),那么您的查询应该等效于
WHERE co.vcode != (SELECT dict.vcode -- ...
co.vcode and dict.vcode might need an index to speed things up. co.vcode和dict.vcode可能需要一个索引来加快速度。
This answer is not an attempt to give a better hint than Pentium10, more a sidenote. 这个答案并不是试图提供比Pentium10更好的暗示,更多的是旁注。
The query looks OK. 查询看起来没问题。 Try add the following indexes
尝试添加以下索引
datadictionary Index (fid,vcode) datadictionary索引(fid,vcode)
testCodes Index (vcode) testCodes索引(vcode)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.