0

免责声明:我知道在需要排序数据时不使用 SQL 中的“ORDER BY”是不好的。

我目前正在支持一个存在奇怪问题的 Pro*C 程序。奇怪问题的可能原因之一可能是原始开发人员(很久以前)没有在他们的 SQL 中使用 ORDER BY,即使程序逻辑依赖于它!这些年来,该程序一直运行良好,直到最近才开始出现问题。

我们正试图将奇怪的问题归结为 ORDER BY 错误(还有其他原因,例如最近发生的从 Solaris 到 Linux 的移植)。

我们应该查看数据库端的哪些阴暗事物可能改变了旧的排序顺序?诸如数据文件之类的东西?有人对 Solaris 上的 Pro*C 神奇地对结果集进行排序有任何经验吗?

谢谢!

4

2 回答 2

2

Since you know that the program cares about the order in which results are returned and you know that the query that is submitted is missing an ORDER BY clause, is there a reason that you don't just fix the problem rather than looking to try to figure out whether the actual order of results may have changed? If you fix the known ORDER BY problem and the "weird problem" you have disappears, that would provide some pretty good evidence that the "weird problem" is, in fact, caused by the missing ORDER BY.

Unfortunately, there are lots of things that might have caused the order of results to change many of which may be impossible to track down. The most obvious cause would be a change in the execution plan. That, in turn, may have been caused either because statistics changed or because statistics didn't change enough or because of a patch or because of an initialization parameter change or because of a client configuration change among other things. If you are licensed to use the AWR (Automatic Workload Repository), you might be able to find evidence that the plan has changed by looking to see if there are multiple PLAN_HASH_VALUE values for the SQL_ID in DBA_HIST_SQLSTAT over different days. If there are, you'd still have to try to figure out whether the different plans actually caused the results to be returned in a different order. Beyond a query plan change, though, there are dozens of other possible causes. The physical order of data on disk may have changed because someone reorganized the table or because someone moved data files around on the disk or because the SAN automatically rebalanced something by moving data around. Some data may have been cached (or may not have been cached) in general in the past that is now cached. An Oracle patch may have been applied.

于 2013-03-13T08:27:24.743 回答
0

我建议使用视图更改您的物理表并在该视图中进行所需的顺序。

例子

TABLE_NOT_SORTED --> 重命名为 --> PHYS_TABLE_NOT_SORTED

CREATE VIEW TABLE_NOT_SORTED 
AS
SELECT * FROM PHYS_TABLE_NOT_SORTED
ORDER BY DESIRED_COLUMNS

回复评论:

根据this question和Ask Tom's Answer,如果您不使用“ORDER BY”,Oracle不保证默认排序,他们可以自由更改它。他们当然是绝对正确的。如果您需要排序,请使用 Order By。

除此之外,我们不能说您的代码或默认排序。

于 2013-03-13T08:11:46.393 回答