位图索引。
今天是2014-01-15,继续进行索引的相关技术学习,今天学习内容为位图索引;
位图索引使用指南:
1、一般位图索引使用在基数列比较小的列
2、最好用于数据仓库和dss系统
3、最好用于星型模式
4、对于在索引列上有很多链接查询或是过滤查询的情况,位图索引有很高的性能
5、位图索引对dml操作支持性不好,建议在进行dml操作时删掉位图索引,之后再重建(同样适用于在分区上建立的位图索引)
位图索引创建:
create bitmap index index_name on table_name(table_column_name) nologging;
注意因为位图索引对dml语句支持性不好需要使用nologging操作。
另外位图索引不像B树索引一样不存储null值。B树索引对于单列字段是不存储null值的,对于多列字段其中一例不为null,另一列为null的索引是存储null值的。因此在B树索引列上如果存在null可能出现不走索引的情况,但是位图索引不会这样,因为它实际存储null值。
测试如下:
SQL> conn amy/rhys
Connected.
SQL> select index_name,column_name,column_position,table_name from user_ind_columns where table_name='EMP';
INDEX_NAME COLUMN_NAME COLUMN_POSITION TABLE_NAME
------------------------------ -------------------- --------------- ------------------------------
EMP_BTIDX1 ENAME 1 EMP
SQL> drop index emp_btidx1;
Index dropped.
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10
9888 xiaohai
15 rows selected.
SQL> create index emp_idx1 on emp(ename);
Index created.
SQL> set autotrace trace
SQL> select * from emp where ename is null;
Execution Plan
----------------------------------------------------------
Plan hash value: 3956160932
--------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
--------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 6 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL| EMP | 1 | 37 | 6 (0)| 00:00:01 |
--------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("ENAME" IS NULL)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
18 consistent gets
0 physical reads
0 redo size
1004 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL> set autotrace off
SQL> drop index emp_idx1;
Index dropped.
SQL> create bitmap index emp_btidx1 on emp(ename);
Index created.
SQL> set autotrace trace
SQL> select * from emp where ename is null;
Execution Plan
----------------------------------------------------------
Plan hash value: 448664046
-------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 37 | 3 (0)| 00:00:01 |
| 1 | TABLE ACCESS BY INDEX ROWID | EMP | 1 | 37 | 3 (0)| 00:00:01 |
| 2 | BITMAP CONVERSION TO ROWIDS| | | | | |
|* 3 | BITMAP INDEX SINGLE VALUE | EMP_BTIDX1 | | | | |
-------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
3 - access("ENAME" IS NULL)
Statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
1003 bytes sent via SQL*Net to client
523 bytes received via SQL*Net from client
2 SQL*Net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1 rows processed
SQL>
分区表与位图索引:
位图索引只能在分区表上创建本地索引,不能创建全局索引:
SQL> create bitmap index achivement_btidx1 on achivement(id);
create bitmap index achivement_btidx1 on achivement(id)
*
ERROR at line 1:
ORA-25122: Only LOCAL bitmap indexes are permitted on partitioned tables
SQL> create bitmap index achivement_btidx1 on achivement(id) local nologging;
Index created.
SQL> drop index achivement_btidx1;
Index dropped.
SQL> create bitmap index achivement_btidx1 on achivement(id) global;
create bitmap index achivement_btidx1 on achivement(id) global
*
ERROR at line 1:
ORA-25113: GLOBAL may not be used with a bitmap index
SQL>
索引组织表与位图索引:
在索引组织表上创建二级位图索引必须指定映射表mapping table,如果没有则可以添加映射表,在进行创建位图索引:
SQL>
SQL>
SQL>
SQL> create table emp_list(
2 emp_id number(6) not null,
3 first_name varchar2(20)
4 ,last_name varchar2(20),
5 email varchar2(25) not null,
6 constraint emp_list_pk primary key (emp_id,first_name)
7 )
8 organization index
9 mapping table;
Table created.
SQL> create bitmap index emp_list_btidx1 on emp_list(email) nologging;
Index created.
SQL> drop index emp_list_btidx1;
Index dropped.
SQL> alter table emp_list move nomapping;
Table altered.
SQL> create bitmap index emp_list_btidx1 on emp_list(email) nologging;
create bitmap index emp_list_btidx1 on emp_list(email) nologging
*
ERROR at line 1:
ORA-28669: bitmap index can not be created on an IOT with no mapping table
SQL> alter table emp_list move mapping table;
Table altered.
SQL> create bitmap index emp_list_btidx1 on emp_list(email) nologging;
Index created.
SQL>
另外在使用星型模式时,位图索引可以提高很高的性能,但是在中心事实表的外键列上必须创建位图索引,另外还需要配置星型转换参数如:star_transformation_enabled 为true;
如果没有走星型转换可以强制添加hint(提示)如:/*+star_transformation*/ /*+star_transformation fact(column_name)*/
在 开始也提到位图索引对dml语句支持性不好,因此在搞的dml操作的时候需要对位图索引进行unusable之后再重建。对于分区位图索引需要对每个分区进行位图索引重建;
如:alter index xxxx modify partition ppppppp unusable; alter index xxxx rebuild partition ppppppp;或是对分区上所有索引重建:alter table xxx modify partition xxxx rebuild unusable local indexes;
另外对于位图索引还有一个位图连接索引
位图连接索引适合与星型模式,就是一个大的事实表与维度表之间的连接列的物化连接,在创建连接索引过程中就已经完成了连接位置操作,连接的结果也存在于连接索引中,因此连接查询速度会更高:
创建实例如下
create bitmap index emp_bjix01
on xxx(a.ssss,b.dddd)
from www a,zzz b
where a.pp=b.pp
tablespace mmmmm
pctfree 5
parallel 4
local
nologging;
对于位图索引查看信息可以查看dba_indexes 和 dba_join_ind_columns两个视图获得相关信息。