Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

How to optimize a query that's running slow on Nested Loops (Inner Join) Expand / Collapse
Author
Message
Posted Thursday, February 27, 2014 1:42 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 5:27 AM
Points: 968, Visits: 256
Hi All,

Please provide your suggestion on this issue.

Original Query :
select distinct
d.researchDocumentId,
d.isDeleted,
d.isExist,
case when d.purchaseDate > dateadd(day, -1, getdate()) then 1 else null end as hasPurchased, --1 day of download availabilityd.hasPurchased,
d.researchContributorId,
d.isBindingContributor,
p.researchProductId,
p.researchContentViewTypeId,
p.researchProductSalesModelId ,
case when p.researchEmbargoDays > 0 then p2d.embargoPeriodDate else null end as embargoEnd,
case when lm.researchConsumptionLimitId is not null then 1 else 0 end as limitExceeded,
case when p.researchProductTypeId=3 then 0 else p2d.price end as price,
d.purchaseDate
from
docsCte d
left join ResearchProductToDocument_tbl (nolock) p2d
on p2d.researchDocumentId=d.researchDocumentId
inner join #products p
on p2d.researchProductId = p.researchProductId
or (p.researchProductTypeId=3 and p.researchContributorId=d.researchContributorId)
left join #limitsExceeded lm
on
lm.researchProductSalesModelId = p.researchProductSalesModelId
and lm.researchDocumentId = p2d.researchDocumentId
where
p.researchEmbargoDays is null or p.researchEmbargoDays=0
or
(p.researchEmbargoDays > 0 and p2d.embargoPeriodDate < getdate()) --Embargo date check, don't include docs that we shouldn't see


  Post Attachments 
sql server central.png (3 views, 99.55 KB)
Post #1545754
Posted Thursday, February 27, 2014 1:47 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:18 AM
Points: 7,184, Visits: 13,638
Please post the actual execution plan as a .sqlplan file attachment.

“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1545757
Posted Thursday, February 27, 2014 2:04 AM
SSC Eights!

SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!SSC Eights!

Group: General Forum Members
Last Login: 2 days ago @ 5:27 AM
Points: 968, Visits: 256
pls find the original execution plan.



  Post Attachments 
Old.sqlplan (17 views, 920.93 KB)
Post #1545760
Posted Thursday, February 27, 2014 2:44 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 5:19 AM
Points: 42,766, Visits: 35,864
Table definitions and index definitions as well please

Could you post the plan for just that query run in isolation, not that and every single other statement in what looks like a long procedure?



Gail Shaw
Microsoft Certified Master: SQL Server 2008, MVP
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass

Post #1545769
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse