与B树索引相关的执行计划
admin
2023-05-07 03:01:15
0
  1. 索引唯一扫描,索引范围扫描,索引全扫描,索引快速全扫描和索引跳跃式扫描。

    索引唯一扫描:

SQL> create table employee(gender varchar2(1),employee_id number);
Table created.
SQL> insert into employee values('F',99);
1 row created.
SQL> insert into employee values('F',100);
1 row created.
SQL> insert into employee values('M',101);
1 row created.
 SQL> insert into employee values('M',102);
1 row created.
SQL> insert into employee values('M',103);
1 row created.
SQL> insert into employee values('M',104);
1 row created.
SQL> insert into employee values('M',105);
1 row created.
SQL> insert into employee values('F',106);
1 row created.
SQL> commit;
Commit complete.
SQL> create unique index idx_unqi_emp on employee(employee_id);
Index created.
SQL> select * from employee where employee_id=100;
G EMPLOYEE_ID
- -----------
F  100
SQL> set lines 200 pagesize 1000
SQL> select plan_table_output from table(dbms_xplan.display_cursor(null,null,'ALL'));
PLAN_TABLE_OUTPUT
---------------------------------------------------------------------------------
SQL_IDbum8qv24s6tqp, child number 0
-------------------------------------
select * from employee where employee_id=100
Plan hash value: 1037614268
--------------------------------------------------------------------------------------------
| Id  | Operation    | Name   | Rows  | Bytes | Cost (%CPU)| Time   |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |   |   |   | 1 (100)|   |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMPLOYEE   | 1 |15 | 1   (0)| 00:00:01 |
|*  2 |   INDEX UNIQUE SCAN    | IDX_UNQI_EMP | 1 |   | 0   (0)|   |
---------------------------------------------------------------------------------
31 rows selected.


索引范围扫描:

SQL> drop index idx_unqi_emp;
Index dropped.
SQL> create index idx_unqi_emp on employee(employee_id);
Index created.
SQL> select * from employee where employee_id=100;
G EMPLOYEE_ID
- -----------
F  100
SQL> select plan_table_output from table(dbms_xplan.display_cursor(null,null,'ALL'));
PLAN_TABLE_OUTPUT
-----------------------------------------------------------------------------------------------------------------------------------------
SQL_IDbum8qv24s6tqp, child number 0
select * from employee where employee_id=100
Plan hash value: 407794244
--------------------------------------------------------------------------------------------
| Id  | Operation    | Name   | Rows  | Bytes | Cost (%CPU)| Time   |
--------------------------------------------------------------------------------------------
|   0 | SELECT STATEMENT    |   |   |   | 2 (100)|   |
|   1 |  TABLE ACCESS BY INDEX ROWID| EMPLOYEE   | 1 |15 | 2   (0)| 00:00:01 |
|*  2 |   INDEX RANGE SCAN    | IDX_UNQI_EMP | 1 |   | 1   (0)| 00:00:01 |
--------------------------------------------------------------------------------------------

索引快速全扫描:

SQL> begin
  2  for i in 1..5000 loop
  3  insert into employee values('F',i);
  4  end loop;
  5  commit;
  6  end;
  7  /
PL/SQL procedure successfully completed.
SQL> begin
  2  for i in 5001..10000 loop
  3  insert into employee values('M',i);
  4  end loop;
  5  commit;
  6  end;
  7  /
PL/SQL procedure successfully completed.
SQL> select gender,count(*) from employee group by gender;
G   COUNT(*)
- ----------
M5000
F5000
BEGIN
  DBMS_STATS.GATHER_TABLE_STATS(ownname          => 'SCOTT',
                                tabname          => 'EMPLOYEE',
                                estimate_percent => 100,
                                method_opt       => 'for all columns size repeat',
                                no_invalidate    => FALSE,
                                degree           => 8,
                                granularity      => 'ALL',
                                cascade          => TRUE);
 END;
PL/SQL procedure successfully completed.
SQL> set autot trace
SQL> select employee_id from employee;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2119105728
------------------------------------------------------------------------------
| Id  | Operation  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     | 10000 | 40000 |   7   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| EMPLOYEE | 10000 | 40000 |   7   (0)| 00:00:01 |
------------------------------------------------------------------------------


提示走索引,无效,因为employee_id有null值:

SQL> create index idx_emp_1 on employee(employee_id);
Index created.
SQL> select /*+ index(employee idx_emp_1) */ employee_id from employee;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2119105728
------------------------------------------------------------------------------
| Id  | Operation  | Name     | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT  |     | 10000 | 40000 |   7   (0)| 00:00:01 |
|   1 |  TABLE ACCESS FULL| EMPLOYEE | 10000 | 40000 |   7   (0)| 00:00:01 |
------------------------------------------------------------------------------


建立组合索引,或许把employee_id限制为非空:

SQL> select /*+ index(employee idx_emp_1) */ employee_id from employee;
10000 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 438557521
------------------------------------------------------------------------------
| Id  | Operation | Name      | Rows  | Bytes | Cost (%CPU)| Time     |
------------------------------------------------------------------------------
|   0 | SELECT STATEMENT |     | 10000 | 40000 |  25   (0)| 00:00:01 |
|   1 |  INDEX FULL SCAN | IDX_EMP_1 | 10000 | 40000 |  25   (0)| 00:00:01 |
------------------------------------------------------------------------------


索引跳跃扫描:
SQL> create index idx_emp_1 on employee(gender,employee_id);
Index created.
SQL> select * from employee where employee_id=109;
Execution Plan----------------------------------------------------------Plan hash value: 2039022311
------------------------------------------------------------------------------| Id  | Operation  | Name      | Rows  | Bytes | Cost (%CPU)| Time     |------------------------------------------------------------------------------|   0 | SELECT STATEMENT |      |    1 |    6 |    3   (0)| 00:00:01 ||*  1 |  INDEX SKIP SCAN | IDX_EMP_1 |    1 |    6 |    3   (0)| 00:00:01 |------------------------------------------------------------------------------
Predicate Information (identified by operation id):---------------------------------------------------
   1 - access("EMPLOYEE_ID"=109)       filter("EMPLOYEE_ID"=109)
Statistics----------------------------------------------------------   1  recursive calls   0  db block gets   6  consistent gets   3  physical reads   0  redo size 600  bytes sent via SQL*Net to client 524  bytes received via SQL*Net from client   2  SQL*Net roundtrips to/from client   0  sorts (memory)   0  sorts (disk)   1  rows processed


相关内容

热门资讯

【珠城“健”闻】市三院引进非侵... 前沿科技赋能: 构建大脑与肢体的康复通路 技术优势对比: 与传统被动康复训练相比的显著突破 1.意...
从微信状态看社交边界 钟 颐 5月11日深夜,“微信状态 访客记录”话题冲上微博热搜,引发网友热议。微信方面表示,该功能仅...
中国科学家成功研制“九章四号”... 4月10日拍摄的“九章四号”量子计算原型机局部。 记者5月13日从中国科学技术大学获悉,该校潘建伟、...
江苏睿恩新能源申请正极极片及其... 国家知识产权局信息显示,江苏睿恩新能源科技有限公司申请一项名为“一种正极极片及其制备方法、锂离子电池...
流言|2026年地球会失重7秒... 流言:2026年8月12日地球将失重7秒、数千万人因此伤亡。 (图片由AI生成) 真相:“地球重...
从渠道赋能到行业基础设施 互联... 2025年,伴随数智技术的加速渗透,保险业的获客方式、决策逻辑乃至服务形态,都在被重新定义,互联网保...
人形机器人绕不开的坎:续航问题... 这两年,关于人形机器人的故事已经被讲了很多:AGI 的终极载体、万亿美元的劳动力替代、工厂和家庭的全...
合成生命重要突破 中国团队首次... 中新网北京5月13日电 (记者 孙自法)构建能够模拟天然细胞分裂行为的人工细胞,是合成生命研究至关重...
“九章四号”问世 中国科学家再... 4月10日拍摄的“九章四号”量子计算原型机局部。新华社记者 周牧 摄 新华社合肥5月13日电(记者陈...
索尼正面回应30%平台税:要养... 快科技5月13日消息,针对长期诟病的PlayStation商店30%抽成,索尼官方近日首度正面回应称...