开发者

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 
                       开发者_StackOverflow中文版 FROM   datadictionary dict 
                        WHERE  dict.fid = 1014) 

one thing about structure is . vCode,vName is varchar and testCodes and DataDictionary have same structure.

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:

  • (testcodes.vcode)
  • (datadictionary.vcode,datadictionary.fid)

Both do a single index scan on each table, but the IN has a Merge Join and the INNER JOIN has a Hash Match.


If dict.fid is a unique key (sounds so), then your query should be equivalent to

WHERE  co.vcode != (SELECT dict.vcode -- ...

co.vcode and dict.vcode might need an index to speed things up.

This answer is not an attempt to give a better hint than Pentium10, more a sidenote.


The query looks OK. Try add the following indexes

datadictionary Index (fid,vcode)
testCodes Index (vcode)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜