﻿<?xml version='1.0' encoding='UTF-8'?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>SQLServerCentral / SQL Server 2008 / SQL Server Newbies  / supplying a schema in queries, performance? / Latest Posts</title><generator>InstantForum.NET v2.9.0</generator><description>SQLServerCentral</description><link>http://www.sqlservercentral.com/Forums/</link><webMaster>notifications@sqlservercentral.com</webMaster><lastBuildDate>Thu, 23 May 2013 22:06:52 GMT</lastBuildDate><ttl>20</ttl><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>Thank you everyone for the replies :-DI have made a C# application that looks at a database and then goes through Triggers, Views and Stored procedures and looks for any references to other objects in the database that don't contain a schema before the object :). Hopefully from now it always returns no results as I have updated some systems with schemas before all objects.</description><pubDate>Thu, 14 Mar 2013 02:40:18 GMT</pubDate><dc:creator>BinaryDigit</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>[quote][b]BinaryDigit (3/12/2013)[/b][hr]HiA random question, if you write queries and put the schema before objects will it affect performance in any way and if so what type of metrics are we looking at? even if they are tiny :-DRegardsGordon Beeming[/quote]Cumulatively, yes, there can be a performance gain by schema-qualifying all your objects. It avoids the overhead of the engine having to find the object you meant to refernece either in the default schema (depends on context as shown above) or in the dbo schema. I like to see all objects schema-qualified. In my opinion it makes the code more readable and avoids potential for latent bugs becoming actual bugs.Consider the code example above. If someone were to drop the table testB.Cities for some reason the stored procedure would suddenly start returning Chicago...probably not an acceptable side-effect of dropping a table. Similarly, if when the proc had gone live at a time when there was no such table as testB.Cities and later someone added that table to the database the proc would suddenly stop returning Chicago and would start returning Atlanta. These are not things I would like to see happening when a table is added or removed from a database.</description><pubDate>Thu, 14 Mar 2013 01:08:14 GMT</pubDate><dc:creator>opc.three</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>Well, at least that makes sense. :-)</description><pubDate>Wed, 13 Mar 2013 07:04:27 GMT</pubDate><dc:creator>Ed Wagner</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>[quote][b]ryan.mcatee (3/13/2013)[/b][hr]To demonstrate this behavior:[code="sql"]CREATE SCHEMA testAGOCREATE SCHEMA testBGOCREATE TABLE dbo.Cities (city varchar(50))GOCREATE TABLE testA.Cities (city varchar(50))GOCREATE TABLE testB.Cities (city varchar(50))GOINSERT INTO dbo.Cities VALUES ('Chicago')INSERT INTO testA.Cities VALUES ('Cairo')INSERT INTO testB.Cities VALUES ('Atlanta')GOCREATE PROCEDURE testB.GetCitiesAS	SELECT * FROM CitiesGO	EXEC testB.GetCitiesGO[/code]Output is:Atlanta...CREATE PROCEDURE testB.GetCitiesWITH EXECUTE AS CALLER...CREATE PROCEDURE testB.GetCitiesWITH EXECUTE AS OWNERThese all yielded the same result.[/quote]Which means if you call a stored procedure in a specific schema and the table in that stored procedure is not accessed with a schema, it first looks in the schema of the stored proc (not dbo) to see if the table exists there.  Hmm, "default" (schema the stored proc resides) then dbo, not dbo, then schema.</description><pubDate>Wed, 13 Mar 2013 07:03:01 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>To demonstrate this behavior:[code="sql"]CREATE SCHEMA testAGOCREATE SCHEMA testBGOCREATE TABLE dbo.Cities (city varchar(50))GOCREATE TABLE testA.Cities (city varchar(50))GOCREATE TABLE testB.Cities (city varchar(50))GOINSERT INTO dbo.Cities VALUES ('Chicago')INSERT INTO testA.Cities VALUES ('Cairo')INSERT INTO testB.Cities VALUES ('Atlanta')GOCREATE PROCEDURE testB.GetCitiesAS	SELECT * FROM CitiesGO	EXEC testB.GetCitiesGO[/code]Output is:Atlanta...CREATE PROCEDURE testB.GetCitiesWITH EXECUTE AS CALLER...CREATE PROCEDURE testB.GetCitiesWITH EXECUTE AS OWNERThese all yielded the same result.</description><pubDate>Wed, 13 Mar 2013 06:16:22 GMT</pubDate><dc:creator>ryan.mcatee</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>[quote][b]Lynn Pettis (3/12/2013)[/b][hr][quote][b]ryan.mcatee (3/12/2013)[/b][hr]The behavior is different within a stored procedure.[/quote]Will have to research further later.It is interesting behaviour.[/quote]Especially when you deal with ownership chaining.</description><pubDate>Tue, 12 Mar 2013 14:40:03 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>[quote][b]ryan.mcatee (3/12/2013)[/b][hr]The behavior is different within a stored procedure.[/quote]Will have to research further later.It is interesting behaviour.</description><pubDate>Tue, 12 Mar 2013 14:38:34 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>The behavior is different within a stored procedure.</description><pubDate>Tue, 12 Mar 2013 14:33:55 GMT</pubDate><dc:creator>ryan.mcatee</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>[quote][b]Ed Wagner (3/12/2013)[/b][hr]The dbo schema first, then default.  Thank you, Lowell.  I just learned something.[/quote]Actually, if you do not provide the schema for the object it will look first in the users default schema, then it will look in the dbo schema.I just tested this myself.</description><pubDate>Tue, 12 Mar 2013 14:30:49 GMT</pubDate><dc:creator>Lynn Pettis</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>The dbo schema first, then default.  Thank you, Lowell.  I just learned something.</description><pubDate>Tue, 12 Mar 2013 12:46:00 GMT</pubDate><dc:creator>Ed Wagner</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>[quote][b]ryan.mcatee (3/12/2013)[/b][hr]To clarify, say we have three tables:schemaA.MyTableschemaB.MyTabledbo.MyTableIf my current-logged in user's default schema is schemaA and the default schema for the database is schemaB.  And we create a stored procedure named dbo.SomeStoredProcedure that does this:SELECT * FROM MyTableIt will actually query from dbo.MyTable, not schemaA.MyTable or schemaB.MyTable.If someone could explain this behavior, I would appreciate it.[/quote] for objects not qualified  with the schema name,SQL Server looks for the object in the default schema your user belongs to first, then in [b]dbo [/b]schema. if my default schema is SchemaC, SchemaA and SchemaB are not even checked to see if the object exists... only SchemaC then In your procedure, you would simply see that behaviour being applied:   objects not qualified with the schema name will assume dbo,  which is the schema of the containing object and if it doesn't exist, would fail with object not found. edit: corrected because my assumptions were way way wrong! Thanks Lynn Pettis!</description><pubDate>Tue, 12 Mar 2013 12:40:09 GMT</pubDate><dc:creator>Lowell</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>Every user in a database has a default schema whether they like it or not.  This is at the database user level, not the login level.  If you don't specify a schema in your query, you'll be querying the table in your default schema.  As far as I know, if you don't specify a schema, it won't look in any other schema for the table you query.</description><pubDate>Tue, 12 Mar 2013 12:39:15 GMT</pubDate><dc:creator>Ed Wagner</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>To clarify, say we have three tables:schemaA.MyTableschemaB.MyTabledbo.MyTableIf my current-logged in user's default schema is schemaA and the default schema for the database is schemaB.  And we create a stored procedure named dbo.SomeStoredProcedure that does this:SELECT * FROM MyTableIt will actually query from dbo.MyTable, not schemaA.MyTable or schemaB.MyTable.If someone could explain this behavior, I would appreciate it.</description><pubDate>Tue, 12 Mar 2013 12:24:00 GMT</pubDate><dc:creator>ryan.mcatee</dc:creator></item><item><title>RE: supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>I have a SQL server instructor (Bell) who told me it does require some CPU cycles to resolve a name if it is not qualified with a schema name.  Also, if you do NOT qualify the schema, what schema will be used?  (I think, by default, the schema that contains the stored procedure will be the default schema rather than the default schema of the current user or the default schema of the database).For these reasons, as a general practice, I always qualify database objects in stored procedures as two-part names.</description><pubDate>Tue, 12 Mar 2013 12:19:35 GMT</pubDate><dc:creator>ryan.mcatee</dc:creator></item><item><title>supplying a schema in queries, performance?</title><link>http://www.sqlservercentral.com/Forums/Topic1429821-1292-1.aspx</link><description>HiA random question, if you write queries and put the schema before objects will it affect performance in any way and if so what type of metrics are we looking at? even if they are tiny :-DRegardsGordon Beeming</description><pubDate>Tue, 12 Mar 2013 07:51:51 GMT</pubDate><dc:creator>BinaryDigit</dc:creator></item></channel></rss>