Quantcast
Channel: SQLServerCentral » SQL Server 2014 » Development - SQL Server 2014 » Latest topics
Viewing all articles
Browse latest Browse all 3145

Not Exist causing major performance problem

$
0
0
I have the following code that includes a Not exist and when I run it takes 4 minutes. If I exclude it take 40 seconds to run.My thought was to create the temporary table without the Not exist and delete the record from the temp table.I need to add the columns in the select statement with a different alias.I'm hung up on this. Can anyone please provide a recommendation?This is the statement that is causing major performance issues.[code="sql"]--and not exists (select postalid from ContractorCoverageException cce where cce.contractorcoverageid = a.id and postalid = postal.id) [/code][code="sql"]select distinct n.contr_id, contr_status, program_type, u.abbreviation, z.ID, n.Contr_Trade, z.name as countyname,t.Trade_ds_tx into #tmpnexusfrom tblnexus n inner join contractor c on c.contr_id = n.contr_id inner join contractorcoverage a on a.ContractorID = c.contr_id inner join Postal_CountyRegion z on z.StateProvinceID = a.StateProvinceID and a.CountyRegionID = z.ID inner join vContractorProfiles p on p.contr_id = n.contr_id and a.CoverageTypeID = p.profileid inner join Postal_StateProvince u on u.ID = a.StateProvinceID inner join trades t on t.trade_id_nb = n.contr_trade inner join tradetablemap m on m.trade_id_nb = t.trade_id_nb inner join prismclients pc on pc.prismclientid = n.prismclientid inner join contractorstatus s on s.status_id = n.contr_status inner join contrtrade ct on ct.contr_id = n.contr_id inner join #tmpselect sel on sel.CountyRegionID = z.ID inner join Postal on postal.countyregionid = a.CountyRegionID and postal.stateprovinceid = a.StateProvinceID inner join #tmpcontrtrade tempc on tempc.contr_trade = n.contr_tradewhere sel.countyregionid = a.countyregionidand n.contr_trade = tempc.contr_trade and n.contr_status in (1,12,17,21,2,13,23,26,27,32,3,14,28,6,7,10,20,36,34,33,35,37) and c.activeforreports = 1 and pc.ActiveForReports = 1 and n.prismclientid in (1) and program_type in (1) and ( (n.contr_trade = 63 and p.contrcoverageareatypeid = Coalesce((Select Top 1 ID from ContractorCoverageType where ClientID = 1 and SingleTradeID = 48),2,1)) OR (n.contr_trade = 46 and p.contrcoverageareatypeid = Coalesce((Select Top 1 ID from ContractorCoverageType where ClientID = 1 and SingleTradeID = 28),1)) OR (n.contr_trade in (1,93,2,81,76) and p.contrcoverageareatypeid = Coalesce((Select Top 1 ID from ContractorCoverageType where ClientID = 1 and SingleTradeID is null),1)) OR (n.contr_trade not in (1,93,2,81,76,63,46) and p.contrcoverageareatypeid = 1) ) --and not exists (select postalid from ContractorCoverageException cce where cce.contractorcoverageid = a.id and postalid = postal.id) [/code]

Viewing all articles
Browse latest Browse all 3145

Trending Articles