简体   繁体   中英

Can this SQL Query be optimized

I have got below SQL query in Procedure, can this more optimized for best results.

SELECT DISTINCT 
      [PUBLICATION_ID] as n
     ,[URL] as u 
  FROM [LINK_INFO]
  WHERE Component_Template_Priority > 0 
    AND PUBLICATION_ID NOT IN (232,481) 
ORDER BY URL

Please suggest, is using NOT Exists is better way in this.

Thanks

It is possible to use NOT EXISTS. Just going from the code above you probably shouldn't, but it's technically possible. As a general rule; a very small, quickly resolved set (two literals would definitely apply) will perform better as a NOT IN than as a NOT EXISTS. NOT EXISTS wins when NOT IN has to do enough comparisons against each row that the correlated subquery for NOT EXISTS (which stops at the first match) resolves more quickly.

This assumes that the comparison set cannot include NULL. Otherwise NOT IN and NOT EXISTS do not return the same results because NOT IN ( NULL, ...) always returns NULL and therefore no rows whereas NOT EXISTS excludes rows for which it finds a match and NULL won't generate a match and so won't exclude the row.

A third way to compare two sets for mismatches is with an OUTER JOIN. I don't see a reason to go into that from what we've got so far, so I'll let that one go for now.

A definitive answer would depend on a lot of variables (hence the comments on your question)...

  1. What is the cardinality (number of different values) of the publication_id column?
  2. Is there an index on the column?
  3. How many rows are in the table?
  4. Where did you get the values in your NOT IN clause?
  5. Will they always be literals or are they going to come from parameters or a subquery?

... just to name a few. Of course, the best way to find out is by writing the query different ways and looking at execution times and query plans.

EDIT Another is with set operators like EXCEPT. Again, probably overkill to go into that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM