Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Plan Reuse - Unrelated tables in execution plan


Plan Reuse - Unrelated tables in execution plan

Author
Message
Preet_S
Preet_S
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 Visits: 240
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
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47404 Visits: 44399
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


Preet_S
Preet_S
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 Visits: 240
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.
Attachments
Plan.doc (9 views, 268.00 KB)
HowardW
HowardW
Ten Centuries
Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)Ten Centuries (1.2K reputation)

Group: General Forum Members
Points: 1211 Visits: 9892
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.
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47404 Visits: 44399
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


Preet_S
Preet_S
SSC Veteran
SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)SSC Veteran (246 reputation)

Group: General Forum Members
Points: 246 Visits: 240
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 ?
GilaMonster
GilaMonster
SSC-Forever
SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)SSC-Forever (47K reputation)

Group: General Forum Members
Points: 47404 Visits: 44399
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


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search