1、创建测试表t_test
iSQL> create table t_test
2 (
3 c1 int,
4 c2 char,
5 c3 int
6 );
Create success.
2、基于表t_test的三列(c1、c2、c3)创建联合PK索引t_test_pk
iSQL> alter table t_test add constraint t_test_pk primary key (c1,c2,c3);
Alter success.
3、查看表t_test表结构
iSQL> desc t_test
[ TABLESPACE : SYS_TBS_MEMORY ]
[ ATTRIBUTE ]
------------------------------------------------------------------------------
NAME TYPE IS NULL
------------------------------------------------------------------------------
C1 INTEGER FIXED NOT NULL
C2 CHAR(1) FIXED NOT NULL
C3 INTEGER FIXED NOT NULL
[ INDEX ]
------------------------------------------------------------------------------
NAME TYPE IS UNIQUE COLUMN
------------------------------------------------------------------------------
T_TEST_PK BTREE UNIQUE C1 ASC,
C2 ASC,
C3 ASC
[ PRIMARY KEY ]
------------------------------------------------------------------------------
C1, C2, C3
4、打开Altibase执行计划
iSQL> alter session set explain plan = only;
Alter success.
5、SQL1执行计划
iSQL> SELECT * FROM t_test WHERE c1=1;
T_TEST.C1 T_TEST.C2 T_TEST.C3
---------------------------------------
No rows selected.
------------------------------------------------------------
PROJECT ( COLUMN_COUNT: 3, TUPLE_SIZE: 12 )
SCAN ( TABLE: T_TEST, INDEX: T_TEST_PK, ACCESS: ??, SELF_ID: 2 )
------------------------------------------------------------
6、SQL2执行计划
iSQL> SELECT * FROM t_test WHERE c1=1 AND c2='a';
T_TEST.C1 T_TEST.C2 T_TEST.C3
---------------------------------------
No rows selected.
------------------------------------------------------------
PROJECT ( COLUMN_COUNT: 3, TUPLE_SIZE: 12 )
SCAN ( TABLE: T_TEST, INDEX: T_TEST_PK, ACCESS: ??, SELF_ID: 2 )
------------------------------------------------------------
7、SQL3执行计划
iSQL> SELECT * FROM t_test WHERE c2='s';
T_TEST.C1 T_TEST.C2 T_TEST.C3
---------------------------------------
No rows selected.
------------------------------------------------------------
PROJECT ( COLUMN_COUNT: 3, TUPLE_SIZE: 12 )
SCAN ( TABLE: T_TEST, FULL SCAN, ACCESS: ??, SELF_ID: 2 )
------------------------------------------------------------
8、SQL4执行计划
iSQL> SELECT * FROM t_test WHERE c2='w' AND c3=2;
T_TEST.C1 T_TEST.C2 T_TEST.C3
---------------------------------------
No rows selected.
------------------------------------------------------------
PROJECT ( COLUMN_COUNT: 3, TUPLE_SIZE: 12 )
SCAN ( TABLE: T_TEST, FULL SCAN, ACCESS: ??, SELF_ID: 2 )
------------------------------------------------------------
总结:
Altibase对于联合索引列(c1,c2,c3),如果where条件后的列包括从左边第一列(c1),那么Altibase将采用联合索引扫描表;反之则进行全表扫描表。
例如:SQL1的where条件列是c1,SQL2的where条件列是c1和c2,因此在查询表时都采用联合索引,反之,SQL3的where条件列是c2,SQL4的where条件列是c2和c3,因此在查询表时都没采用索引,而是全表扫描。
[
本帖最后由 tom_111 于 2009-4-14 00:24 编辑 ]
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10954452/viewspace-606860/,如需转载,请注明出处,否则将追究法律责任。