ITPub博客

首页 > 数据库 > Oracle > 创建索引选项记载

创建索引选项记载

原创 Oracle 作者:dotaddjj 时间:2011-10-25 21:51:02 0 删除 编辑

索引可以说是oracle中比较重要但是又不容易彻底掌握的知识点。对于我们创建索引可能更多的就是一个很简单的语句

create index index_name on table(id) tablespace index_tbs

最近才了解到在创建索引时可以指定可选项让索引的创建适合业务的具体环境。

1 NOSORT | REVERSE:NOSORT表示与表中相同的顺序创建索引,REVERSE表示相反顺序存储索引值

create index index_name on table(id)

nosort

tablespace index_tbs

其实默认情况下,表中创建索引会对表中记录排序成功后再创建索引,其实索引的结构也可以更好的证明这点。

如果记录比较多,肯定排序是个很消耗资源的操作,不过有时候insert into语句插入数据时同时采用order by字句对索引字段进行了排序,此时就可以执行nosort选项让其不在排序。

不过个人做过如下测试:
create table tt08 as select * from dba_objects where 1<>1;

Insert into tt08 select * from dba_objects order by object_id

Create index index_tt08 on tt08(object_id) nosort

Create index时出现了ora-01409指定索引的列不是升序排列。(对于这个问题现在自己也无法能理清)

翻看资料可能是如下这个原因:

Cause: Creation of index with NOSORT option when rows were not ascending. For non-unique indexes the rowid is considered part of the index key. Therefore, if you create an index nosort and two of the rows in the table have the same key and are stored in ascending order, but get split accross two extents where the dba of the first block in the second extent is less than the dba of the last block in the first extent, then the create index nosort may fail.

Action: Create the index without the NOSORT option, or ensure table is stored in one extent

2 nologging 是否写入redo。

对于nologging可能大家都清楚,不过自己在测试中曾经做过一个

create table tt08 nologging

as select * from dba_objects

select count(*) from tt08记录条数

此时指定nologging不写入redo

.然后我此时shutdown abort数据库,然后在os上全部删除数据文件用rman来进行恢复按理说,此时buffer cache中的数据块应该没有写入数据文件的,然后执行恢复时由于redo的信息oracle在archivelog和log中找不到tt08的记录,此时是应该无法恢复tt08的,但是后来发现tt08没有任何丢失。所以这点觉得nologging确实是保障了sql的执行效率,但是如果利用redo恢复自己也还需要测试。(可能由于是测试库,dbwn已经马上就写入了,还是redo中还是有别的保障机制)

3 compute statistics

创建索引指定收集统计信息,其实个人测试发现在oracle 10G中不指定comput statistics选项oracle也会在对索引进行分析创建索引默认收集索引的统计信息。所以此项个人觉得在oracle 10G中已经和pctused在自动段空间管理中一样失去了意义。

4 online ,dml操作于创建索引

一般create index时如果别的session对表中的数据信息dml肯定是无法进行的。但是有时可能需要两者同时进行,执行online选项可以保障用户的dml不受影响,不过此时肯定会延长索引创建的时间。

5 parallel 多服务进程创建

如果企业服务器多个cpu,则可以在创建索引采取这个选项,当然数据库使用多个服务进程并行创建(并行插入也是如此)提高索引创建速度。

[@more@]

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/25362835/viewspace-1055943/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: ITL事务槽记载-01
请登录后发表评论 登录
全部评论
  • 博文量
    220
  • 访问量
    1662979