网站首页
JSP空间
动态资讯
开源项目
技术文档
资源下载
J2EE资源
客户论坛
在线支付
 
  技术文档>>数据库技术>>Oracle技术>>Oracle开发>查看文档  
  奇怪的sql:排序方法不同但结果却是一样的     
  文章作者:未知  文章来源:赛迪网技术社区  
  查看:83次  录入:管理员--2008-03-07  
 

错误现象:开发中发现一条sql出现问题,唯一的不同之处就是gmt_create的排序方法不同,但得到的结果却是一样的,下面是这句sql。

@>select rw ,id from
2 (select rownum rw, id from cmm_message t where t.topic_id=197 and
t.status=0 order by t.topic_id,t.status,t.gmt_create ) tt
3 where tt.id=485;

rw id
---------- ----------
11 485

@>
@>select rw ,id from
2 (select rownum rw, id from cmm_message t where t.topic_id=197 and
t.status=0 order by t.topic_id,t.status,t.gmt_create desc) tt
3 where tt.id=485;

rw id
---------- ----------
11 485

尝试着把中间的子查询单独拿出来运行。发现结果是正确的:

@>select rownum rw, id from cmm_message t where t.topic_id=197 and
t.status=0 order by t.topic_id,t.status,t.gmt_create desc ;

rw id
---------- ----------
1 485
2 484
3 483
4 482
5 481
6 480
7 444
8 418
9 416
10 320
11 275

11 rows selected.

@>select rownum rw, id from cmm_message t where t.topic_id=197 and
t.status=0 order by t.topic_id,t.status,t.gmt_create;

rw id
---------- ----------
1 275
2 320
3 416
4 418
5 444
6 480
7 481
8 482
9 483
10 484
11 485

我们可以发现这个结果很容易让人产生错觉,好像oracle是有问题的,子查询中的结果正确,但是整个语句是不正确的。

大家都知道rownum是在取数据的时候就确定了的,order by是最后才执行的。这个语句本身的写法就是错误的。那为什么子查询中产生了正确的结果,而整个语句是错误的呢?让我们再来看看执行计划。

1* select rownum rw, id,gmt_create from 

cmm_message t where t.topic_id=197 and t.status=0 order by 

t.topic_id,t.status,t.gmt_create @>/

rw id gmt_create
---------- ---------- -------------------
1 275 2005-09-05 13:09:24
2 320 2005-09-05 14:34:02
3 416 2005-09-08 11:18:22
4 418 2005-09-08 11:24:15
5 444 2005-09-08 16:25:05
6 480 2005-09-09 19:46:01
7 481 2005-09-09 19:50:36
8 482 2005-09-09 19:50:47
9 483 2005-09-09 19:50:54
10 484 2005-09-09 19:51:15
11 485 2005-09-09 19:51:23
12 488 2005-09-12 11:14:25
13 489 2005-09-12 11:15:00
14 490 2005-09-12 11:15:23
15 491 2005-09-12 11:15:41

15 rows selected.


execution plan
----------------------------------------------------------
0 select statement optimizer=choose (cost=2 card=3 bytes=45)
1 0 count
2 1 index (range scan) of 'cmm_message_tpid_st_cr_id_ind' (n
on-unique) (cost=2 card=3 bytes=45)

发现走了index扫描。

1* select rownum rw, id,gmt_create from 

cmm_message t where t.topic_id=197 and t.status=0 order by

t.topic_id,t.status,t.gmt_create desc @>/

rw id gmt_create
---------- ---------- -------------------
1 491 2005-09-12 11:15:41
2 490 2005-09-12 11:15:23
3 489 2005-09-12 11:15:00
4 488 2005-09-12 11:14:25
5 485 2005-09-09 19:51:23
6 484 2005-09-09 19:51:15
7 483 2005-09-09 19:50:54
8 482 2005-09-09 19:50:47
9 481 2005-09-09 19:50:36
10 480 2005-09-09 19:46:01
11 444 2005-09-08 16:25:05
12 418 2005-09-08 11:24:15
13 416 2005-09-08 11:18:22
14 320 2005-09-05 14:34:02
15 275 2005-09-05 13:09:24

15 rows selected.


execution plan
----------------------------------------------------------
0 select statement optimizer=choose (cost=2 card=3 bytes=45)
1 0 count
2 1 index (range scan descending) of 'cmm_message_tpid_st_cr
_id_ind' (non-unique) (cost=2 card=3 bytes=45)

我们可以发现走了index倒叙扫描,这样就印证了我们的结论。我们再看

select">admintools@deve>select rw ,id from

2 (select rownum rw, id from cmm_message t where t.topic_id=197 and

3 t.status=0 order by t.topic_id,t.status,t.gmt_create desc) tt
4 where tt.id=485;

rw id
---------- ----------
11 485


execution plan
----------------------------------------------------------
0 select statement optimizer=choose (cost=6 card=3 bytes=78)
1 0 view (cost=6 card=3 bytes=78)
2 1 sort (order by) (cost=6 card=3 bytes=45)
3 2 count
4 3 index (range scan) of 'cmm_message_tpid_st_cr_id_ind
' (non-unique) (cost=2 card=3 bytes=45)

当变成子查询后,走的是index正序扫描,然后再排序。这样我们就知道了为什么查询的结果总是一样的原因了。

接下来,为了进一步验证我们的观点,我在子查询中加入提示,让他走fts.结果如下:

1* select /*+full(t)*/ rownum rw, id,gmt_create from cmm_message t where
t.topic_id=197 and t.status=0 order by t.topic_id,t.status,t.gmt_create desc @>/

rw id gmt_create
---------- ---------- -------------------
15 491 2005-09-12 11:15:41
14 490 2005-09-12 11:15:23
13 489 2005-09-12 11:15:00
12 488 2005-09-12 11:14:25
11 485 2005-09-09 19:51:23
10 484 2005-09-09 19:51:15
9 483 2005-09-09 19:50:54
8 482 2005-09-09 19:50:47
7 481 2005-09-09 19:50:36
6 480 2005-09-09 19:46:01
5 444 2005-09-08 16:25:05
4 418 2005-09-08 11:24:15
3 416 2005-09-08 11:18:22
2 320 2005-09-05 14:34:02
1 275 2005-09-05 13:09:24

select /*+full(t)*/ rownum rw, id,gmt_create from cmm_message t where
2 t.topic_id=197 and t.status=0 order by t.topic_id,t.status,t.gmt_create;

rw id gmt_create
---------- ---------- -------------------
1 275 2005-09-05 13:09:24
2 320 2005-09-05 14:34:02
3 416 2005-09-08 11:18:22
4 418 2005-09-08 11:24:15
5 444 2005-09-08 16:25:05
6 480 2005-09-09 19:46:01
7 481 2005-09-09 19:50:36
8 482 2005-09-09 19:50:47
9 483 2005-09-09 19:50:54
10 484 2005-09-09 19:51:15
11 485 2005-09-09 19:51:23
12 488 2005-09-12 11:14:25
13 489 2005-09-12 11:15:00
14 490 2005-09-12 11:15:23
15 491 2005-09-12 11:15:41
16 513 2005-09-13 11:37:31

至此,大家可以发现485总是排在第11位,这样就验证了rownum是在order by之前就取得了。前面有一个查询是走index倒序扫描的,所以让我们产生了多余的错觉。

 
 
上一篇: 在suse中让其他用户也能运行oracle命令    下一篇: 对外连接的表加上条件后将会使外连接失效
  相关文档
oracle中系统process与session的关系 04-07
判断字段中是否含有中文字符的实例脚本 01-31
如何检测oracle的可用性和表空间容量 02-28
全面剖析Oracle数据库中的分区功能 04-11
在数据库日渐庞大时进行归档的解决思路 07-17
诊断全局错误时如何在系统级进行设置 05-16
基于已被证实的oracle高可用性技术maa 03-14
使用oracle功能特性提高应用执行效率 (1) 04-15
修改"oracle"数据库的进程数及会话数 02-01
通过db查询的两个数据库间scn会被同步 03-26
oracle数据库中管理表空间和数据文件 (1) 04-24
Oracle9i与SYBASE ASE12.5相比的几个不足 08-05
讲解v$datafile_header相关字段的使用 03-10
了解国外公司的Oracle DBA面试试题 05-13
解析:怎样在oracle 9i中正确的转换时区 11-15
详解Oracle分布式系统数据复制技术 04-23
Oracle-Decode()函数和CASE语句的比较 06-03
讲解如何用组来保证Oracle数据库的安全 06-03
实例讲解更改oracle数据库中的sys口令 07-30
讲解forall与bulk collect的使用方法 05-12
返回首页 | 关于我们 | J网章程 | JSP空间合租 | 客服中心 | 免责声明 | 常见问题 | 参观机房
本站主机空间代理至厦门市华众网络科技有限公司
《中华人民共和国增值电信业务经营许可证》
编号:闽B2-20050079
@2005-2008福建JSP技术网 版权所有 闽ICP备05000928号
厦门(总部):13616026886 福州:0591-87655121
邮箱:admin@fjjsp.com 站长QQ,点击这里给我发消息