oracle 查询索引语句
一、查询索引定义
1. 查询Oracle数据库中所有的索引
```
SELECT index_name, table_name FROM all_indexes;
```
2. 查询指定表中的所有索引
```
SELECT index_name FROM all_indexes WHERE table_name = '表名';
```
3. 查询指定索引的定义
```
SELECT index_name, table_name, column_name FROM all_ind_columns WHERE index_name = '索引名';
```
4. 查询索引的类型
```
SELECT index_name, table_name, index_type FROM all_indexes;
```
5. 查询索引的存储方式
```
SELECT index_name, table_name, index_type, index_subpartition_name, index_partition_
name FROM all_indexes;
```
二、查询索引状态与统计信息
1. 查询索引的状态(有效/无效)
```
SELECT index_name, status FROM all_indexes;
```
2. 查询索引的使用情况(最后一次访问时间、读取次数等)
```
SELECT index_name, last_analyzed, num_rows, leaf_blocks, distinct_keys, clustering_factor FROM all_indexes;
```
3. 查询索引的大小
```
SELECT index_name, table_name, index_type, ROUND(bytes/1024/1024, 2) AS size_mb FROM dba_segments WHERE segment_type = 'INDEX';
```
4. 查询索引的碎片化情况
```
SELECT index_name, table_name, index_type, blevel, leaf_blocks, distinct_keys, clustering_factor FROM all_indexes;
```
5. 查询索引的使用情况(是否被频繁访问)
```
SELECT index_name, table_name, user_reads, user_updates FROM all_indexes;
```
三、查询索引的相关约束
1. 查询索引所属的表的主键约束
```
SELECT index_name, table_name FROM all_indexes WHERE index_type = 'NORMAL' AND uniqueness = 'UNIQUE';
```
2. 查询索引所属的表的外键约束
```
SELECT index_name, table_name FROM all_indexes WHERE index_type = 'NORMAL' AND uniqueness = 'NONUNIQUE';
```oracle数据库怎么查询表
3. 查询索引所属的表的唯一约束
```
SELECT index_name, table_name FROM all_indexes WHERE index_type = 'NORMAL' AND uniqueness = 'NONUNIQUE';
```
4. 查询索引所属的表的检查约束
```
SELECT index_name, table_name FROM all_indexes WHERE index_type = 'NORMAL' AND uniqueness = 'NONUNIQUE';
```
5. 查询索引所属的表的默认值约束
```
SELECT index_name, table_name FROM all_indexes WHERE index_type = 'NORMAL' AND uniqueness = 'NONUNIQUE';
```
四、查询索引的相关操作
1. 查询索引的创建语句
```
SELECT _ddl('INDEX', '索引名') FROM dual;
```
2. 查询索引的重建语句
```
SELECT 'ALTER INDEX ' || index_name || ' REBUILD;' FROM all_indexes;
```
3. 查询索引的重命名语句
```
SELECT 'ALTER INDEX ' || index_name || ' RENAME TO 新索引名;' FROM all_indexes;
```
4. 查询索引的删除语句
```
SELECT 'DROP INDEX ' || index_name || ';' FROM all_indexes;
```
5. 查询索引的禁用语句
```
SELECT 'ALTER INDEX ' || index_name || ' UNUSABLE;' FROM all_indexes;
```
五、查询索引的相关性能优化
1. 查询索引是否需要重新构建
```
SELECT index_name, table_name, last_analyzed FROM all_indexes WHERE last_analyzed < SYSDATE - 30;
```
2. 查询未使用的索引
```
SELECT index_name, table_name FROM all_indexes WHERE status = 'VALID' AND (user_reads = 0 OR user_updates = 0);
```
3. 查询索引碎片化严重的情况
```
SELECT index_name, table_name, blevel, leaf_blocks, distinct_keys, clustering_factor FROM all_indexes WHERE blevel > 3;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。