bruno
bruno

Reputation: 1841

Are table aliases faster than tablenames?

Are table aliases faster than just using the tablename instead?
Or doesn't this make any difference?
And do you guys have some good tips to use table aliases? Or recommends/non-recommendations?

Thanks,
Bruno

Upvotes: 1

Views: 277

Answers (2)

HLGEM
HLGEM

Reputation: 96572

They aren't faster but they do make the code easier to read. And they are required for somethings such as when you join to a table more than once and when you use a derived table.

I find it a good practice to always use aliases and to alias all columns in the select so that the maintainer doesn't have to figure out which table in a complex query each field came from six months later.

Upvotes: 1

Quassnoi
Quassnoi

Reputation: 425371

No, they are not.

The query engine does not even see them (they only appear in the comments in the plan).

When executing the query, the engine deals with the actual physical objects (tables, indexes etc.), according to the query plan.

When the query says something like this:

SELECT  *
FROM    mytable t

, the optimizer creates a plan similar to this:

|--Clustered Index Scan(OBJECT:([mydb].[dbo].[mytable].[pk_mytable] AS [t]))

and the query engine interprets is like "find the first page of the index pk_mytable and traverse the linked list of the pages, returning all records found, until there are no more records".

On this stage, it's not important how exactly did you alias the query: the engine deals with physical objects like pages, pointers etc., not logical things like aliases.

Upvotes: 5

Related Questions