Liny_@NotePad

沉迷ACG中

Oracle居然没有默认按主键排序。。

YOYO posted @ 2010年5月03日 09:03 in 【数据库】 with tags oracle , 9574 阅读

囧死了。。按照oracle官方的说法有:

Oracle assigns a ROWNUM value to each row as it is retrieved, before rows are sorted for an ORDER BY clause, so an ORDER BY clause normally does not affect the ROWNUM of each row. However, if an ORDER BY clause causes Oracle to use an index to access the data, Oracle may retrieve the rows in a different order than without the index, so the ROWNUMs may be different than they would be without the ORDER BY clause.

好无语。。巨讨厌。。最后为每个实体类加了一个返回排序的方法,在泛型DAO里面用。无比郁闷。ORACLE真爱搞特殊化,存储过程已经能灭杀人了连排序都…… TAT。

躺着读书 说:
2010年8月07日 20:11

呵呵,你知道增加一个order by 对数据库会有多少开销么?

oracle的数据库实现就一个原则,怎么快怎么效率高就怎么来。大多数情况下不需要排序还非得按主键排序这不是浪费资源么?

这和oracle的表结构是有关系的,因为oracle的表结构默认是按堆存放的。按堆存放的意思就是,随便存,存的时候就是乱序的。如果你建表的时候就是建的按索引组织的表,那么它返回的时候就会默认排序了。

Head_small
YOYO 说:
2010年8月08日 03:13

= =|| 知道是知道呀 但是这样开发来好郁闷 还需要自己排序。。

躺着读书 说:
2010年8月11日 23:28

呵呵,oracle就是这个规矩。习惯就好。

他有一个原则。这些不加说明的叫做默认行为,而默认行为是可以改动的。

比如说:oracle10g返回的结果是默认排序的,于是你不加order by写了大量的代码。但是有一天数据库升级到了10g release1 你就发现你的程序完全不能运行,错误莫名奇妙。

哦,原来是oracle改掉了这个默认行为,默认不排序了。

只是举例。实际上oracle目前为止都不会自动按主键排序。但是自动排序的sql server 或者mysql 有一天改动了这个默认行为。你是不是要全部重写你的sql呢?

oracle文档化的东西它就不会改动,并且会一直支持。而不是oracle文档化的东西,随时都可能被改掉。说不定一个升级包就改掉了。

不过不按主键自动排序是有性能方面的原因。原因就是,排序是有性能开销的。你可以查看一下不写order by和写了order by语句的执行计划,加了order by 肯定会增加很多的IO和cpu的开销。而你没有必要排序的时候排序了,是不是在浪费性能呢?呵呵。

这就是oracle的另外一个原则:凡是影响数据库速度而不一定会带来好处的东西,oracle一概默认不支持。

Head_small
YOYO 说:
2010年8月12日 06:55

嗯嗯~
不过一般大家都会向下兼容吧.. > < 要是不能的话太郁闷了..

- ◇- 的确为了性能应该默认不排序, 如果有个选项来设定就好了..
懒人的我先祈祷ORM框架对oracle有更好的支持吧.. > /// <

繁体字 说:
2012年5月28日 19:58

最近,我才发现这个问题, 我一直以为, 和m$sql 一样吧,都按主键排序~

Bihar Board 10th Eng 说:
2022年9月25日 14:50

Board of Secondary Education Bihar, Patna board is conducted the state class 10th examination tests as 2nd Indian language for all Hindi, Urdu and Bengali medium students, and the students who have continued their study’s at English medium students have Hindi is the second language and their first language is English, Bihar Board 10th English Question Paper all students can download BSEB 10th Class English Model Set 2023 Pdf with answer solutions to practice chapter wise important questions.Board of Secondary Education Bihar, Patna board is conducted the state class 10th examination tests as 2nd Indian language for all Hindi, Urdu and Bengali medium students, and the students who have continued their study’s at English medium students have Hindi is the second language and their first language is English.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter