---
title: "关于oracle 索引"
author: "Forestsai"
date: "2011-06-03T18:00:36Z"
tags: []
categories: ["数据库"]
slug: "oracle"
draft: false
lastmod: "2011-06-03T18:00:36Z"
id: 1069783
url: "https://forestsai.iteye.com/blog/1069783"
---

创建唯一索引,ind\_name为索引名称,TBS为存放索引段的表空间.(索引表空间)  
create unique index ind\_name on table\_name(col\_name) tablespace tbs;

重建索引,如果一个索引创建了很长时间就会产生碎片从而影响查询性能,这时需要重建索引.rebuild意思为重建  
alter index ind\_name rebuild;

有时候由于空间的调整,把索引从一个表空间移到另外一个表空间  
1.先查看当前所在的表空间

select index\_name,tablespace\_name from dba\_indexes where index\_name='INDEX\_PS';  
2.移到另外一个product表空间

alter index index\_ps rebuild tablespace product;  
3.再次查看表空间，确定索引是否成功移到另一个表空间  
select index\_name,tablespace\_name from dba\_indexes where index\_name='INDEX\_PS';

查看创建索引都语句

set long 10000;  
select dbms\_metadata.get\_ddl('INDEX','IND\_NAME','ZWC');

 

1.创建表，通过utlxplan.sql脚本  
SQL&gt; @?/rdbms/admin/utlxplan.sql  
2\. 创建同义词为了多个用户可以共享一个plan\_table 并授权给public  
SQL&gt; create public synonym plan\_table for plan\_table;  
SQL&gt; grant all on plan\_table to public;  
3.创建plustrace 角色  
SQL&gt; @?/sqlplus/admin/plustrce.sql 执行这个脚本就可以了。  
4、将角色权限授予public  
sql&gt;grant plustrace to public;  
完成以上几步就可以使用autotrace 功能  
set timing on --显示执行时间  
set autotrace on 查看执行计划和查询信息  
set autotrace traceonly 只显示执行计划，但不显示查询输出