看到ASK TOM的一篇文章,挺有感触的。

http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:32812348052

主要问的是ROWNUM的问题。后面的一个讨论提问谈到:

select ename, sal
from emp
where rownum<=10
order by sal desc;

select ename, sal
from ( select ename, sal
from emp
order by sal desc)
where rownum<=10;

是否相同?

第一个SQL是先找到ROWNUM<10的记录,然后排序。

第二个SQL是先ORDER BY排序,再找ROWNUM<10的记录。

因此两种查询得到的答案不同,当然有时也会碰巧相同。

另外,如果表有索引,那么对于第二个SQL,可以从后面的记录开始读,避免排序。对于这个问题我做了实验:

create table t as select * from dba_objects;

create table t2 as select * from dba_objects;

create index t2_i on t2(object_id);

SQL> select * from (select owner, object_name, object_id from t order by object_id desc) where rownum<10;
9 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 3299198703
----------------------------------------------------------------------------------------
| Id  | Operation               | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |      |     9 |   864 |       |  1189   (1)| 00:00:15 |
|*  1 |  COUNT STOPKEY          |      |       |       |       |            |       |
|   2 |   VIEW                  |      | 47308 |  4435K|       |  1189   (1)| 00:00:15 |
|*  3 |    SORT ORDER BY STOPKEY|      | 47308 |  4435K|     9M|  1189   (1)| 00:00:15 |
|   4 |     TABLE ACCESS FULL   | T    | 47308 |  4435K|       |   150   (1)| 00:00:02 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(ROWNUM<10)
   3 - filter(ROWNUM<10)

Note
-----
   - dynamic sampling used for this statement

Statistics
----------------------------------------------------------
          7  recursive calls
          0  db block gets
        793  consistent gets
          0  physical reads
          0  redo size
        878  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          9  rows processed

SQL> select * from ( select owner, object_name, object_id from t2 order by object_id desc) where rownum < 10;
9 rows selected.

Execution Plan
----------------------------------------------------------
Plan hash value: 98068844
----------------------------------------------------------------------------------------
| Id  | Operation               | Name | Rows  | Bytes |TempSpc| Cost (%CPU)| Time     |
----------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT        |      |     9 |   864 |       |  1164   (1)| 00:00:14 |
|*  1 |  COUNT STOPKEY          |      |       |       |       |            |       |
|   2 |   VIEW                  |      | 46110 |  4322K|       |  1164   (1)| 00
:00:14 |
|*  3 |    SORT ORDER BY STOPKEY|      | 46110 |  4322K|  9848K|  1164   (1)| 00:00:14 |
|   4 |     TABLE ACCESS FULL   | T2   | 46110 |  4322K|       |   150   (1)| 00:00:02 |
----------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------
   1 - filter(ROWNUM<10)
   3 - filter(ROWNUM<10)

Note
-----
   - dynamic sampling used for this statement

Statistics
----------------------------------------------------------
          7  recursive calls
          0  db block gets
        791  consistent gets
          0  physical reads
          0  redo size
        878  bytes sent via SQL*Net to client
        492  bytes received via SQL*Net from client
          2  SQL*Net roundtrips to/from client
          1  sorts (memory)
          0  sorts (disk)
          9  rows processed

第二个SQL仅比第一个SQL少2个consistent gets,不像讨论中说的会明显的变化。这个讨论是2001年的,不知道是不是版本的问题?我用的是10g。

还请高手指点!

【Oracle】-【ROWNUM与索引】-索引对ROWNUM检索的影响的更多相关文章

  1. Oracle 课程四之索引

    课程目标 完成本课程的学习后,您应该能够: 理解b*tree索引的结构与特征 了解聚簇因子的产生原因 理解分区索引与全局索引的区别及场景 掌握组合索引的高效设计 位图索引的适用场景 全文索引的适用场景 ...

  2. 类型:Oracle;问题:oracle 查询表详细信息;结果:oracle查询表信息(索引,外键,列等)

    oracle查询表信息(索引,外键,列等) oracle中查询表的信息,包括表名,字段名,字段类型,主键,外键唯一性约束信息,索引信息查询SQL如下,希望对大家有所帮助: 1.查询出所有的用户表sel ...

  3. Oracle【序列、索引、视图、分页】

    1.Oracle序列语法:create sequence 序列名 特点1:默认是无值,指针指向没有值的位置 特点2:序列名.nextval 每次执行值会自增一次,步长为 1 特点3:序列名.currv ...

  4. Oracle的几种索引

    Oracle 提供了多种不同类型的索引以供使用.简单地说, Oracle 中包括如下索引: 1. B* 树索引 这些是我所说的 “ 传统 “ 索引.到目前为止,这是 Oracle 和大多数其他数据库中 ...

  5. 【Oracle学习笔记】索引

    1 简介 1)索引是数据库对象之一,用于加快数据的检索,类似于书籍的索引.在数据库中索引可以减少数据库程序查询结果时需要读取的数据量,类似于在书籍中我们利用索引可以不用翻阅整本书即可找到想要的信息. ...

  6. sql(Oracle)优化之索引

    原文:https://www.cnblogs.com/oraclestudy/articles/5779210.html 建立索引的目的是:l 提高对表的查询速度:l 对表有关列的取值进行检查. 注意 ...

  7. Oracle对象-视图和索引

    Oracle 对象-视图 视图概念 ​ 视图就是提供一个查询的窗口,所有的数据来自于原表 创建视图[必须有dba权限] --查询语句创建表 create table emp as select * f ...

  8. Oracle外键不加索引会引起死锁问题

    转载链接:http://www.jb51.net/article/50161.htm 这篇文章主要介绍了Oracle外键不加索引引起死锁的情况及解决,需要的朋友可以参考下 --创建一个表,此表作为子表 ...

  9. 深入学习Oracle分区表及分区索引

    关于分区表和分区索引(About Partitioned Tables and Indexes)对于10gR2而言,基本上可以分成几类: •       Range(范围)分区 •       Has ...

  10. oracle 分区表和分区索引

    很复杂的样子,自己都没有看完,以备后用 http://hi.baidu.com/jsshm/item/cbfed8491d3863ee1e19bc3e ORACLE分区表.分区索引ORACLE对于分区 ...

随机推荐

  1. 他的第一个NDK的Demo

    DEMO下载链接: http://download.csdn.net/detail/logicsboy/7535409 首先给你们恶补下啥是NDK:(我从百度Copy的) NDK全称:Native D ...

  2. Claris and XOR

    Problem Description Claris loves bitwise operations very much, especially XOR, because it has many b ...

  3. iOS开发- 拨打电话总结

    关于iOS应用拨打电话, 我所知道的有3种办法, 详细例如以下: 一.利用openURL(tel) 特点: 直接拨打, 不弹出提示. 而且, 拨打完以后, 留在通讯录中, 不返回到原来的应用. //拨 ...

  4. Eddy's mistakes(字母大小写转换)strlwr函数的应用

    Problem Description Eddy usually writes  articles ,but he likes mixing the English letter uses, for ...

  5. 绘制一个绿色矩形平面((50, 50)->(350, 350))

    //VS2008+opencv2.4 //绘制一个绿色矩形平面 #include "stdafx.h" #include "highgui.h" #includ ...

  6. 采用tcpdump攫Android网络数据包

    1 空灵的原理 tcpdump(需Root用户执行)拦截和显示发送或收到过网络连接到该机器的TCP/IP和其它数据包.简单说就监控手机进出网络数据. 2 方法优劣 2.1长处 1.手机数据包无遗漏 2 ...

  7. Advance Installer安装问题

    一,在Advance Installer中注冊dll 1,首先将文件加入到Files And Folders中.此处以InstallValidate.dll为例. 2,在Custom Action处进 ...

  8. hdu 3709 数字dp(小思)

    http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negati ...

  9. 【UVA】580-Critical Mass

    依据递推公式计算,须要打表不然可能会超时. #include<cstdio> #include<cstring> #include<iostream> #inclu ...

  10. java23种设计模式详解(转)

    设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...