Does using fully qualified names affect performance?

sql, sql-server, t-sql

Solution

Fully qualified names are usually preferred, but some considerations apply. I will say it depends a lot on the requirements and a single answer may not suffice all scenarios.

Note that this is just a compilation binding, not an execution one. So if you execute the same query thousand times, only the first execution will 'hit' the look up time, which means lookup time is less in case of fully qualified names. This also means using fully qualified names will save the compilation overhead (the first time when query is executed).

The rest will reuse the compiled one, where names are resolved to object references.

This MSDN Article gives a fair guidance on SQL Server best practices. (Check the section named: How to Refer to Objects)

This link explains in more details on set of steps done to resolve and validate the object references before execution: http://blogs.msdn.com/b/mssqlisv/archive/2007/03/23/upgrading-to-sql-server-2005-and-default-schema-setting.aspx

Going through the second link, the conclusion says that:

Obviously the best practice still stands: You should fully qualify all object names and not worry about the name resolution cost at all. The reality is, there are still many imperfect applications out there and this setting help great for those cases.

Also, in case the database name change is not allowed on production environment, you may then think to include database names in fully qualified names.

Problem

Does the use of fully qualified table names in SQL Server have any affect on performance? I have a query where I am joining two tables in different databases. A DBA has suggested to omit the database name on the host query, which I am guessing is either for performance or a convention. All tables fully qualified ``` USE [DBFoo] SELECT * FROM [DBFoo].[dbo].[people] a INNER JOIN [DBBar].[dbo].[passwords] b on b.[EntityID] = a.[EntityID] ``` Preferred? ``` USE [DBFoo] SELECT * FROM [dbo].[people] a INNER JOIN [DBBar].[dbo].[passwords] b on b.[EntityID] = a.[EntityID] ``` Does this actually make a difference?

Original source