教你如何成为Oracle 10g OCP - 第九章 对象管理(11) - 管理索引,sequence及resumable


管理索引


9.2.7 管理索引
管理索引包括创建索引,rebuild index及监控索引。
我们可以使用database control来创建索引。
create index idx_book_author on books(author) tablespace indx;

重建索引 --
alter index .... rebuild ;
从Oracle8.1.6以来,可以使用online 进行索引重建。
rebuild index使用原索引的叶子节点作为新索引的数据来源,I/O比
扫描表中的数据要少很多,还有,由于叶子节点索引条目已经排好序,
所以效率会高一些。rebuild index原理详见:
http://space.itpub.net/35489/viewspace-594278

还可以通过parallel进行并行处理。加入compute statistics在索引重建过
程中就统计信息。虽然索引可以提高查询的效率,但是在更新数据的时候,
需要同时维护索引里的索引条目,因此它会降低DML操作的性能,Oracle建议
一个表上的索引数量不要超过7个。

 

监控索引 --

如果有些索引不能被很好的使用,就需要删除它,以免影响到DML
操作的性能,为此,Oracle提供了一个监控索引的方法,让DBA知
道,某个索引是否被使用过了,启用索引监控的方法:
alter index idx_books_emp monitoring usage ;
启用之后,我们可以查看v$object_usage视图,used字段说明该
字段在监控的时间段中是否被使用过了。
之后我们可以关闭监控索引。
alter index idx_books_emp nomonitoring usage ;
一般我们可以在典型的业务运行过程中启用索引监控。

 

9.2.8 其他对象
Oracle中常用的对象还有视图和序列号。
使用视图的好处:
1. 可以屏蔽SQL语句的复杂性。
2. 可以管理权限。


对于Sequence来说,这是一个唯一数值发生器,用来生成唯一的数值。Oracle
在生成序列号时,会考虑并发的问题,即多个session同时申请序列号的值,每
个session获得序列号的值是不一样的。特别体现在单机和RAC上。 


ORACLE序列的使用 ---
1、Create Sequence
你首先要有CREATE SEQUENCE或者CREATE ANY SEQUENCE权限,

语法 (具体解释见最下端):
CREATE SEQUENCE [schema.]sequence
    [INCREMENT BY integer]
    [START WITH integer]
    [MAXVALUE integer | NOMAXVALUE]
    [MINVALUE integer | NOMINVALUE]
    [CYCLE | NOCYCLE]
    [CACHE integer | NOCACHE]
    [ORDER | NOORDER]

例如:
CREATE SEQUENCE use1.emp_sequence
    INCREMENT BY 1  -- 每次加几个
    START WITH 1    -- 从1开始计数
    NOMAXVALUE      -- 不设置最大值
    NOCYCLE       -- 一直累加,不循环
    CACHE 10
    NOORDER;   -- 并行时取得序列的顺序需要用ORDER

一旦定义了emp_sequence,你就可以用CURRVAL,NEXTVAL
CURRVAL=返回 sequence的当前值
NEXTVAL=增加sequence的值,然后返回 sequence 值
比如:
emp_sequence.CURRVAL
emp_sequence.NEXTVAL

 


可以使用sequence的地方:
- 不包含子查询、snapshot、VIEW的 SELECT 语句
- INSERT语句的子查询中
- INSERT语句的VALUES中
- UPDATE 的 SET中  
可以看如下例子:
INSERT INTO emp VALUES  
(empseq.nextval, 'LEWIS', 'CLERK',7902, SYSDATE, 1200, NULL, 20);
SELECT empseq.currval     FROM DUAL;

- 第一次NEXTVAL返回的是初始值(比如1);随后NEXTVAL会自动增加你定义的
INCREMENT BY值,然后返回增加后的值。CURRVAL 总是返回当前SEQUENCE的
值,但是在第一次NEXTVAL初始化之后才能使用CURRVAL,否则会出错。一次
NEXTVAL会增加一次 SEQUENCE的值. 

SQL> SELECT  emp_sequence.CURRVAL from dual;  
ORA-08002: 序列 EMP_SEQUENCE.CURRVAL 尚未在此会话中定义

SQL> SELECT  emp_sequence.NEXTVAL from dual; 
NEXTVAL
-------
1

SQL> SELECT  emp_sequence.CURRVAL from dual;
CURRVAL
-------
1

SQL> select * from user_sequences ; 

sequence_name  min_value   max_value   cache_size   last_number
------------------------------------------------------------------
emp_sequence       1          1E27         10            11 


可以看到数据字典中的last_number值为11,1~10被cache到shared pool中。
这时候如果实例crash或者alter system flush shared pool,序号会不连续,
后面会详细说明。

SQL> SELECT  emp_sequence.NEXTVAL from dual; 
NEXTVAL
-------
2
再次执行发现NEXTVAL一直增加。

 

- 如果指定CACHE值,ORACLE就可以预先在内存里面放置一些sequence,这
样存取的快些。cache里面的取完后,oracle自动再取一组 到cache。 使用
cache或许会跳号, 比如数据库突然不正常down掉(shutdown abort),cache
中的sequence就会丢失. 所以可以在create sequence的时候用nocache防止
这种情况。
     比如设置cache 为20,在第一次取序列号时,会将数据字典里面
的值修改为21,因为1~20都cache到内存中了,当内存中的20个号都是用完
了,再次取20个号到内存中,数据字典里面的值改为41,以此类推,也即是
这可能引起不连号的现象,比如21~40已经在内存中,这20个序列号使用了5
个,当前内存中最小的序列号是26,如果这时实例崩溃,下次实例启动后,
26到40之间的14个号都丢失了,序列号会从数据字典记录的41开始,因此
对于字段连号的需求来说,不推荐使用sequence, 当然cache设置为0,可以
保证不删除记录的情况下会连号,因为每次取号都是从数据字典获得,不是
从内存,所以性能非常差。这里的cache是在shared pool中分配的。


一般系统cache为默认的20已经足够了,对于RAC系统,如果多个session在
不同节点并发循环间断取大量的值,那么设置cache大一些,比如1000,性能
改善比较明显,单实例测试也一样,只是没有RAC明显而已。所以cache大小
增加与否要看是否影响到性能。

排序参数: 默认是NOORDER,如果设置为ORDER;在单实例中没有影响,在RAC
环境,多实例实际缓存相同的序列,此时在多个实例并发取该序列的时候,会
有短暂的资源竞争来在多实例之间进行同步。因次性能相比noorder要差,所以
RAC环境非必须的情况下不要使用ORDER, 尤其要避免NOCACHE ORDER组合;

 


2、Alter Sequence
你或者是该sequence的owner,或者有ALTER ANY SEQUENCE 权限才能改动
sequence. 可以alter除start至以外的所有sequence参数.如果想要改变
start值,必须 drop sequence 再 re-create .
Alter sequence 的例子
ALTER SEQUENCE emp_sequence
    INCREMENT BY 10
    MAXVALUE 10000
    CYCLE    -- 到10000后从头开始
    NOCACHE ;

影响Sequence的初始化参数:
SEQUENCE_CACHE_ENTRIES =设置能同时被cache的sequence数目。 
可以很简单的Drop Sequence
DROP SEQUENCE order_seq;


特别注意:

如果你正在parallel 模式下使用Oracle parallel server选项,且指定了
ORDER选项,sequence值将不会被cache, 不管是否你指定cache或nocache。

仅仅在parallel 模式下使用Oracle parallel server 选项的情况下,ORDER
选项对于保证顺序生成序列号才有必要。 如果你正在使用exclusive mode,
sequence 号码总是按照顺序的。


----------------------------------

 


SYNTAX:

CREATE SEQUENCE [schema.]sequence
    [INCREMENT BY integer]
    [START WITH integer]
    [MAXVALUE integer | NOMAXVALUE]
    [MINVALUE integer | NOMINVALUE]
    [CYCLE | NOCYCLE]
    [CACHE integer | NOCACHE]
    [ORDER | NOORDER]

where:

INCREMENT BY
    specifies the interval between sequence numbers. This value can be
    any positive or negative Oracle integer, but it cannot be 0. If
    this value is negative, then the sequence descends. If the
    increment is positive, then the sequence ascends. If you omit this
    clause, the interval defaults to 1.

    指定序列号的interval, 可以是正负整数值,但是不能是0,如果是负值,那
    么序列号会降低,如果是正值,序列号会上升,如果忽略这个选项,interval
    默认是1. 

MINVALUE
    specifies the sequence's minimum value.

NOMINVALUE
    specifies a minimum value of 1 for an ascending sequence or -10
    for a descending sequence.
    The default is NOMINVALUE.
   
    不指定最小值,这时默认升序序列号最小值是1,降序的最小值是-10,默认设
    置是NOMINVALUE .


MAXVALUE
    specifies the maximum value the sequence can generate.

NOMAXVALUE
    specifies a maximum value of 10 for a descending sequence.
    The default is NOMAXVALUE.
   
    不指定最大值,这时默认降序的序列号最大值为10,默认为NOMAXVALUE.

START WITH
    specifies the first sequence number to be generated. You can use
    this option to start an ascending sequence at a value greater than
    its minimum or to start a descending sequence at a value less than
    its maximum. For ascending sequences, the default value is the
    sequence's minimum value. For descending sequences, the default
    value is the sequence's maximum value. 

    指定一个生成的第一个序列号码,可以使用这个选项去开始一个升序,值要
    大于它的最小值,或者开始一个降序序列号,值要小于它的最大值,对于升
    序,start with的默认值就是序列号的minimum value, 对于降序序列,默认
    值就是设置的maximum value .

CYCLE
    specifies that the sequence continues to generate values after
    reaching either its maximum or minimum value. After an ascending
    sequence reaches its maximum value, it generates its minimum value.
    After a descending sequence reaches its minimum, it generates its
    maximum. 
    指定在达到最大或最小值后继续生成值,降序序列达到它的最大值后,重新
    从最小值开始,升序序列达到它的最小值,那么重新从最大值开始。


NOCYCLE
    specifies that the sequence cannot generate more values after
    reaching its maximum or minimum value.
    The default is NOCYCLE.

    达到最大或最小值之后,不能重新生成。默认为NOCYCLE.


CACHE
    specifies how many values of the sequence Oracle preallocates and
    keeps in memory for faster access. The minimum value for this
    parameter is 2. For sequences that cycle, this value must be less
    than the number of values in the cycle.
   
    指定Oracle预分配多少个sequence值保存在内存中,这样会得到更快的访问
    速度,这个参数最小值是2。 对于循环的sequences,这个值必须小于在这个
    cycle 中的值的数目 。


NOCACHE
    specifies that values of the sequence are not preallocated.

    If you omit both the CACHE parameter and the NOCACHE option, Oracle
    caches 20 sequence numbers by default. However, if you are using
    Oracle with the Parallel Server option in parallel mode and you
    specify the ORDER option, sequence values are never cached,
    regardless of whether you specify the CACHE parameter or the NOCACHE
    option.

    指定sequecne的值不能被预分配,如果忽略cache和nocache选项,Oracle
    默认会cache 20 个sequence numbers 。 但是如果你正在parallel 模式
    下使用Oracle parallel server选项,且你指定了ORDER选项,sequence值
    将不会被cache, 不管是否你指定cache或nocache与否。

ORDER
    guarantees that sequence numbers are generated in order of request.
    You may want to use this option if you are using the sequence
    numbers as timestamps. Guaranteeing order is usually not important
    for sequences used to generate primary keys.

    保证sequence数值按照请求的顺序生成,如果你正在使用sequence numbers
    作为 timestamps,你可能想要使用这个选项。保证顺序对于用于生成PK的
    sequence而言常常不是重要的。ORDER对于性能有一定的影响。

NOORDER
    does not guarantee sequence numbers are generated in order of
    request.

    If you omit both the ORDER and NOORDER options, Oracle chooses
    NOORDER by default. Note that the ORDER option is only necessary to
    guarantee ordered generation if you are using Oracle with the
    Parallel Server option in parallel mode. If you are using exclusive
    mode, sequence numbers are always generated in order.
   
    不按照顺序生成sequence, 如果忽略ORDER或NOORDER选项,默认是NOORDER,
    注意,仅仅在parallel mode下使用Oracle parallel server 选项的情况下,
    ORDER选项对于保证顺序生成序列号才有必要。 如果你正在使用exclusive mode,
    sequence 号码总是按照顺序的。


PREREQUISITES:
    To create a sequence in your own schema, you must have CREATE
    SEQUENCE privilege.

    先决条件: 要建立sequence,必须有create sequence的权限。

    To create a sequence in another user's schema, you must have CREATE
    ANY SEQUENCE privilege. If you are using Trusted Oracle in DBMS MAC
    mode, your DBMS label must dominate the creation label of the owner
    of the schema to contain the sequence.

    要在其他用户schema下创建sequence,需要有create any sequence权限。

 


9.2.9  可恢复的空间分配(resumable space allocation) 

我们经常因为没有规划好而在做大操作接近尾声的时候,发现空间不足,全部回滚,
这非常让人沮丧,在ORACLE10g 中引入了一个新特性: 可恢复的空间分配(resumable
space allocation), 可以让我们发现空间不足等问题时,可以让操作暂停并挂起,
在加入新的空间后继续。

因为某用户操作挂起时,会导致用户获得的资源长时间不能释放,因此能够进行挂
起的用户需要有 resumable 的系统权限。 

需要注意,发生空间耗尽问题后,如果一定时间内不处理,插入操作还是要失败,这
个时间段就叫做挂起超时。 挂起时间由 resumable_timeout决定(默认是0秒)。

SQL> grant resumable to tony ;
SQL> alter system set resumable_timeout=0 ;
SQL> insert into test1 select * from test2 ; 
发现空间不足时,会报错空间不足。接下来我们启用可恢复操作。

SQL> alter system set resumable_timeout=10 ;
SQL> insert into test1 select * from test2 ; 
如果空间不足,然后我们在10秒内没有处理完,那么会显示挂起超时及空间不足
的错误。

下面我们在session级别显示启用可恢复操作,同时指定timeout为600秒.
SQL> alter session enable resumable timeout 600 ;
SQL> insert into test1 select * from test2 ; 
查看dba_resumable视图可以看到相关挂起信息。
然后我们加入空间。
回到插入操作的挂起的session上,命令很快执行完毕。

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

转载于:http://blog.itpub.net/35489/viewspace-687739/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值