不将分表条件传入sql的话,对于分表的中间层来说效率是最高的,因为不再需要分析sql来判断使用哪个表,但这样对于应用来说就不够透明了。
现在只对这两种情况的索引效率做下对比。
########################
表数据量200W
单一索引
create index IDX_TMP_1 on tmp_xf(auc_id) compute statistics;
SQL> select * from tmp_xf
2 - access("auc_id"=1002445366)
23 consistent gets
联合索引,完全满足查询条件列顺序
create index IDX_TMP_2 on tmp_xf(rated_uid, auc_id) compute statistics;
SQL> select * from tmp_xf where rated_uid = 39512 and auc_id = 1002445366;
2 - access("RATED_UID"=39512 AND "auc_id"=1002445366)
23 consistent gets
--逻辑读和单一索引一样
联合索引,索引列中间多出一列,s为number(1)类型
create index IDX_TMP_3 on tmp_xf(rated_uid,s, auc_id) compute statistics;
2 - access("RATED_UID"=39512 AND "auc_id"=1002445366)
filter("auc_id"=1002445366)
31 consistent gets
create index IDX_TMP_4 on tmp_xf(rated_uid,s,r, auc_id) compute statistics; --r 为 number(1)
2 - access("RATED_UID"=39512 AND "auc_id"=1002445366)
filter("auc_id"=1002445366)
31 consistent gets
create index IDX_TMP_5 on tmp_xf(rated_uid,s,rr, auc_id) compute statistics; --rr 为 varchar(32)
2 - access("RATED_UID"=39512 AND "auc_id"=1002445366)
filter("auc_id"=1002445366)
41 consistent gets
小结,联合索引的消耗根据查询条件和索引中列的对应关系来确定,如果满足索引列逐个用下来的情况,那效果应当是不错的。 其他的情况需要慎重,随着查询条件和索引列的匹配关系,代价可能越来越大。
这个只是非常简单的一个数据测试,具体的联合索引消耗可以参阅Troubleshooting Oracle Performance ,有非常精彩的论述
This work is licensed under a CC A-S 4.0 International License.