Plan Reuse - Unrelated tables in execution plan

  • Hi all

    I have been banging my head against a brick wall over this one. Hopefully somebody can help.

    One our users ran a simple query on a partitioned table but for some reason it was taking an age to execute. The table is accessed via a simple view in another database.

    [Code]

    SELECT TOP 1000 * FROM FactTable fld (NOLOCK)

    WHERE PartitionKey = 50230216

    [/Code]

    However, the execution plan shows tables that have not even been specified in the query which is the reason why it is taking so long. Obviously, the Optimizer is reusing an existing plan but this is not the best choice.

    I have tried ;

    -creating a proc and executing sp_compile against it

    -clearing the cache (DBCC FREEPROCCACHE)

    -specifying WITH RECOMPILE against the proc

    There is a composite non-clustered index on the base table which includes the PartitionKey (2nd col of index).

    Statistics update automatically.

    No matter what I try, it keeps producing this sub-optimal execution plan containing the two unrelated tables. Anybody know why it would do this especially given that the cache has been cleared ?

    Thanks

    Preet

  • Why do you think it has unrelated tables in?

    The optimiser will NOT use a plan for one query for another query. It cannot. For ad-hoc queries the plan matching is on the exact SQL text, hence using one query's plan will never match the plan lookup for a different query. For procedures the lookup is on the object ID, hence one procedure can never match the plan lookup for a different query

    sp_recompile on a procedure will remove it's plan from cache, next time it runs it has to compile fresh.

    DBCC FREEPROCCACHE clears the entire plan cache, no plans can survive that, all queries running after that will compile fresh plans.

    WITH RECOMPILE on the proc and SQL will never even cache the plan, it compiles a fresh plan every single time.

    Post the plan?

    p.s. Nolock? You do know the effects of that? (and I don't mean just dirty reads)

    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
  • Hi Gail, thanks for posting.

    GilaMonster (4/18/2013)


    Why do you think it has unrelated tables in? )

    Because those tables were not in the query. Why on Earth would other tables need to be scanned? Surely it would be more efficient to go to pages the appropriate partition and get the first 1000 rows ?

    The optimiser will NOT use a plan for one query for another query.)

    Agreed, my misunderstanding. Except in the case of Parameter Sniffing.

    I am aware of the purpose of

    sp_recompile

    DBCC FREEPROCCACHE

    WITH RECOMPILE

    These were used in an attempt to generate a plan specific to the adhoc query (which you are saying would have been the case anyway). The plan just seems inefficient. In this case using other tables to filter the main fact table is taking too long.

    Post the plan?

    Attached. Sorry it's not the xml, Corporate Paranoia. Hopefully there is enough there. Basically

    [FactTable] is what I am querying, Table scan on [Dim_Objective] (75%) is what I am unhappy with. I know about the benefits of the Bitmap operator but in this case it is at the cost of another table scan.

    p.s. Nolock? You do know the effects of that? (and I don't mean just dirty reads)

    Other than dirty reads, No. Please elaborate.

  • Would the fact that your "table" is called View_FactTable have anything to do with it? E.g. it must be a view that references other tables if those other tables are in the plan.

    And you're misunderstanding Parameter Sniffing - it's the same query with different parameters, not a different query.

  • Preet_S (4/18/2013)


    The optimiser will NOT use a plan for one query for another query.)

    Agreed, my misunderstanding. Except in the case of Parameter Sniffing.

    No exceptions. Parameter sniffing is the same query, same execution plan, different parameter value. Not a different query.

    Post the plan?

    Attached. Sorry it's not the xml, Corporate Paranoia. Hopefully there is enough there.

    Picture of the plan's not very useful.

    Also, all of those are a query against a view, not the query against the single table that you initially posted. If the view has those two tables in it, then querying the view will, depending on exactly what the view does, have more than one table in the resultant plan.

    Going to need to see the view definition to explain why the other tables are there.

    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
  • Apologies. The view used to be a simple reference to the base table. It has been changed since I last looked at it. Seems I was making it more complicated than required. Those tables ARE in the view. :rolleyes:

    So Gail, NOLOCK, what's the problem with that ?

  • In summary, incorrect data. Not dirty reads, missing rows and duplicate rows. You can read entire chunks of data twice (or more). You can miss entire chunks of rows.

    Fine for a dashboard where close enough is acceptable, not particularly good if users expect their data to actually be correct (most do).

    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 7 posts - 1 through 6 (of 6 total)

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