表别名比表名更快吗?
表别名是否比仅使用表名更快?
或者这没有什么区别吗?
你们有一些使用表别名的好技巧吗?或者推荐/不推荐?
谢谢,
布鲁诺
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,他们不是。
查询引擎甚至看不到它们(它们只出现在计划的注释中)。
执行查询时,引擎根据查询计划处理实际的物理对象(表、索引等)。
当查询说这样的话: 时
,优化器会创建一个类似于这样的计划:
查询引擎解释为“找到索引的第一页
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:
, the optimizer creates a plan similar to this:
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.
它们并不更快,但确实使代码更易于阅读。当您多次连接到一个表以及使用派生表时,它们是必需的。
我发现始终使用别名并对选择中的所有列进行别名是一个很好的做法,这样维护人员就不必在六个月后找出复杂查询中每个字段来自哪个表。
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.