DB2索引與並發性分析
在DB2 V9.7版本之前或者DB2 V9.7版本未啟用CC特性,那麼如果查詢掃描到正在被插入或者修改的記錄,
那麼查詢將會掛起,直到修改記錄的會話提交或者回滾。
[huateng@db2server ~]$ db2 connect to dbtest
Database Connection Information
Database server = DB2/LINUX 9.7.0
SQL authorization ID = HUATENG
Local database alias = DBTEST
[huateng@db2server ~]$ db2 get db cfg | grep CUR_COMMIT
Currently Committed www.2cto.com (CUR_COMMIT) = DISABLED
當前的數據庫版本是9.7 ,未開啟 Currently Committed 特性。
會話1進行了如下的修改,沒有提交:
db2 => select * from t
ID
-----------
1
2
3
4
5
6
7
8
8 record(s) selected.
db2 => update t set id=9 where id=8
DB20000I The SQL command completed successfully.
db2 =>
此時會話2進行如下查詢將會導致掛起:
db2 => select * from t where id=1
直到會話1進行了提交。 www.2cto.com
如果T表的ID上有索引,那麼結果就會不同:
會話1:
db2 => select * from t
ID
-----------
1
2
3
4
5
6
7
8
8 record(s) selected.
db2 => create index idx_t_01 on t(id)
DB20000I The SQL command completed successfully.
db2 => commit
DB20000I The SQL command completed successfully.
db2 => update t set id=9 where id=8
DB20000I The SQL command completed successfully.
db2 =>
會話2此時進行如下查詢將會在瞬間完成
db2 => select * from t where id=1
ID
-----------
1
1 record(s) selected.
這是此時查詢將會通過索引而定位到id=1的記錄,無需掃描到ID=8的記錄,因此不會阻塞。
作者 TOMSYAN