处理默认排序顺序的 SQL 最佳实践 [英] SQL best practice to deal with default sort order

查看:35
本文介绍了处理默认排序顺序的 SQL 最佳实践的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过很多 SQL 代码,似乎开发人员假设默认的排序顺序总是成立.例如,在构建 HTML 选择列表时,他们只需 SELECT id, name FROM table 而不会发出 ORDER BY 子句.

A lot of SQL code I've read, it seems like the developer assumes that the default sort order always holds. For example when building an HTML select list they would just SELECT id, name FROM table without issuing an ORDER BY clause.

根据我自己的经验,如果没有给出 ORDER BY 子句并且没有索引,dbms 似乎总是使用 FIFO 对数据进行排序.但是,不能保证顺序.但是我从未见过如果表没有变化,dbms 会重新排序数据.

From my own experience it seems like dbms alway orders data using FIFO if no ORDER BY clause is given and no index. However, the order is not guaranteed. But I have never seen a dbms reordering data if there no change to the table.

您是否遇到过 dbms 在没有更改表的情况下以不确定的顺序选择数据的情况?

Have you ever experienced a dbms selecting data in a non deterministic order if there is no change to the table?

始终放置 ORDER BY 子句是最佳实践吗?

Is it best practice to always put an ORDER BY clause?

推荐答案

没有默认的排序顺序.即使该表具有聚集索引,也不能保证按该顺序获得结果.如果您需要特定订单,则必须使用 order by 子句.

There is no default sort order. Even if the table has a clustered index, you are not guaranteed to get the results in that order. You must use an order by clause if you want a specific order.

这篇关于处理默认排序顺序的 SQL 最佳实践的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆