tsql - Performance of SQL Query killed by join -
we have problem performance of query in sql server 2008 r2. query joins tables 2 databases, 1 our data mart getting data iseries system, , 1 our main reporting system.
in below query should 90939 records in less 30 seconds. if remove column po.powner, results come in 10 seconds.
but when add po.powner column killing it.
select cu.* ,c4.c4ind, cu.cus_citizenship [country of incorporation], cu.cus_nationality [residence country],[firstparticipantid]=isnull(po.powner,cu.cus_no) -- , cu.cus_no collate latin1_general_cs_as, po.powner collate latin1_general_cs_as dbo.customerdata cu left outer join hermes_import.dbo.kfildto_c4pf c4 on cu.cus_type collate latin1_general_cs_as =c4.c4ctp collate latin1_general_cs_as outer apply ( select top 1 case when gfctp = 'gg' , len(y41scv) > 5 y41scv else y41cus end 'powner', gfctp hermes_import.dbo.kfildto_y41pf inner join hermes_import.dbo.kfildto_gfpf on gfcus = y41cus y41typ in ('bn', 'bb', 'co') , y41del = 0 , y41dec = 0 , y41cus collate latin1_general_cs_as = cu.cus_no collate latin1_general_cs_as ) po left(cu.[cus_no],1)< '8' , (cu.[cus_type])<>'za' , datadate='2014-04-30' order cus_no
here execution plan (just top 3000 records):
when take out primary owner column , results in 11 seconds , here execution plan:
what can differently primary owner, bearing in mind want first record of 6 possible participanst in customers account
the reason don't poor performance when comment out po
fields outer apply / top 1
guaranteed result 1 record no matter what.
if don't care contents of record, sql server
optimize whole clause out, does.
create following indexes:
hermes_import.dbo.kfildto_y41pf (y41cus, y41del, y41dec, y41typ) hermes_import.dbo.kfildto_gfpf (gfcus)
also using top 1 without order by
in outer apply
. sure don't care record selecting?
Comments
Post a Comment