SQL Tuning 基础概述10
在《SQL Tuning 基础概述05 - Oracle 索引类型及介绍》的1.5小节,提到了几种"索引的常见执行计划":
INDEX FULL SCAN:索引的全扫描,单块读,有序
INDEX RANGE SCAN:索引的范围扫描
INDEX FAST FULL SCAN:索引的快速全扫描,多块读,无序
INDEX FULL SCAN(MIN/MAX):针对MAX(),MIN()函数的查询
INDEX SKIP SCAN:查询条件没有用到组合索引的第一列,而组合索引的第一列重复度较高时,可能用到
本文用简单的测试案例,体会下索引使用这些执行计划的场景:
1.准备测试环境
创建测试表和索引:
conn jingyu/jingyu
drop table test_objects;
create table test_objects as select * from all_objects;
create index idx_test_objects_1 on test_objects(owner, object_name, subobject_name);
create index idx_test_objects_2 on test_objects(object_id);
查看测试表结构:
SQL> desc test_objects;
Name Null? Type
----------------------------------------------------------------- -------- --------------------------------------------
OWNER NOT NULL VARCHAR2(30)
OBJECT_NAME NOT NULL VARCHAR2(30)
SUBOBJECT_NAME VARCHAR2(30)
OBJECT_ID NOT NULL NUMBER
DATA_OBJECT_ID NUMBER
OBJECT_TYPE VARCHAR2(19)
CREATED NOT NULL DATE
LAST_DDL_TIME NOT NULL DATE
TIMESTAMP VARCHAR2(19)
STATUS VARCHAR2(7)
TEMPORARY VARCHAR2(1)
GENERATED VARCHAR2(1)
SECONDARY VARCHAR2(1)
NAMESPACE NOT NULL NUMBER
EDITION_NAME VARCHAR2(30)
查看测试表上的索引信息:
SQL> select index_name, column_name, column_position from user_ind_columns where table_name = 'TEST_OBJECTS';
INDEX_NAME COLUMN_NAME COLUMN_POSITION
------------------------------ ---------------------------------------- ---------------
IDX_TEST_OBJECTS_1 OWNER 1
IDX_TEST_OBJECTS_1 OBJECT_NAME 2
IDX_TEST_OBJECTS_1 SUBOBJECT_NAME 3
IDX_TEST_OBJECTS_2 OBJECT_ID 1
分析表并清空测试环境的shared_pool和buffer_cache:
analyze table test_objects compute statistics;
alter system flush shared_pool;
alter system flush buffer_cache;
2.编写SQL语句
根据不同执行计划的场景,编写SQL语句:
--INDEX RANGE SCAN(索引的范围扫描)
SELECT owner, object_name FROM test_objects WHERE owner = 'SYS' AND object_name = 'DBMS_OUTPUT';
--INDEX SKIP SCAN(针对MAX(),MIN()函数的查询)
SELECT owner, object_name FROM test_objects WHERE object_name = 'DBMS_OUTPUT';
--INDEX FAST FULL SCAN(索引的快速全扫描,多块读,无序)
SELECT owner, object_name FROM test_objects;
--INDEX FULL SCAN(索引的全扫描,单块读,有序)
SELECT owner, object_name FROM test_objects order by 1, 2;
--INDEX FULL SCAN (MIN/MAX)(针对MAX(),MIN()函数的查询)
SELECT max(object_id) FROM test_objects;
3.实验环境验证
根据2中的SQL分别在实验环境中验证,没有问题,结果如下:
SQL> set autot trace
--1. INDEX RANGE SCAN
SQL> SELECT owner, object_name FROM test_objects WHERE owner = 'SYS' AND object_name = 'DBMS_OUTPUT';
Execution Plan
----------------------------------------------------------
Plan hash value: 3492129186
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 29 | 3 (0)| 00:00:01 |
|* 1 | INDEX RANGE SCAN| IDX_TEST_OBJECTS_1 | 1 | 29 | 3 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("OWNER"='SYS' AND "OBJECT_NAME"='DBMS_OUTPUT')
Statistics
----------------------------------------------------------
59 recursive calls
0 db block gets
104 consistent gets
17 physical reads
0 redo size
676 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
6 sorts (memory)
0 sorts (disk)
2 rows processed
--2. INDEX SKIP SCAN
SQL> SELECT owner, object_name FROM test_objects WHERE object_name = 'DBMS_OUTPUT';
Execution Plan
----------------------------------------------------------
Plan hash value: 1228438998
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 58 | 27 (0)| 00:00:01 |
|* 1 | INDEX SKIP SCAN | IDX_TEST_OBJECTS_1 | 2 | 58 | 27 (0)| 00:00:01 |
---------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("OBJECT_NAME"='DBMS_OUTPUT')
filter("OBJECT_NAME"='DBMS_OUTPUT')
Statistics
----------------------------------------------------------
2 recursive calls
0 db block gets
32 consistent gets
23 physical reads
0 redo size
684 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
3 rows processed
--3. INDEX FAST FULL SCAN
SQL> SELECT owner, object_name FROM test_objects;
84311 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2324984732
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 84311 | 387K| 138 (0)| 00:00:02 |
| 1 | INDEX FAST FULL SCAN| IDX_TEST_OBJECTS_1 | 84311 | 387K| 138 (0)| 00:00:02 |
-------------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
6097 consistent gets
480 physical reads
0 redo size
3509341 bytes sent via SQL*Net to client
62339 bytes received via SQL*Net from client
5622 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
84311 rows processed
--4. INDEX FULL SCAN
SQL> SELECT owner, object_name FROM test_objects order by 1, 2;
84311 rows selected.
Execution Plan
----------------------------------------------------------
Plan hash value: 2751381935
---------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 84311 | 387K| 505 (1)| 00:00:07 |
| 1 | INDEX FULL SCAN | IDX_TEST_OBJECTS_1 | 84311 | 387K| 505 (1)| 00:00:07 |
---------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
6090 consistent gets
0 physical reads
0 redo size
3509341 bytes sent via SQL*Net to client
62339 bytes received via SQL*Net from client
5622 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
84311 rows processed
--5. INDEX FULL SCAN (MIN/MAX)
SQL> SELECT max(object_id) FROM test_objects;
Execution Plan
----------------------------------------------------------
Plan hash value: 729623451
-------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 4 | 2 (0)| 00:00:01 |
| 1 | SORT AGGREGATE | | 1 | 4 | | |
| 2 | INDEX FULL SCAN (MIN/MAX)| IDX_TEST_OBJECTS_2 | 1 | 4 | 2 (0)| 00:00:01 |
-------------------------------------------------------------------------------------------------
Statistics
----------------------------------------------------------
2 recursive calls
0 db block gets
5 consistent gets
2 physical reads
0 redo size
534 bytes sent via SQL*Net to client
519 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL Tuning 基础概述10的更多相关文章
- SQL Tuning 基础概述10 - 体会索引的常见执行计划
在<SQL Tuning 基础概述05 - Oracle 索引类型及介绍>的1.5小节,提到了几种"索引的常见执行计划": INDEX FULL SCAN:索引的全扫描 ...
- SQL Tuning 基础概述08 - SQL Tuning Advisor
SQL调优顾问 SQL Tuning Advisor的使用案例: 1.构建测试表T 2.定义调整任务 3.修改调整任务参数 4.执行调整任务 5.监控调整任务 6.查看调整任务建议 7.删除调整任务 ...
- SQL Tuning 基础概述03 - 使用sql_trace和10046事件跟踪执行计划
1.使用sql_trace跟踪执行计划 1.1 当前session跟踪: alter session set sql_trace = true; //开始sql_trace alter session ...
- SQL Tuning 基础概述05 - Oracle 索引类型及介绍
一.B-Tree索引 三大特点:高度较低.存储列值.结构有序 1.1利用索引特性进行优化 外键上建立索引:不但可以提升查询效率,而且可以有效避免锁的竞争(外键所在表delete记录未提交,主键所在表会 ...
- SQL Tuning 基础概述09 - SQL Access Advisor
Oracle官方文档对SQL Access Advisor的描述如下: SQL Access Advisor, which is a tuning tool that provides advice ...
- SQL Tuning 基础概述01 - Autotrace的设定
1.autotrace的设定 SQL> set autotrace Usage: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [ST ...
- SQL Tuning 基础概述02 - Explain plan的使用
1.explain plan的使用 SQL> explain plan for delete from t_jingyu; Explained. SQL> select * from ta ...
- SQL Tuning 基础概述06 - 表的关联方式:Nested Loops Join,Merge Sort Join & Hash Join
nested loops join(嵌套循环) 驱动表返回几条结果集,被驱动表访问多少次,有驱动顺序,无须排序,无任何限制. 驱动表限制条件有索引,被驱动表连接条件有索引. hints:use_n ...
- SQL Tuning 基础概述07 - SQL Joins
N多年之前,刚刚接触SQL的时候,就被多表查询中的各种内连接,外连接,左外连接,右外连接等各式各样的连接弄的晕头转向. 更坑的是书上看到的各种表连接还有两种不同的写法, 比如对于表A,表B的查询 1, ...
随机推荐
- dbgrideh 中的keylist,picklist的用法
dbgrideh procedure TForm1.FormCreate(Sender: TObject); begin DBGridEh1.DataSource:=DataSource1; Data ...
- 【无线安全实践入门】破解WiFi密码的多个方法
本文希望可以帮助到想要学习接触此方面.或兴趣使然的你,让你有个大概的印象. 文中可能存在错误操作或错误理解,望大家不吝指正. !阅前须知! 本文是基于我几年前的一本笔记本,上面记录了我学习网络基础时的 ...
- 【计算机视觉】UCLA开源图像检测器
UCLA (加州大学洛杉矶分校)发布了一个强大的图像检测软件的源码 ,该软件可以非常高速的检测每个图像的细节,例如可用于检测指纹和虹膜,或者用于自动驾驶.通过识别物体的边界进行提取.首先确定一个物体的 ...
- Windows主机SAM文件格式破解解密
文件格式如是下图这种格式: 那么就可以通过通过kali终端samdump2 + system + sam 生成出来通过hashcat -m 1000去跑,或者通过md5查询
- nginx 增加认证
1.检查工具是否安装,如果未安装则使用yum安装 #htpasswd 有以上输出表示已经安装,如果没有按装,使用如下命令安装: #yum -y install httpd-tools 2.htpass ...
- (模板)hdoj1007(分治求平面最小点对)
题目链接:https://vjudge.net/problem/HDU-1007 题意:给定n个点,求平面距离最小点对的距离除2. 思路:分治求最小点对,对区间[l,r]递归求[l,mid]和[mid ...
- Minimum Number of Arrows to Burst Balloons
There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...
- Oracle - 函数及多表关联
函数一般是在数据上执行的,它给数据的转换和处理提供了方便.只是将取出的数据进行处理,不会改变数据库中的值.函数根据处理的数据分为单行函数和聚合函数(组函数),组函数又被称作聚合函数,用于对多行数据进行 ...
- 【LOJ】#3109. 「TJOI2019」甲苯先生的线段树
LOJ#3109. 「TJOI2019」甲苯先生的线段树 发现如果枚举路径两边的长度的话,如果根节点的值是$x$,左边走了$l$,右边走了$r$ 肯定答案会是$(2^{l + 1} + 2^{r + ...
- static char定义的用法
static char *p是全局静态变量,char *p是临时变量,static定义的你在其他地方可以调用,而且是通用的,也就是说你在一个地方改了它的值,其他地方也就跟着改了,而char *p只是一 ...