网页资讯视频图片知道文库贴吧地图采购
进入贴吧全吧搜索

 
 
 
日一二三四五六
       
       
       
       
       
       

签到排名:今日本吧第个签到,

本吧因你更精彩,明天继续来努力!

本吧签到人数:0

一键签到
成为超级会员,使用一键签到
一键签到
本月漏签0次!
0
成为超级会员,赠送8张补签卡
如何使用?
点击日历上漏签日期,即可进行补签。
连续签到:天  累计签到:天
0
超级会员单次开通12个月以上,赠送连续签到卡3张
使用连续签到卡
08月30日漏签0天
oracle吧 关注:61,989贴子:133,524
  • 看贴

  • 图片

  • 吧主推荐

  • 视频

  • 游戏

  • 3回复贴,共1页
<<返回oracle吧
>0< 加载中...

ORDER BY是否走索引情况分析

  • 只看楼主
  • 收藏

  • 回复
  • 黑夜使者
  • 副总监
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
1.ORDER BY 索引列
走索引
示例:
12:15:49 SCOTT@orcl> select * from emp order by empno;
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 4004826672
--------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |--------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 560 | 2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 560 | 2 (0)| 00:00:01 || 2 | INDEX FULL SCAN | EMP_PK | 14 | | 1 (0)| 00:00:01 |
--------------------------------------------------------------------------------------
显然走了主键索引EMP_PK.
2.order by 索引列a,索引列b。
不走索引
示例:
--查看索引
12:18:48 SCOTT@orcl> select INDEX_NAME,COLUMN_NAME from user_ind_columns where table_name='EMP';
INDEX_NAME COLUMN_NAME
---------------------------------------------------------------------------------
EMP_PK EMPNO
TEST_INX ENAME
已选择2行。
--
12:16:50 SCOTT@orcl> select * from emp order by empno,ename;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 150391907
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
|---------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 14 | 560 | 719 (1)| 00:00:09 || 1 | SORT ORDER BY | | 14 | 560 | 719 (1)| 00:00:09 || 2 | TABLE ACCESS FULL| EMP | 14 | 560 | 718 (1)| 00:00:09
|---------------------------------------------------------------------------
3.order by 复合索引.
走复合索引(注意ORDER BY的顺序与复合索引要一致)
新增复合索引.
12:23:37 SCOTT@orcl> select INDEX_NAME,COLUMN_NAME from user_ind_columns where table_name='EMP';
INDEX_NAME COLUMN_NAME
------------------------------ ---------------
EMP_PK EMPNO
TEST_INX ENAME
CMD_IND ENAME
CMD_IND EMPNO
已选择4行。
已用时间: 00: 00: 00.01
--查看执行计划
12:10:24 SCOTT@orcl> select * from emp order by empno,ename;
已选择14行。
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 1060919621
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 560 | 2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 560 | 2 (0)| 00:00:01 || 2 | INDEX FULL SCAN | COM_IND | 14 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------I


  • 黑夜使者
  • 副总监
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
哎,这百度贴吧的格式真是让人郁闷啊。。。


2025-08-30 05:11:26
广告
不感兴趣
开通SVIP免广告
  • 黑夜使者
  • 副总监
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
关于DESC的...
17:51:41 SCOTT@orcl> select * from emp order by empno desc;
已用时间: 00: 00: 00.01
执行计划
----------------------------------------------------------
Plan hash value: 1024267123
---------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|
---------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 448 | 2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 448 | 2 (0)| 00:00:01 || 2 | INDEX FULL SCAN DESCENDING| CMD_IND | 14 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------
17:54:01 SCOTT@orcl> select * from emp order by empno desc,ename desc;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 1024267123
---------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time
---------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 14 | 448 | 2 (0)| 00:00:01 || 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 448 | 2 (0)| 00:00:01 || 2 | INDEX FULL SCAN DESCENDING| CMD_IND | 14 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------
--此时不走索引
17:54:09 SCOTT@orcl> select * from emp order by empno desc,ename;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 150391907
---------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 14 | 448 | 719 (1)| 00:00:09 |
| 1 | SORT ORDER BY | | 14 | 448 | 719 (1)| 00:00:09 |
| 2 | TABLE ACCESS FULL| EMP | 14 | 448 | 718 (1)| 00:00:09 |
---------------------------------------------------------------------------I


  • 黑夜使者
  • 副总监
    11
该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
使用序号是和使用字段名一样的
18:01:42 SCOTT@orcl> select * from emp order by 1;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 1186483535
---------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|--------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 14 | 448 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 448 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | CMD_IND | 14 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------
18:02:06 SCOTT@orcl> select * from emp order by 1,2;
已用时间: 00: 00: 00.00
执行计划
----------------------------------------------------------
Plan hash value: 1186483535
---------------------------------------------------------------------------------| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time|---------------------------------------------------------------------------------| 0 | SELECT STATEMENT | | 14 | 448 | 2 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID| EMP | 14 | 448 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | CMD_IND | 14 | | 1 (0)| 00:00:01 |
---------------------------------------------------------------------------------I


登录百度账号

扫二维码下载贴吧客户端

下载贴吧APP
看高清直播、视频!
  • 贴吧页面意见反馈
  • 违规贴吧举报反馈通道
  • 贴吧违规信息处理公示
  • 3回复贴,共1页
<<返回oracle吧
分享到:
©2025 Baidu贴吧协议|隐私政策|吧主制度|意见反馈|网络谣言警示