SMS/SCCM Query Performance Tips - Addressing objects with the schema name (.dbo)

A commonly overlooked little performance tip in SQL is addressing your objects (like tables, views, functions, procedures, etc.) using the schema (aka, the owner) name in front of the object.

For example, usually people will write a simple SMS query like this:

SELECT sys.resourceID, sys.name0
  FROM v_R_System AS sys
 WHERE sys.Client0 = 1
   AND sys.Obsolete0 = 0

When the SQL engine goes out to find the data here, it has to first determine what schema the v_R_System view belongs to which is admittedly pretty fast, but all things being equal takes a little more processing:
    Is there a jnelson.v_R_System view? No. 
    Is there a dbo.v_R_System view? Yes. 
    OK, pull data from there.

Next, after the query is run, the way I understand it is that SQL will cache the query plan separately for each user that runs the exact same query.  So there's no sharing of the cached execution plan and everyone incurs the query compilation cost.

So, the solution is to always include the schema when addressing your objects so that when the SQL engine goes out to find the execution plan it can say, "Oh, someone's already pulled that same data, lemme just get that  for you..."

Here's how the above snippet should be rewritten then - the schema/owner shown in RED:

SELECT sys.resourceID, sys.name0
  FROM dbo.v_R_System AS sys
 WHERE sys.Client0 = 1
   AND sys.Obsolete0 = 0

One last note, if you address the same object multiple times in your query, make SURE you put the schema in front of ALL instances of ALL objects or you will still get hit with that query compilation.

Number2 (John Nelson)
MyITForum - Forum Posts
MyITForum - Blog
Add to Google

Published Tuesday, October 09, 2007 3:13 PM by jnelson

Comments

# Turning Rows into Columns using PIVOT

Tuesday, October 16, 2007 11:58 AM by John Nelson (Number2) - at MyITForum.com

I was looking at the ever-popular (but strangely absent this summer Big Smile) Paul Thomsen's blog recently and I noticed this article entitled "Reporting on client version for multiple sites". His code does some cool stuff so I did what normal

# M@d Skillz - BITWISE "AND" with a PIVOT thrown in for pretty :)

Monday, November 19, 2007 11:01 PM by John Nelson (Number2) - at MyITForum.com

M@d SKILLZ ARTICLE ALERT! (This is gonna get cr@zy, if you have a helmet, now's the time to put it

Powered by Community Server (Commercial Edition), by Telligent Systems