sql server - Which to Prefer IN vs Join vs Equals in SQL? -


i want manufacturer tbl_manufacturer. wrote 3 different queries manufacturers of item tbl_sales_orderitems table. want know 1 prefer. let me know query should used on other , why?

query 1: using subquery

select manufacturer   tbl_manufacturers  manufacturerid in(select trc.manufacturerid                            tbl_sales_repaircategory trc                           trc.repaircategoryid in (select repaircategoryid                                                            tbl_vendorparts                                                           vendorpartid in (select refid                                                                                    tbl_sales_orderitems                                                                                   typeid = 2                                                                                         , salesorderid = 182))) 

query 2: using sub-query distinct

select distinct manufacturer   tbl_manufacturers m,        tbl_vendorparts tvp,        tbl_sales_repaircategory trc  tvp.repaircategoryid = trc.repaircategoryid        , trc.manufacturerid = m.manufacturerid        , tvp.vendorpartid in (select refid                                   tbl_sales_orderitems                                  isnull(typeid, 0) = 2                                        , salesorderid = 182) 

query 3: using '=' (equals) distinct, join

select distinct manufacturer   tbl_manufacturers m,        tbl_vendorparts tvp,        tbl_sales_repaircategory trc  tvp.repaircategoryid = trc.repaircategoryid        , trc.manufacturerid = m.manufacturerid        , tvp.vendorpartid = (select refid                                  tbl_sales_orderitems                                 isnull(typeid, 0) = 2                                       , salesorderid = 182)  

here execuition plan: enter image description here

please suggest use?

suggestions appreciated!

  • in general, in == poor performance.
  • in general, joins way go.
  • pre-sql92 (old fashioned) joins have join condition in clause should coded using "new" (20+ years old) join syntax

none of proposed queries perform this:

select distinct manufacturer tbl_sales_orderitems s join tbl_vendorparts tvp on tvp.vendorpartid = s.refid join tbl_sales_repaircategory trc on tvp.repaircategoryid = trc.repaircategoryid join tbl_manufacturers m on trc.manufacturerid = m.manufacturerid (typeid = 2 or typeid null) , salesorderid = 182 

notes:

  • all joins have been converted proper joins
  • the table join order has been reversed, clause operates on first table listed, indexes can used , importantly few row access operations possible needed
  • typeid condition has been converted or avoid isnull() function being called

Comments

Popular posts from this blog

database - VFP Grid + SQL server 2008 - grid not showing correctly -

jquery - Set jPicker field to empty value -

.htaccess - htaccess convert request to clean url and add slash at the end of the url -