Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Plan Reuse - Unrelated tables in execution plan Expand / Collapse
Author
Message
Posted Thursday, April 18, 2013 5:42 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, December 20, 2013 4:45 AM
Points: 236, Visits: 239
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.

SELECT TOP 1000 * FROM FactTable fld (NOLOCK)
WHERE PartitionKey = 50230216

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
Post #1443756
Posted Thursday, April 18, 2013 6:11 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 41,530, Visits: 34,447
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 2008, MVP
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

Post #1443780
Posted Thursday, April 18, 2013 7:56 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, December 20, 2013 4:45 AM
Points: 236, Visits: 239
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.


  Post Attachments 
Plan.doc (9 views, 268.00 KB)
Post #1443880
Posted Thursday, April 18, 2013 8:05 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Thursday, April 17, 2014 10:09 AM
Points: 1,207, Visits: 9,333
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.
Post #1443885
Posted Thursday, April 18, 2013 8:06 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 41,530, Visits: 34,447
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 2008, MVP
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

Post #1443886
Posted Thursday, April 18, 2013 9:59 AM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Friday, December 20, 2013 4:45 AM
Points: 236, Visits: 239
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.

So Gail, NOLOCK, what's the problem with that ?
Post #1443972
Posted Thursday, April 18, 2013 10:52 AM


SSC-Forever

SSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-ForeverSSC-Forever

Group: General Forum Members
Last Login: Today @ 7:59 AM
Points: 41,530, Visits: 34,447
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 2008, MVP
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

Post #1444009
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse