您好,欢迎访问一九零五行业门户网

Oracle sql 调优:使用虚拟索引在生产环境测试创建索引对数据库

虚拟索引是一种“假”索引,其定义存在于数据字典中,但不具有相应的索引段,也就是不会分配任何存储空间。利用虚拟索引,开发人员 可以无需等待索引创建完成,也不需要额外的索引存储空间,就可以当做索引已经存在,累测试 sql 语句的执行计划。如果优化器
虚拟索引是一种“假”索引,其定义存在于数据字典中,但不具有相应的索引段,也就是不会分配任何存储空间。利用虚拟索引,开发人员
可以无需等待索引创建完成,也不需要额外的索引存储空间,就可以当做索引已经存在,累测试 sql 语句的执行计划。如果优化器为某个
sql 语句创建的执行计划代价很高,sql tuning advisor 可能会建议在某个列上创建索引,但是在生产环境下,我们是没法随意
来创建索引和测试这些更改的。我们需要确保要创建的索引不会对数据库中运行的其他查询的执行计划产生任何影响。虚拟索引的出现就
是为了解决这个问题的:
下面我们来做一个测试来介绍虚拟索引的用法
1)  创建示例表
sql> create table test as select * from dba_objects;
2) 对该表执行任意的查询
16:43:55 system@prod>  select * from test where object_name = 'emp';
owner                          object_name          subobject_name                  object_id data_object_id object_type         created         last_ddl_time       timestamp           status  t g s  namespace edition_name
------------------------------ -------------------- ------------------------------ ---------- -------------- ------------------- ------------------- ------------------- ------------------- ------- - - - ---------- ------------------------------
scott                          emp                                                      75315          75315 table               2011-09-18 18:03:42 2013-03-10 17:07:42 2011-09-18:18:03:42 valid   n n n      1
3) 查看上述查询的执行计划
16:44:31 system@prod> set autotrace traceonly explain
16:44:42 system@prod> select * from test where object_name = 'emp';
elapsed: 00:00:00.01
execution plan
----------------------------------------------------------
plan hash value: 1357081020
--------------------------------------------------------------------------
| id  | operation         | name | rows  | bytes | cost (%cpu)| time     |
--------------------------------------------------------------------------
|   0 | select statement  |      |    12 |  2484 |   293   (1)| 00:00:04 |
|*  1 |  table access full| test |    12 |  2484 |   293   (1)| 00:00:04 |
--------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(object_name='emp')
note
-----
   - dynamic sampling used for this statement (level=2)
4)  在 test 表的 object_name 字段上面创建虚拟索引
16:45:44 system@prod> create index test_index on test(object_name) nosegment;
index created.
注意,在创建虚拟索引时需要在 create index 语句中指定 nosegment 子句,执行上述语句后,实际上数据库中
并未创建索引段,也就是并未给 test_index 对象分配存储空间,这点我们可以通过下面步骤来验证。
5)  通过 dba_objects 可以查看到刚刚创建的 test_index 对象
16:46:02 system@prod> set autotrace off
16:50:16 system@prod> col object_name for a20;
16:50:26 system@prod> select object_name,object_type from dba_objects where object_name = 'test_index';
object_name          object_type
-------------------- -------------------
test_index           index
但是通过 dba_indexes、dba_segments 和 dba_extents 我们查看不到该对象
16:53:06 system@prod> select index_name,index_type,table_name from dba_indexes where index_name = 'test_index';
no rows selected
16:55:50 system@prod> select segment_name,segment_type,tablespace_name from dba_segments where segment_name = 'test_index';
no rows selected
16:56:46 system@prod> select segment_name,segment_type,tablespace_name from dba_extents where segment_name = 'test_index';
no rows selected
通过上述的查询可以看出,数据库中创建了该对象,但未创建相应的 segment ,分配存储空间。
6)  再次查看之前 sql 的执行计划,看看是否使用了刚刚创建的虚拟索引
16:57:11 system@prod> set autotrace traceonly explain
16:58:47 system@prod> select * from test where object_name = 'emp';
elapsed: 00:00:00.04
execution plan
----------------------------------------------------------
plan hash value: 1357081020
--------------------------------------------------------------------------
| id  | operation         | name | rows  | bytes | cost (%cpu)| time     |
--------------------------------------------------------------------------
|   0 | select statement  |      |    12 |  2484 |   293   (1)| 00:00:04 |
|*  1 |  table access full| test |    12 |  2484 |   293   (1)| 00:00:04 |
--------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(object_name='emp')
note
-----
   - dynamic sampling used for this statement (level=2)
--我们看到创建虚拟索引后,执行计划并未改变
7)  我们需要修改数据库的隐含参数 _use_nosegment_indexes 来强制session使用虚拟索引
17:01:24 system@prod> alter session set _use_nosegment_indexes=true;
session altered.
8) 再次查看执行计划
17:02:06 system@prod> select * from test where object_name = 'emp';
elapsed: 00:00:00.02
execution plan
----------------------------------------------------------
plan hash value: 2627321457
------------------------------------------------------------------------------------------
| id  | operation                   | name       | rows  | bytes | cost (%cpu)| time     |
------------------------------------------------------------------------------------------
|   0 | select statement            |            |    12 |  2484 |     5   (0)| 00:00:01 |
|   1 |  table access by index rowid| test       |    12 |  2484 |     5   (0)| 00:00:01 |
|*  2 |   index range scan          | test_index |   309 |       |     1   (0)| 00:00:01 |
------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
2 - access(object_name='emp')
note
-----
   - dynamic sampling used for this statement (level=2)
-----------------------------------------------------------------------------------------
设置 _use_nosegment_indexes 隐含参数后,优化器将使用在此表上创建的虚拟索引。在其他session中运行该查询时,不会使用
此虚拟索引,因为我们只是修改了 session 级别的隐含参数。
虚拟索引使用的注意事项:
1、可以对虚拟索引执行 analyze 操作
17:07:13 system@prod> analyze index test_index compute statistics;
index analyzed.
2、无法对虚拟索引执行 rebuild 操作,否则会报 ora-8114: user attempted to alter a fake index 错误
17:07:52 system@prod> alter index test_index rebuild;
alter index test_index rebuild
*
error at line 1:
ora-08114: can not alter a fake index
3、可以像普通索引那样删除虚拟索引
17:19:20 system@prod> drop index test_index;
index dropped.
4、在oracle 9.2 to 11.1 中利用 dbms_metadata.get_ddl 来获取虚拟索引的 ddl 脚本时,不会输出虚拟索引的 nosegment 子句
我个人测试的环境是 11.2.0.3.5 可以输出 nosegment 子句
17:13:46 system@prod>  select dbms_metadata.get_ddl('index','test_index','system') ddl from dual;
ddl
--------------------------------------------------------------------------------
create index system.test_index on system.test (o
bject_name)
  pctfree 10 initrans 2 maxtrans 255  nosegment
http://blog.csdn.net/xiangsir/article/details/8693814
其它类似信息

推荐信息