使用 SQL 序数位置表示法的好处?

发布于 2024-08-21 01:31:58 字数 442 浏览 9 评论 0原文

背景信息

序数位​​置表示法(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 技术交流群。

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

发布评论

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

评论(6

極樂鬼 2024-08-28 01:31:58

我会使用它:

  • 如果您喜欢排除故障
  • 创建没有智能感知的即席查询

没有任何好处。

无论如何,SQL Server 只支持 ORDER BY。在其他任何地方,它都是要计算的表达式。

I'd use it:

  • If you love troubleshooting
  • Creating adhoc queries without intellisense

There is no upside.

SQL Server only supports in the ORDER BY anyway. Anywhere else it's an expression to be evaluated.

温暖的光 2024-08-28 01:31:58

很多时候,当我查询包含很多列的表时(在临时区域中只是为了数据探索......我永远不会为 PROD 环境编写这样的代码)我会做这样的事情来获取我关心的字段紧密结合在一起:

select top 1000
  Col_1, Col_18, Col_50, Col_117, *
from
  TableWithTonsOfCols
order by
  1, 4 desc, 3

如果我说 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:

select top 1000
  Col_1, Col_18, Col_50, Col_117, *
from
  TableWithTonsOfCols
order by
  1, 4 desc, 3

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.

岁月无声 2024-08-28 01:31:58

对我来说,两个用例是:

  • 我很着急,不想打字,所以我使用序数。我总是会将其转换为列名称,以供任何非临时使用,
  • 我排序所依据的列是一个冗长的 CASE 语句;我没有为 ORDER BY 子句重新输入 CASE 语句,而是使用序数来保留它 。有很多方法可以解决这个问题,例如使用 CTE、子查询或视图,但我经常发现序数是最简单的解决方案。

The two use cases for me are:

  • I am in a hurry and don't want to type, so I use the ordinal. I would always convert this to the column name for any non-temporary use
  • the column I am ordering by is a lengthy CASE statement; rather than retyping the CASE statement for the ORDER 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.
夜声 2024-08-28 01:31:58

我现在倾向于使用内联视图:

select col_a, count(*) from
  (select case ...... end col_a from ...)
group by col_a
order by col_a;

但在它们成为有效语法之前的日子里,它确实有助于重新输入列的全文。使用棘手的函数,您可能会在 SELECT 和 ORDER BY 中的值之间出现差异,例如

select ltrim(col_name,'0123456789')
from table
order by ltrim(col_name,'123456789')

SELECT 中的“0”意味着您没有按您选择的内容进行排序。

I tend to use in-line views now:

select col_a, count(*) from
  (select case ...... end col_a from ...)
group by col_a
order by col_a;

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

select ltrim(col_name,'0123456789')
from table
order by ltrim(col_name,'123456789')

The '0' in the SELECT means that you are not ordering by what you select.

流星番茄 2024-08-28 01:31:58

我有一个查询生成算法 - 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

行至春深 2024-08-28 01:31:58

如果我没记错的话,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.

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