表别名比表名更快吗?

发布于 2024-09-29 22:15:48 字数 87 浏览 0 评论 0原文

表别名是否比仅使用表名更快?
或者这没有什么区别吗?
你们有一些使用表别名的好技巧吗?或者推荐/不推荐?

谢谢,
布鲁诺

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鸠魁 2024-10-06 22:15:48

不,他们不是。

查询引擎甚至看不到它们(它们只出现在计划的注释中)。

执行查询时,引擎根据查询计划处理实际的物理对象(表、索引等)。

当查询说这样的话: 时

SELECT  *
FROM    mytable t

,优化器会创建一个类似于这样的计划:

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

查询引擎解释为“找到索引的第一页 pk_mytable 并遍历页面的链接列表,返回找到的所有记录,直到没有更多记录为止”。

在这个阶段,如何为查询添加别名并不重要:引擎处理页面、指针等物理对象,而不是别名等逻辑对象。

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.

红ご颜醉 2024-10-06 22:15:48

它们并不更快,但确实使代码更易于阅读。当您多次连接到一个表以及使用派生表时,它们是必需的。

我发现始终使用别名并对选择中的所有列进行别名是一个很好的做法,这样维护人员就不必在六个月后找出复杂查询中每个字段来自哪个表。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文