it编程 > 数据库 > Oracle

Oracle数据库索引查询方式

28人参与 2026-01-06 Oracle

一、索引基础概念

​​索引类型与适用场景​​

​​ 索引的优缺点​​

二、索引查询方法

1. ​​查看索引元信息​​

​​表的所有索引​​

select index_name, index_type, uniqueness 
from dba_indexes 
where table_name = 'employees';

​​索引的列信息​​

select column_name, column_position 
from dba_ind_columns 
where index_name = 'idx_dept_firstname';

索引所在的表信息分析

select i.index_name, i.table_name, ic.column_name, ic.column_position
from dba_indexes i
join dba_ind_columns ic on i.index_name = ic.index_name
where i.index_name = 'idx_name'; --按索引名称条件查询

2. ​​分析索引使用情况​​

​​监控索引使用频率​​

select * from v$index_usage;  -- 跟踪索引是否被有效利用,需 12c 以上版本管理员权限

​​检查未使用索引​​

select index_name from dba_indexes 
where index_name not in (select name from v$index_usage);

索引碎片与空间效率

-- 1.查询当前用户创建的索引碎片率
select index_name,
       blevel,
       leaf_blocks,
       clustering_factor,
       round((leaf_blocks * 100) / nullif(clustering_factor, 0), 2) as fragmentation_ratio
from (
    select di.index_name,
           di.blevel,
           di.leaf_blocks,
           di.clustering_factor
    from dba_indexes di
    join dba_tables dt on di.table_name = dt.table_name
    where dt.owner = user -- 只查询当前用户创建的表
      and di.clustering_factor > 1
) t
where (leaf_blocks * 100) / clustering_factor > 30; -- >30%表示需重建

-- 2.查询 索引所在的表信息分析
select i.index_name, i.table_name, ic.column_name, ic.column_position
from dba_indexes i
join dba_ind_columns ic on i.index_name = ic.index_name
where i.index_name = 'idx_name'; --按索引名称条件查询

--3.重建碎片化索引​​
alter index idx_name rebuild online;  -- idx_name  为索引名称

3. ​​执行计划分析​​

explain plan for 
select * from employees where department_id = 10;
select * from table(dbms_xplan.display);

关键指标​​:

三、优化索引空间的策略​

1. 创建索引

create unique index idx_user_name on user_info(user_name) 
tablespace idx_tbs 
compress nologging;

2. 重建碎片化索引​​

alter index idx_old rebuild online;  -- 减少空间碎片,提升查询效率

3. ​​调整存储参数​​

alter index idx_large pctfree 10;  -- 降低空闲空间预留,压缩索引体积

4. 删除冗余索引​​

drop index idx_redundant;  -- 通过监控确认使用率低的索引

5. ​​启用高级压缩​​(仅限企业版)

alter index idx_big compress advanced low;  -- 节省30-50%空间

四、关键监控指标​​

​​指标​​ ​​查看方式​​ ​​优化阈值​​
​​索引大小​​dba_segments.bytes>表空间的20%需优化
​​碎片率​​(leaf_blocks / clustering_factor) * 100>30%需重建
​​使用频率​​v$index_usage.user_reads近30天无读操作可删
​​分区均匀性​​dba_index_partitions.bytes 的方差值方差>50%需调整分区

五、 查询实践案例

1. ​​查询 oracle 表空间大小

-- 表空间使用率监控(含自动扩展状态)
select 
    df.tablespace_name "tablespace",
    df.total_mb,
    df.total_mb - fs.free_mb "used_mb",
    fs.free_mb "free_mb",
    round((df.total_mb - fs.free_mb) / df.total_mb * 100, 2) pct_used, -- 使用率
    autoext "autoext"
from 
    (select tablespace_name, 
            sum(bytes)/1024/1024 total_mb,
            max(decode(autoextensible,'yes','y','n')) autoext
     from dba_data_files 
     group by tablespace_name) df
join 
    (select tablespace_name, 
            sum(bytes)/1024/1024 free_mb 
     from dba_free_space 
     group by tablespace_name) fs 
    on df.tablespace_name = fs.tablespace_name
where round((df.total_mb - fs.free_mb) / df.total_mb * 100, 2) > 80  -- 仅显示>80%使用率的表空间
order by pct_used desc;

结果示例:

tablespacetotal_mbused_mbfree_mbpct_usedautoext
tbs_picp3548330923993.26
tbs_picp_new4048352352587.03

2. 查询 oracle 索引使用情况

替换 picp_formal(表用户) 和 t_user_info(表名称 需大写)

-- 替换 picp_formal 和 t_user_info(需大写)
with table_info as (
    select 
        t.owner,
        t.table_name,
        t.tablespace_name,
        t.num_rows,
        t.avg_row_len,
        round((t.num_rows * t.avg_row_len) / 1024 / 1024, 2) as estimated_data_size_mb,
        round(sum(s.bytes) / 1024 / 1024, 2) as actual_table_size_mb
    from dba_tables t
    join dba_segments s on t.owner = s.owner and t.table_name = s.segment_name
    where t.owner = 'picp_formal'
      and t.table_name = 't_user_info'
      and s.segment_type = 'table'
    group by t.owner, t.table_name, t.tablespace_name, t.num_rows, t.avg_row_len
),
index_info as (
    select 
        i.index_name,
        round(s.bytes / 1024 / 1024, 2) as index_size_mb,
        i.uniqueness
    from dba_indexes i
    join dba_segments s on i.owner = s.owner and i.index_name = s.segment_name
    where i.table_owner = 'picp_formal'
      and i.table_name = 't_user_info'
      and s.segment_type = 'index'
)
select 
    -- 表基本信息
    ti.table_name,
    ti.tablespace_name,
    ti.num_rows,
    ti.avg_row_len,
    ti.estimated_data_size_mb,
    ti.actual_table_size_mb,
    
    -- 索引详细信息
    ii.index_name,
    ii.index_size_mb,
    ii.uniqueness,
    
    -- 索引汇总信息
    round(sum(ii.index_size_mb) over (), 2) as total_index_size_mb,
    round((sum(ii.index_size_mb) over () / ti.actual_table_size_mb) * 100, 2) as index_to_table_ratio_percent
from table_info ti
left join index_info ii on 1=1
order by ii.index_size_mb desc nulls last;

示例结果如下:

table_nametablespace_namenum_rowsavg_row_lenestimated_data_size_mbactual_table_size_mbindex_nameindex_size_mbuniquenesstotal_index_size_mbindex_to_table_ratio_percent
t_user_infotbs_picp_new63604637262.55271pk_t_user_id45unique37137.09
t_user_infotbs_picp_new63604637262.55271idx_user_name28nonunique7137.09

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

您想发表意见!!点此发布评论

推荐阅读

Oracle索引失效的情况及处理过程

01-06

oracle实现新建用户并授权

01-07

windows环境下重建oracle监听方式

01-07

oracle表碎片查询整理过程

01-07

Oracle统计每日发生次数多种实现方式

01-04

Oracle清理监听文件的几种安全方式

12-31

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论