Force a query to use a specific execution plan

  • Hi guys,

    I have two tables:

    cpCases. Index on polref/Caseno (non clustered and not marked as unique, even though it is)

    CaseExceptions: Index on polref/caseNo/ReasonID (non clustered and not marked as unique, even though it is)

    Each case will have 0..n CaseExceptions

    I want a list of all cpCases without a caseExceptions record

    The following query

    SELECT top 231000

    c.caseno

    FROM

    cpcases C

    left join

    caseExceptions X on X.cppolref = c.cppolref

    and X.caseno = c.caseno

    WHERE

    X.caseno is null

    Generates an execution plan that includes an index scan on caseExceptions and an index scan on cases and returns the results in 2 seconds

    as soon as I increase the Top to 232000 the plan changes to use a table scan on CaseExceptions and the query takes minutes to complete. How can I force the Query to use a specific query plan. the expected results set should be around 700,000 records

  • Never mind,

    the following query gives me the desired result and runs in 11 seconds

    select caseno from cpcases

    except

    select caseno from caseExceptions

    I would still be interested in the answer to the original question

  • Yes, in a limited sense. Check out Plan Guides in BOL.

    “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

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

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