索引是我们经常选择的数据表检索优化方案之一。其中,复合索引是我们经常选择的策略。那么,构建索引列的顺序上,有何种差异和需要注意的方面呢?下面我们通过实验来进行说明。
实验环境说明
准备数据表和实验环境。索引列的差异,主要体现在选择性上,我们通过构建不同选择性的列来进行试验。
SQL> conn scott/tiger@orcl;
Connected to Oracle DatabaseEnterpriseEdition Release10.2.0.1.0
Connected as scott
SQL> create table t as select owner, object_name from dba_objects;
Table created
SQL> select count(distinct owner), count(distinct object_name) from t;
COUNT(DISTINCTOWNER)COUNT(DISTINCTOBJECT_NAME)
-------------------- --------------------------
3030716
可以看出,在数据表T上不同列具有很大的选择性差异。
构建方案1——低选择性列为前导列
首先我们选择低选择性列owner作为索引列的前导列。
SQL> create index idx_t_cmp1 on t(owner,object_name);
Index created
SQL> exec dbms_stats.gather_table_stats(user,'T',cascade => true);
PL/SQL proc