Chinaunix首页 | 论坛 | 博客
  • 博客访问: 196038
  • 博文数量: 33
  • 博客积分: 2020
  • 博客等级: 大尉
  • 技术积分: 380
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-20 14:56
文章分类

全部博文(33)

文章存档

2010年(26)

2009年(7)

我的朋友

分类: Oracle

2010-05-04 13:57:07

Virtual indexes are definitions for indexes that are not physically created. The pur-
pose of a virtual index is to determine whether an index would be useful in tun-
ing a SQL statement without having to go through the time-consuming CPU, IO,
and storage intensive process of actually creating the index.
 
For instance, consider the following query:
SQL> EXPLAIN PLAN FOR
2      SELECT * FROM sh.sales WHERE quantity_sold > 10000
3  /
Explained.
SQL> SELECT *
2    FROM TABLE (DBMS_XPLAN.display (NULL, NULL, 'BASIC +COST'))
3  /
--------------------------------------------------
| Id  | Operation           | Name  | Cost (%CPU)|
--------------------------------------------------
|   0 | SELECT STATEMENT    |       |   441  (12)|
|   1 |  PARTITION RANGE ALL|       |   441  (12)|
|   2 |   TABLE ACCESS FULL | SALES |   441  (12)|
-------------------------------------------------
We can create a virtual index to see if the optimizer would choose to use an
index on QUANTITY_SOLD and what the expected change in cost would be:
SQL> ALTER SESSION SET "_use_nosegment_indexes"=TRUE;
Session altered.
SQL> CREATE INDEX sh.sales_vi1 ON sh.sales(quantity_sold) NOSEGMENT;
Index created.
The NOSEGMENT option of CREATE INDEX specifies that the index is
“virtual”—not associated with an actual index segment. The _USE_NOSEGMENT
_INDEXES parameter instructs Oracle to use these indexes in explain plans, if the
optimizer determines that such an index would be useful. Now let’s see if this
index would be used if we create it for real:
SQL> EXPLAIN  PLAN FOR
2      SELECT * FROM sh.sales WHERE quantity_sold > 10000;
Explained.
SQL> SELECT *
2    FROM TABLE (DBMS_XPLAN.display (NULL, NULL, 'BASIC +COST'));

---------------------------------------------------------------------
| Id  | Operation                          | Name      | Cost (%CPU)|
---------------------------------------------------------------------
|   0 | SELECT STATEMENT                   |           |     3   (0)|
|   1 |  TABLE ACCESS BY GLOBAL INDEX ROWID| SALES     |     3   (0)|
|   2 |   INDEX RANGE SCAN                 | SALES_VI1 |     2   (0)|
---------------------------------------------------------------------
In the preceding example, DBMS_XPLAN shows that the index was indeed
deemed useful and that the cost estimate reduced from 441 to 3. If queries like
this were going to be common, we’d be well advised to create the index.
阅读(2445) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~