使用 SQL 序数位置表示法的好处?
背景信息
序数位置表示法(AKA 序数)是基于 SELECT 子句中列列表中的列顺序(而不是列名称或列别名)的列简写。通常在 ORDER BY
子句中受支持,某些数据库(MySQL 3.23+、PostgreSQL 8.0+)也支持 GROUP BY
子句的语法。
这是使用序数的示例:
GROUP BY 1, 2
ORDER BY 1, 2
它不好用,因为它使查询变得脆弱 - 如果列顺序发生变化,则需要更新序数,否则查询将不会返回您想要的结果。如果这些位置的列包含在聚合中,那么在 GROUP BY 中使用时很可能会出现错误...
问题
我能想到的唯一好处是发送的数据更少如果您不使用存储过程或函数(无论如何对我来说这使得顺序使用毫无意义),则可以使用电线。我还缺少其他任何好处吗?
Background Information
Ordinal position notation, AKA ordinals, is column shorthand based on the column order in the list of columns in the SELECT
clause, instead of either the column name or column alias. Commonly supported in the ORDER BY
clause, some databases (MySQL 3.23+, PostgreSQL 8.0+) support the syntax for the GROUP BY
clause as well.
Here's an example of using Ordinals:
GROUP BY 1, 2
ORDER BY 1, 2
It's not good to use because it makes the query brittle - if the column order changes, the ordinals need to be updated or your query won't return what you thought it would. Very likely, you'd get an error when used in the GROUP BY
if the columns at those locations are wrapped within aggregates...
The Question
The only benefit I can think of is less data to send over the wire, if you aren't using stored procedures or functions (which make ordinal usage moot, to me anyways). Are there any other benefits I'm missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我会使用它:
没有任何好处。
无论如何,SQL Server 只支持 ORDER BY。在其他任何地方,它都是要计算的表达式。
I'd use it:
There is no upside.
SQL Server only supports in the ORDER BY anyway. Anywhere else it's an expression to be evaluated.
很多时候,当我查询包含很多列的表时(在临时区域中只是为了数据探索......我永远不会为 PROD 环境编写这样的代码)我会做这样的事情来获取我关心的字段紧密结合在一起:
如果我说
order by Col_1, Col_117 desc, Col_50
我的查询将会失败,因为由于“ * ”加倍,该语句不知道我打算按哪些列进行排序。不是很常见,但仍然是一个有用的功能。Often times when I'm querying a table with a lot of columns (in ad-hoc-land just for data exploration... I would never code like this for a PROD environment) I do something like this to get fields I care about close together:
If I said
order by Col_1, Col_117 desc, Col_50
my query would barf because the statement wouldn't know which columns I meant to order by due to the " * " doubling up. Not very common, but still a useful feature.对我来说,两个用例是:
ORDER BY
子句重新输入CASE
语句,而是使用序数来保留它 干。有很多方法可以解决这个问题,例如使用 CTE、子查询或视图,但我经常发现序数是最简单的解决方案。The two use cases for me are:
CASE
statement; rather than retyping theCASE
statement for theORDER BY
clause, I use the ordinal which keeps it DRY. There are ways around this, e.g., using CTEs, subqueries, or view, but I often find the ordinal is the simplest solution.我现在倾向于使用内联视图:
但在它们成为有效语法之前的日子里,它确实有助于重新输入列的全文。使用棘手的函数,您可能会在 SELECT 和 ORDER BY 中的值之间出现差异,例如
SELECT 中的“0”意味着您没有按您选择的内容进行排序。
I tend to use in-line views now:
But in the days before they were valid syntax, it did help retyping the full text of the column. With tricky functions you had the potential for discrepancies between the value in the SELECT and ORDER BY such as
The '0' in the SELECT means that you are not ordering by what you select.
我有一个查询生成算法 - SQL 是自动生成的。使用序数意味着我可以引用生成的字段,而无需再次获取字段名称。用户可以通过从屏幕上的列表中选择来引用表中的字段名称。只要我使列表与 sql 相对应,如果 SELECT 项也是有序的,我就永远不需要知道字段名称。
据记忆,这曾经是 1970 年代末的 SQL 标准
I have a query generating algorithm - the SQL is auto generated. Using the ordinal means that I can refer to the generated field without having to fetch the field name again. The user can refer to the field name in a table by selecting it from a list on the screen. As long as I make the list correspond with the sql, I would never need to know field names, if the SELECT items were ordinal, too.
Memory says this used to be in the SQL standard in the late 1970's
如果我没记错的话,Microsoft 在 SQL Server 的未来版本中将不再使用您所描述的序数。我可能是错的,但我认为情况就是这样。我一直喜欢在某些情况下使用它们,因为当您处理包含较长查询的派生列时,它需要更少的输入。
If I recall correctly, the use of ordinals like you describe is being deprecated by Microsoft in a future release of SQL Server. I could be wrong on this, but I think that's the case. I've always liked using them in certain cases because it involves less typing when you're dealing with derived columns that contain a longish query.