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

查看:168
本文介绍了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天全站免登陆