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

  • 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

  • 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

  • pls find the original execution plan.

  • 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

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply