where fldValue=123 vs join to table with one row

  • I've run this test previously with similar results. I've attached the execution plan. I removed some columns so that we're just dealing with the view in question.

    Thanks! I appreciate the help.

    ST

  • Sean Pearce (1/25/2013)

    PLEASE NOTE: You have not aliased all your columns so I have no idea which tables they come from. You must edit the following queries to remove any non-aliased columns that are not from vwREPQICASSCostsByCC.

    Exactly! As I noted above, that's why I didn't try a re-write yet. I'm certain the query needs at least some re-write, but w/o knowing which table/view every column comes from, it's like coding in the dark.

    So, good general rules to follow are:

    1) Always alias tables when using multiple tables/views in one query;

    2) Always prefix every column with the appropriate alias when doing joins.

    This not only helps anyone else looking at the query, it can help you if/when you have to look at it 6 mths later and may not remember all the table details yourself.

    SQL DBA,SQL Server MVP(07, 08, 09) "Money can't buy you happiness." Maybe so, but it can make your unhappiness a LOT more comfortable!

  • souLTower (1/25/2013)


    I've run this test previously with similar results. I've attached the execution plan. I removed some columns so that we're just dealing with the view in question.

    Thanks! I appreciate the help.

    ST

    My original response still holds true. The view definition is not giving the optimizer a chance to find the optimum plan.

    StatementOptmEarlyAbortReason="TimeOut"

    What does the execution plan for the following look like?

    SELECT

    v.fldConsulKey,

    v.fldPlanKey

    FROM

    tblConsul v

    WHERE

    v.fldPLanKey IN (SELECT X.fldIMPPlanKey FROM #tmpPlan X);

    SELECT

    v.fldConsulKey,

    v.fldPlanKey

    FROM

    tblConsul v

    WHERE

    v.fldPLanKey IN (30472);

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

  • Thanks for the response. I've attached the execution plans.

    I find it weird that the optimizer chose in both cases to use the non-clustered index for the seek. The table has a clustered index on fldPlanKey precisely to avoid this.

    IDX_tblConsul_PlanKey clustered located on PRIMARY fldPlanKey

  • Can you please supply DDL for tblConsul and #tmpPlan, including all indexes.

    While you are there, can you supply the view definition.

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

Viewing 5 posts - 16 through 19 (of 19 total)

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