疑问? Oracle普通的B树索引是升序还是降序?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了疑问? Oracle普通的B树索引是升序还是降序?相关的知识,希望对你有一定的参考价值。

疑问? Oracle普通的B树索引是升序还是降序?

一、测试创建对象

SQL> create table a as select * from dba_objects;

Table created.

SQL> create index a_id on a(object_id);

Index created.

SQL> set autotrace on 

二、测试索引,观察执行计划

SQL> select object_name from a where object_id in(7788,7799) order by object_id desc;

OBJECT_NAME
--------------------------------------------------------------------------------------------------------------------------------
V$DIAG_DDE_USR_INC_ACT_MAP
V_$DIAG_DDE_USER_ACTION_DEF


Execution Plan
----------------------------------------------------------
Plan hash value: 2600461980

--------------------------------------------------------------------------------------
| Id  | Operation                     | Name | Rows  | Bytes | Cost (%CPU)| Time     |
--------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT              |      |    14 |  1106 |     9   (0)| 00:00:01 |
|   1 |  INLIST ITERATOR              |      |       |       |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID | A    |    14 |  1106 |     9   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN DESCENDING| A_ID |   299 |       |     3   (0)| 00:00:01 |
--------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("OBJECT_ID"=7788 OR "OBJECT_ID"=7799)

Note
-----
   - dynamic sampling used for this statement (level=2)

SQL> select object_name from a where object_id in(7788,7799) order by object_id asc;

OBJECT_NAME
--------------------------------------------------------------------------------------------------------------------------------
V_$DIAG_DDE_USER_ACTION_DEF
V$DIAG_DDE_USR_INC_ACT_MAP


Execution Plan
----------------------------------------------------------
Plan hash value: 2908601139

-------------------------------------------------------------------------------------
| Id  | Operation                    | Name | Rows  | Bytes | Cost (%CPU)| Time     |
-------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT             |      |    14 |  1106 |     9   (0)| 00:00:01 |
|   1 |  INLIST ITERATOR             |      |       |       |            |          |
|   2 |   TABLE ACCESS BY INDEX ROWID| A    |    14 |  1106 |     9   (0)| 00:00:01 |
|*  3 |    INDEX RANGE SCAN          | A_ID |   299 |       |     3   (0)| 00:00:01 |
-------------------------------------------------------------------------------------

Predicate Information (identified by operation id):
---------------------------------------------------

   3 - access("OBJECT_ID"=7788 OR "OBJECT_ID"=7799)

Note
-----
   - dynamic sampling used for this statement (level=2)

可以发现,使用ASC时,执行计划无改变

使用DESC 时,执行计划存在

INDEX RANGE SCAN DESCENDING 索引范围降序扫描,因此本次实验证明,当默认排序方式创建索引时,索引数据使用升序排列。

那么问题来了,知道这个东西有啥用呢?
创建复合索引,并且业务SQL存在order by 的方式时!!!注意理解,或者SQL order by desc/asc 则创建复合索引列跟着 desc/asc也没毛病。

以上是关于疑问? Oracle普通的B树索引是升序还是降序?的主要内容,如果未能解决你的问题,请参考以下文章

hive order by是升序还是降序

SQL Server 索引 - 升序或降序,有啥区别?

SQL语言中的升序,降序,是怎么会事

python:argsort,将数组升序或降序,将矩阵每一行升序或降序,返回其索引

你可能不知道SQL Server索引列的升序和降序带来的性能问题

字符串String 升序和降序 Vue检测数据的原理