sql server - How to improve this SQL? -


my boss sent query , asked replace more efficient version , achieve following objective:

get records package table has @ least 1 record in pass_package_details table.

given sql:

select distinct      pckg.*        pass_package pckg  join      pass_package_details pckg_dtl on (pckg.package_id = pckg_dtl.package_id)       is_active = 1      , '2015/04/22' between date_start , date_end  order      package_name 

correct me if i'm wrong, believe query above slow down performance due join method. after reading this, i'm wondering of query achieved boss requirement , why.

my sql :

attempt #1 - using in:

select     pckg.*      pass_package pckg      is_active = 1     , '2015/04/22' between date_start , date_end      , pckg.package_id in (select distinct pckg_dtl.package_id                              pass_package_details pckg_dtl) order      package_name 

attempt #2 - using exists:

select      pckg.*      pass_package pckg      is_active = 1     , '2015/04/22' between date_start , date_end      , exists (select pckg_dtl.package_id                  pass_package_details pckg_dtl                 pckg_dtl.package_id = pckg.package_id) order      package_name 

hope valuable information experts here!

edit: i'm using sql server management studio. execution time kinda weird. in method, 1st execution take around 90+ ms, 2nd execution 200 ms, 3rd execution 90+ ms. i'm not sure execution time should refer to. same goes exists method.

exists , in equivalent, , both @ best optimized join, , @ worst.. well, not.

you spent few minutes typing entire post, suggest looking @ execution plan instead better use of time. lost credibility @ "i believe" (even before reading rest , seeing you're wrong) instead of showing hard data support each alternative.

edit: note queries aren't equivalent. top 1 (useless) sort , filter (distinct), other 2 don't.


Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -