August 28, 2015 at 8:15 am
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.
--and not exists (select postalid from ContractorCoverageException cce where cce.contractorcoverageid = a.id and postalid = postal.id)
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 #tmpnexus
from 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_trade
where
sel.countyregionid = a.countyregionid
and 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)
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 28, 2015 at 8:16 am
Read the article on your signature on how to post performance questions.
August 28, 2015 at 8:37 am
Thank you.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 28, 2015 at 9:22 am
All that I want to do is remove the not exist and delete from the Temp table.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 28, 2015 at 11:02 am
Help us help you. Provide what is asked in the articles you reference in your own signature block.
August 28, 2015 at 5:28 pm
You've been around long enough to know what's needed to address a problem.
Like Luis and Lynn said, read your own signature block.
August 28, 2015 at 6:49 pm
ok, I will resolve the problem myself.
No need for additional post.
Thank you.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 28, 2015 at 6:55 pm
Welsh Corgi (8/28/2015)
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.
--and not exists (select postalid from ContractorCoverageException cce where cce.contractorcoverageid = a.id and postalid = postal.id)
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 #tmpnexus
from 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_trade
where
sel.countyregionid = a.countyregionid
and 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)
Before you go to all that trouble, try changing "postalid" to "*" or "1" in the not exists and see what happens.
--Jeff Moden
Change is inevitable... Change for the better is not.
August 28, 2015 at 7:47 pm
You can also get rid of the not exist altogether by doing a LEFT JOIN to ContractorCoverageException cce...
Then in the WHERE clause...
WHERE cce.contractorcoverageid IS NULL
August 30, 2015 at 7:40 am
Before I read the replies I replaced the not exist with a left join.
It takes 5 minutes either way.
Thank you.
left JOIN ContractorCoverageException cce ON cce.contractorcoverageID = a.ID and cce.PostalID = postal.ID and e.postalid is null
-- and not exists (select postalid from ContractorCoverageException e where e.contractorcoverageid = ContractorCoverage.id and postalid = postal.id)
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 30, 2015 at 7:42 am
Jeff Moden (8/28/2015)
Welsh Corgi (8/28/2015)
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.
--and not exists (select postalid from ContractorCoverageException cce where cce.contractorcoverageid = a.id and postalid = postal.id)
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 #tmpnexus
from 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_trade
where
sel.countyregionid = a.countyregionid
and 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)
Before you go to all that trouble, try changing "postalid" to "*" or "1" in the not exists and see what happens.
ok I will do that, Thank you.
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 30, 2015 at 7:46 am
.Jeff,
I'm not sure how to change the code?
Thanks again.
left JOIN ContractorCoverageException cce ON cce.contractorcoverageID = a.ID and cce.PostalID = postal.ID and e.postalid is null
-- and not exists (select postalid from ContractorCoverageException e where e.contractorcoverageid = ContractorCoverage.id and postalid = postal.id)
For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/
For better answers on performance questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/
August 30, 2015 at 1:50 pm
and not exists (select * from ContractorCoverageException cce where cce.contractorcoverageid = a.id and postalid = postal.id)
--Jeff Moden
Change is inevitable... Change for the better is not.
September 1, 2015 at 1:40 pm
Have you tried the following?
-- in the joins section
left outer join ContractorCoverageException e where e.contractorcoverageid = ContractorCoverage.id and e.postalid = postal.id
-- in the where section
and e.postalid is null
September 1, 2015 at 1:49 pm
... and of course, you have a suitable (unique? clustered? as appropriate) index on ContractorCoverageException, like:
... ContractorCoverageException (contractorcoverageid, postalid)
or
... ContractorCoverageException (contractorcoverageid) include (postalid)
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply