Oracle中B-Tree索引与Bitmap索引的锁代价的比较
转载自:http://www.52php.cn/article/p-cavlgczq-uh.html –环境: SQL> select * from v$version;
BANNER --------------------------------------------------------------------------------
Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - Production
PL/SQL Release 11.2.0.1.0 - Production
CORE 11.2.0.1.0 Production
TNS for 32-bit Windows: Version 11.2.0.1.0 - Production
NLSRTL Version 11.2.0.1.0 - Production
–理论介绍 1)Bitmap index 场合:列的基数很少,可枚举,重复值很多,数据不会被经常更新 原理:一个键值对应N行,即 (键值:rowid)=(1:n) 格式:键值|start_rowid|end_rowid|位图 优点: 缺点: 2) B-Tree index 场合:非常适合数据重复度低的字段 例如 身份证号码 手机号码 QQ号等字段,常用于主键、唯一性约束,一般在在线交易的项目中用到的多些。 原理:一个键值对应一行(rowid) 格式: 索引头|键值|rowid 优点: 缺点:不适合键值重复率较高的字段上使用,例如 第一章 1-500page 第二章 501-1000page –实验 ①--建表
SQL> create table t_bitmap (id number(10),name varchar2(10),sex varchar2(1));
表已创建。
SQL> create bitmap index t_bitmap_idx on t_bitmap(sex);
索引已创建。
SQL> create table t_btree (id number(10),sex varchar2(1));
表已创建。
SQL> create index t_btree_idx on t_btree(sex);
索引已创建。
SQL> insert into t_btree values (1,'think','M');
已创建 1 行。
SQL> insert into t_btree values (2,'qinqin','F');
已创建 1 行。
SQL> insert into t_bitmap values(1,'M');
已创建 1 行。
SQL> insert into t_bitmap values(2,'F');
已创建 1 行。
SQL> commit;
提交完成。
SQL> select * from t_btree;
ID NAME S
---------- ---------- -
1 think M
2 qinqin F
SQL> select * from t_bitmap;
ID NAME S
---------- ---------- -
1 think M
2 qinqin F
②--对Btree index进行DML
***********session_A***************
SQL> insert into t_btree values (3,'hangzhen','M');
已创建 1 行。
***********session_B***************
SQL> insert into t_btree values (4,'yuechuang','M');
已创建 1 行。
SQL> update t_btree set sex='M' where id=2;
已更新 1 行。
SQL> delete from t_btree;
已删除3行。
③--对Bitmap index进行DML
***********session_A****************
SQL> insert into t_bitmap values (3,'M');
已创建 1 行。
***********session_B******************
㈠ insert动作
SQL> insert into t_bitmap values (4,'M');
--出现锁等待
当插入数据未涉及位图索引列“sex”字段时,是可以完成的
SQL> insert into t_bitmap (id,name) values (4,'yuechuang');
已创建 1 行。
㈡ update动作
当更新位图索引列“sex”字段值为“M”时,是无法完成的
SQL> select * from t_bitmap;
ID NAME S
---------- ---------- -
1 think M
2 qinqin F
4 yuechuang
SQL> update t_bitmap set sex='M' where id=1;
已更新 1 行。
此时成功,是因为第1行数据的sex值本身就是“M”。
SQL> update t_bitmap set sex='M' where id=4;
--锁等待。
SQL> update t_bitmap set sex='M' where id=2;
--锁等待
特别注意一下,如果更新的列不是位图索引对应的列,将不会受位图段级索引锁的限制
SQL> update t_bitmap set name='Water' where id=1;
已更新 1 行。
㈢ delete 动作
SQL> select * from t_bitmap;
ID NAME S
---------- ---------- -
3 hangzhen M
4 yuechuang F
5 ss M
SQL> delete from t_bitmap where id=3;
--锁等待
SQL> delete from t_bitmap where id=4;
已删除 1 行。
SQL> delete from t_bitmap where id=5;
--锁等待
–小结 对于B-Tree索引来说,插入动作不影响其他会话的DML操作 对于Bitmap索引来说,由于是索引段级锁,会导致与操作列值相关的内容被锁定(文中提到的“M”信息)。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |