您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

Oracle 索引简单总结

2026/2/2 1:01:05发布15次查看
建立索引时先进行排序,逻辑上分为root(根块),branch(茎块),leaf(叶子块)。leaf中存在索引列的值、长度、和所在rowid,茎块存了
建立索引时先进行排序,逻辑上分为root(根块),branch(茎块),leaf(叶子块)。leaf中存在索引列的值、长度、和所在rowid,茎块存了指向具体茎块的指针,root块同理。这是逻辑上的划分实际上根据表中数据量的多少可能会存在多层,但是索引整体上层次较低,例如一张500g的表数据量达到几百亿条,这时候它的索引只有6层。
在查询结果返回多的情况下使用索引会浪费更多的代价,另外如果一张表有五个字段,我们在三个字段上建上btree索引。那样效率只会更低。可考虑使用位图索引
索引的三大特点:
1.索引数的高度一般较低
2.索引由索引列存储的值及rowid组成
 索引select * from t where id = 1会导致索引回表的产生,若不需要看全部数据可用select id from t where id= 1或可建多列的复核索引,但是复核索引最好不要超过3列的复核。在更新操作不频繁的情况下可考虑使用索引组织表
3.索引本身是有序的
 减少order by、distinct排序所浪费的cost
聚合因子:
建立索引的列的顺序与索引自动排序的列的顺序的不对应度被称为聚合因子,聚合因子越大索引回表读越浪费时间(索引回表读不可避免的情况下)
oracle 执行计划分类:
1、table access full  全表扫描
2、index fast full scan  索引快速扫描  (不考虑排序count(id), sum(id), avg(id),列必须非空或is not null)
3、index full scan  索引全扫  (select id from t where id = 120)
4、index full scan(min/max)  最大值最小值索引全扫  (select max(id) from t)
5、table access by index rowid  索引回表读  (select * from t where id = 1)
6、index range scan  索引范围扫描  (select * from t where id 7、bitmap index fast full scan  位图索引快速扫描  (不考虑排序count(id), sum(id), avg(id),位图快速索引扫描速度非常快将近是普通索引的百倍.列不必非空)
btree索引优化简介
t表  字段:object_id, object_name
单列索引:
create index idx1_object_id on t(object_id);
count(*)、sum、avg优化:
改为count(object_id)查询,但是必须保证object_id列非空。可用如下方法告知oracle可走idx1_object_id索引:
1、select count(object_id) from t where object_id is not null;
2、设置字段非空
max/min优化:
根据第三个特性有序排列,所以max/min的查询代价会非常小。
select max(object_id) from t; 不需加上is not null; 使用执行计划:index full scan(min/max);
order by、distinct排序优化:
select * from t where object_id 未建立索引的情况下会进行排序产生tempspc;
建立索引的情况下不需要产生排序 会使用idx1_object_id索引
位图索引
创建语法:
create bitmap index idx_bitm_t_status on t(status);
适用在更新非常少的表,建立在重复度较高的列(性别)
存储结构:
位图索引存储的是比特位值
函数索引:
create table t as select * from dba_object;
create index idx_object_id on t(object_id);
create index idx_object_name on t(object_name);
create index idx_created on t(created);
select * from t where upper(object_name) = 't';
普通的btree索引,如果在对列做运算的条件下是无法使用索引查询的,,会使用table access full;
创建语句:
create index idx_upper_obj_name on(upper(object_name));
select index_name, index_type from user_indexes where table_name = 't';
函数索引的type是:function-based normal;
函数索引的cost比全表扫描要小,但是比普通的索引要大的多。
select * from t where object_id - 10这时候如果在object_id列建立普通索引时无法使用的。oracle会默认使用全表扫描的方式进行查询。可有以下两个思路进行优化:
1、select * from t where obejct_id 2、在object_id - 10上建立函数索引
写sql时要注意规范,很多语句是等价的。
select * from t where substr(object_name,1,4) = 'clus'  =    select * from t where object_name like 'clus%';
select * from t where trunc(created) >= to_date('2012-10-02', 'yyyy-mm-dd') and trunc(created) =
select * from t where created >= to_date('2012-10-02', 'yyyy-mm-dd') and created
相关阅读:
由oracle索引来理解arcsde索引
oracle索引技术之如何建立最佳索引
oracle索引列null值引发执行计划该表的测试示例
oracle索引 主键影响查询速度
oracle索引扫描
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product