Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


How to optimize a query that's running slow on Nested Loops (Inner Join)


How to optimize a query that's running slow on Nested Loops (Inner Join)

Author
Message
kbhanu15
kbhanu15
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1325 Visits: 335
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
Attachments
sql server central.png (9 views, 99.00 KB)
ChrisM@Work
ChrisM@Work
SSChampion
SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)SSChampion (10K reputation)

Group: General Forum Members
Points: 10721 Visits: 19243
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
kbhanu15
kbhanu15
Ten Centuries
Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)Ten Centuries (1.3K reputation)

Group: General Forum Members
Points: 1325 Visits: 335
pls find the original execution plan.
Attachments
Old.sqlplan (21 views, 920.00 KB)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)SSC Guru (57K reputation)

Group: General Forum Members
Points: 57329 Visits: 44707
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, MVP, M.Sc (Comp Sci)
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search