sql - TOP 1 or EXISTS - Which is faster? -
i have table in database has 2 million records. need find out if more 1 record available customer. have 2 following scripts:
select top 1 ex.exceptionid exception ex ex.customerid='{1bf87745-de00-47ff-9ac0-430baf87679a}' , ex.loanid null , ex.statustype = 'required' , ex.exceptionstate <> 'n' select (case when exists ( select ex.exceptionid exception ex ex.customerid = '{1bf87745-de00-47ff-9ac0-430baf87679a}' , ex.loanid null , ex.statustype = 'required' , ex.exceptionstate <> 'n') 1 else 0 end) customerexceptioncount which 1 faster , why..?
many thanks..!
don't want like,
declare @morethanone bit; select @morethanone = case when count(*) > 1 1 else 0 end ( select top 2 1 [exception] [customerid] ='{1bf87745-de00-47ff-9ac0-430baf87679a}' , [loanid] null , [statustype] = 'required' , [exceptionstate] <> 'n' ) [firsttwo]
Comments
Post a Comment