oracle的位图索引多用于数据仓库和dss系统,这些年维护的系统也都是oltp的系统,基本都是在和b tree索引打交道,对于bitmap也很少接触,查阅了一些书籍和自己的测试来简单学习下bitmap index。 位图索引一般创建在distinct value很小(num_distinct/num_rows
oracle的位图索引多用于数据仓库和dss系统,这些年维护的系统也都是oltp的系统,基本都是在和b tree索引打交道,对于bitmap也很少接触,查阅了一些书籍和自己的测试来简单学习下bitmap index。
位图索引一般创建在distinct value很小(num_distinct/num_rows
这个结构描述确实有些复杂难懂,简单来说就是原来的b tree索引的键值和rowid被修改为了键值、rowid上下限、位图段,当通过位图索引查找数据时,oracle同样的先根据root到branch再到leaf block,然后根据键值过滤,但是由于bitmap index下索引键值并不是和rowid一一对应,而是记录一系列的rowid上下限,bitmap index下找到键值后会将这个键值的位图段解压缩,获取到位图是1的,然后结合rowid的上下限通过转换函数得到最终的rowid然后回表,同样由于位图段是被压缩的,索引键值对应一个rowid上下限,bitmap index往往比b tree索引小很多。
位图索引的结构图:
位图索引的并发局限性:
由于位图索引是用一个位图段来记录,dml时候就没有行锁的概念,要锁就锁住了整个位图段,位图索引也不建议在oltp这类高并发的事务处理系统中使用。
session a:
sql> create table tab_bitmap01(id number,name varchar2(10));
table created.
sql> create bitmap index ind_bitmap_name on tab_bitmap01(name);
index created.
sql> insert into tab_bitmap01 values(1,'ss');
1 row created.
session a不提交事务
session b:
sql> insert into tab_bitmap01 values(1,'ss’);
session b被阻塞
session c:
sql> insert into tab_bitmap01 values(1,'st');
1 row created.
sql> insert into tab_bitmap01 values(1,'sr');
1 row created.
session c的dml事务可以正常执行
由于等于ss的数据较少,bitmap index中键值等于ss都在同一个索引条目中也就是同一个位图段中,由于每次dml都是锁住对应的位图段,那么插入到同一个位图段的相关dml语句全部被阻塞,但是别的索引键值插入会重新生成一个索引条目也就是生成另一个位图段,由于不共用相同的位图段,对应的dml语句将不会被阻塞。
位图索引的优点:
1 根据存储结构而言,位图索引会更加节省空间
2 位图索引能够显示某些复杂的and和or查询条件的sql语句,这主要是因为位图索引能够实现快捷的按位运算
下面我们来看有关bitmap index的执行计划,bitmap index有关的执行计划有bitmap index single value、bitmap index range scan、bitmap index full scan、bitmap index fast full scan、bitmap and、bitmap or和bitmap conversion to rowids等,下面只简单的列出常见的几种。
sql> create table tab_bitmap02 as select * from dba_objects;
table created.
sql> create bitmap index ind_bitmap_type on tab_bitmap02(object_type);
index created.
sql> exec dbms_stats.gather_table_stats(ownname=>user,tabname=>'tab_bitmap02',cascade=>true);
pl/sql procedure successfully completed.
sql> select * from tab_bitmap02 where object_type='table';
2898 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 2735916582
------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
------------------------------------------------------------------------------------------------
| 0 | select statement | | 2726 | 260k| 281 (1)| 00:00:04 |
| 1 | table access by index rowid | tab_bitmap02 | 2726 | 260k| 281 (1)| 00:00:04 |
| 2 | bitmap conversion to rowids| | | | | |
|* 3 | bitmap index single value | ind_bitmap_type | | | | |
------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
3 - access(object_type='table')
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
389 consistent gets
0 physical reads
0 redo size
322090 bytes sent via sql*net to client
2646 bytes received via sql*net from client
195 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
2898 rows processed
比较常见的bitmap index single value先进行键值筛选,然后bitmap conversion to rowids转换为rowid
sql> select * from tab_bitmap02 where object_type>'lob' and object_type
execution plan
----------------------------------------------------------
plan hash value: 1977456414
------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
------------------------------------------------------------------------------------------------
| 0 | select statement | | 8 | 784 | 3 (0)| 00:00:01 |
| 1 | table access by index rowid | tab_bitmap02 | 8 | 784 | 3 (0)| 00:00:01 |
| 2 | bitmap conversion to rowids| | | | | |
|* 3 | bitmap index range scan | ind_bitmap_type | | | | |
------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
3 - access(object_type>'lob' and object_type
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
4 consistent gets
0 physical reads
0 redo size
1651 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
这里是范围的bitmap index range scan扫描。
sql> select object_type from tab_bitmap02;
86304 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 45367078
------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
------------------------------------------------------------------------------------------------
| 0 | select statement | | 86304 | 758k| 5 (0)| 00:00:01 |
| 1 | bitmap conversion to rowids | | 86304 | 758k| 5 (0)| 00:00:01 |
| 2 | bitmap index fast full scan| ind_bitmap_type | | | | |
------------------------------------------------------------------------------------------------
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
46 consistent gets
0 physical reads
0 redo size
1485341 bytes sent via sql*net to client
63806 bytes received via sql*net from client
5755 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
86304 rows processed
bitmap index fast full scan的执行计划类似于b tree的index fast full scan
sql> select * from (select * from tab_bitmap02 order by object_type) where rownum
execution plan
----------------------------------------------------------
plan hash value: 3339180245
------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes |tempspc| cost (%cpu)| time |
------------------------------------------------------------------------------------------------
| 0 | select statement | | 1 | 207 | | 2271 (1)| 00:00:28 |
|* 1 | count stopkey | | | | | | |
| 2 | view | | 86304 | 17m| | 2271 (1)| 00:00:28 |
|* 3 | sort order by stopkey| | 86304 | 8259k| 11m| 2271 (1)| 00:00:28 |
| 4 | table access full | tab_bitmap02 | 86304 | 8259k| | 344 (1)| 00:00:05 |
------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(rownum 3 - filter(rownum
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
1237 consistent gets
0 physical reads
0 redo size
1609 bytes sent via sql*net to client
523 bytes received via sql*net from client
2 sql*net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
sql> select * from (select /*+index(tab_bitmap02 ind_bitmap_type)*/* from tab_bitmap02 order by object_type) where rownum
execution plan
----------------------------------------------------------
plan hash value: 2399409298
--------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
--------------------------------------------------------------------------------------------------
| 0 | select statement | | 1 | 207 | 7 (0)| 00:00:01 |
|* 1 | count stopkey | | | | | |
| 2 | view | | 1 | 207 | 7 (0)| 00:00:01 |
| 3 | table access by index rowid | tab_bitmap02 | 1 | 98 | 7 (0)| 00:00:01 |
| 4 | bitmap conversion to rowids| | | | | |
| 5 | bitmap index full scan | ind_bitmap_type | | | | |
--------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(rownum
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
3 consistent gets
0 physical reads
0 redo size
1609 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> select * from (select /*+index(tab_bitmap02 ind_bitmap_type)*/* from tab_bitmap02 order by object_type desc) where rownum
execution plan
----------------------------------------------------------
plan hash value: 89545540
-----------------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes |tempspc| cost (%cpu)| time |
-----------------------------------------------------------------------------------------------------------
| 0 | select statement | | 1 | 207 | | 3937 (1)| 00:00:48 |
|* 1 | count stopkey | | | | | | |
| 2 | view | | 86304 | 17m| | 3937 (1)| 00:00:48 |
|* 3 | sort order by stopkey | | 86304 | 8259k| 11m| 3937 (1)| 00:00:48 |
| 4 | table access by index rowid | tab_bitmap02 | 86304 | 8259k| | 2011 (1)| 00:00:25 |
| 5 | bitmap conversion to rowids| | | | | | |
| 6 | bitmap index full scan | ind_bitmap_type | | | | | |
-----------------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
1 - filter(rownum 3 - filter(rownum
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
3504 consistent gets
0 physical reads
0 redo size
1631 bytes sent via sql*net to client
523 bytes received via sql*net from client
2 sql*net roundtrips to/from client
1 sorts (memory)
0 sorts (disk)
1 rows processed
这里需要加上hint index,优化器才会考虑走该索引的全扫描然后回表,而如果我们不加hint,上面这个sql会走成本更大的全表扫描,这个大家可以做个10053来看看优化器为什么不选择成本更低的索引回表的方式。
另外更需要我们注意的是如果asc排序的话,bitmap index是可以扫描leaf block的头部块然会回表,但是desc排序bitmap index则要扫描完所有的leaf block后回表排序取出数据,大家有兴趣的可以倒腾下。
sql> select count(*) from tab_bitmap02 where object_type='table';
execution plan
----------------------------------------------------------
plan hash value: 1775585465
-----------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-----------------------------------------------------------------------------------------------
| 0 | select statement | | 1 | 9 | 1 (0)| 00:00:01 |
| 1 | sort aggregate | | 1 | 9 | | |
| 2 | bitmap conversion count | | 2726 | 24534 | 1 (0)| 00:00:01 |
|* 3 | bitmap index single value| ind_bitmap_type | | | | |
-----------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
3 - access(object_type='table')
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
2 consistent gets
0 physical reads
0 redo size
527 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
count(*)求总数时bitmap index是会直接走bitmap conversion count,通过解压缩的位图来直接计算count(*)
sql> create bitmap index ind_bitmap_owner on tab_bitmap02(owner);
index created.
sql> select * from tab_bitmap02 where object_type='table' and owner='sys';
1020 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 3549035508
-------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------------------------------
| 0 | select statement | | 119 | 11662 | 27 (0)| 00:00:01 |
| 1 | table access by index rowid | tab_bitmap02 | 119 | 11662 | 27 (0)| 00:00:01 |
| 2 | bitmap conversion to rowids| | | | | |
| 3 | bitmap and | | | | | |
|* 4 | bitmap index single value| ind_bitmap_type | | | | |
|* 5 | bitmap index single value| ind_bitmap_owner | | | | |
-------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
4 - access(object_type='table')
5 - access(owner='sys')
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
143 consistent gets
0 physical reads
0 redo size
108064 bytes sent via sql*net to client
1260 bytes received via sql*net from client
69 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
1020 rows processed
通过两个bitmap index先进行bitmap index single value,然后再进行bitmap and合并
sql> select * from tab_bitmap02 where object_type='cluster' or owner='account';
10 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 1941447823
-------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------------------------------
| 0 | select statement | | 3759 | 359k| 319 (0)| 00:00:04 |
| 1 | table access by index rowid | tab_bitmap02 | 3759 | 359k| 319 (0)| 00:00:04 |
| 2 | bitmap conversion to rowids| | | | | |
| 3 | bitmap or | | | | | |
|* 4 | bitmap index single value| ind_bitmap_owner | | | | |
|* 5 | bitmap index single value| ind_bitmap_type | | | | |
-------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
4 - access(owner='account')
5 - access(object_type='cluster')
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
11 consistent gets
0 physical reads
0 redo size
2449 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)
10 rows processed
将两个bitmap index single value通过bitmap or合并
这里由于是两个过滤条件and或者or运算,而且两个过滤条件都有bitmap index,bitmap这里可以直接通过位图按位合并进行bitmap and或者bitmap or,然后转换为rowid。
bitmap index还可以有bitmap minus这种方式来处理下列的查询
sql> select /*+index(tab_bitmap02 ind_bitmap_type) index(tab_bitmap02 ind_bitmap_owner)*/* from tab_bitmap02 where object_type='cluster' and owner!='xiaoyu';
10 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 2263546975
-------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------------------------------
| 0 | select statement | | 15 | 1470 | 6 (0)| 00:00:01 |
| 1 | table access by index rowid | tab_bitmap02 | 15 | 1470 | 6 (0)| 00:00:01 |
| 2 | bitmap conversion to rowids | | | | | |
| 3 | bitmap minus | | | | | |
| 4 | bitmap minus | | | | | |
|* 5 | bitmap index single value| ind_bitmap_type | | | | |
|* 6 | bitmap index single value| ind_bitmap_owner| | | | |
|* 7 | bitmap index single value | ind_bitmap_owner| | | | |
-------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
5 - access(object_type='cluster')
6 - access(owner='xiaoyu')
7 - access(owner is null)
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
13 consistent gets
1 physical reads
0 redo size
2449 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)
10 rows processed
如果是一个复合索引对于下列这种or的查询是怎么样:
sql> create bitmap index ind_multi_bitmap on tab_bitmap02(object_type,owner);
index created.
sql>drop index ind_bitmap_owner ;
sql> select /*+index(tab_bitmap02 ind_multi_bitmap)*/* from tab_bitmap02 where owner='account' or object_type='cluster';
10 rows selected.
execution plan
----------------------------------------------------------
plan hash value: 4065408901
-------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------------------------------
| 0 | select statement | | 3759 | 359k| 2013 (1)| 00:00:25 |
| 1 | table access by index rowid | tab_bitmap02 | 3759 | 359k| 2013 (1)| 00:00:25 |
| 2 | bitmap conversion to rowids| | | | | |
|* 3 | bitmap index full scan | ind_multi_bitmap | | | | |
-------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
3 - filter(owner='account' or object_type='cluster')
statistics
----------------------------------------------------------
1 recursive calls
0 db block gets
16 consistent gets
13 physical reads
0 redo size
2449 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)
10 rows processed
优化器考虑成本一般只能选择全索引扫描来完成.
xiaoyu在测试中没有发现bitmap有b tree的跳跃索引的执行计划,而都是采用的bitmap index single value
sql> select /*+index(tab_bitmap02 ind_multi_bitmap)*/* from tab_bitmap02 where owner='account';
no rows selected
execution plan
----------------------------------------------------------
plan hash value: 4065408901
-------------------------------------------------------------------------------------------------
| id | operation | name | rows | bytes | cost (%cpu)| time |
-------------------------------------------------------------------------------------------------
| 0 | select statement | | 3744 | 358k| 2013 (1)| 00:00:25 |
| 1 | table access by index rowid | tab_bitmap02 | 3744 | 358k| 2013 (1)| 00:00:25 |
| 2 | bitmap conversion to rowids| | | | | |
|* 3 | bitmap index full scan | ind_multi_bitmap | | | | |
-------------------------------------------------------------------------------------------------
predicate information (identified by operation id):
---------------------------------------------------
3 - filter(owner='account')
statistics
----------------------------------------------------------
0 recursive calls
0 db block gets
8 consistent gets
0 physical reads
0 redo size
1343 bytes sent via sql*net to client
512 bytes received via sql*net from client
1 sql*net roundtrips to/from client
0 sorts (memory)
0 sorts (disk)
0 rows processed
当然对于上面这些执行计划和索引的创建方式,大家也没有必要去死记这些东西,理解了index的原理,个人觉得对于sql调优是易学难精的,要想在调优上有所建树,肯定得有大量的案例、测试用例和理论知识做铺垫!
原文地址:bitmap index及其执行计划简介, 感谢原作者分享。