您的位置:首页 > 数据库 > Oracle

Oracle Sequence Cache 参数说明

2013-12-30 02:09 399 查看
转自 /article/1449214.html

之前整理的一篇文章:

ORACLE SEQUENCE 介绍

http://blog.csdn.net/tianlesoftware/archive/2009/10/30/4745039.aspx

之前整理的一篇文章。 那是还是写blog初期的作品。 2009年10月份的。 转眼一年,写Blog 也比以前成熟了很多。

一. 理论知识

先看一个创建Sequence的语句:

SQL> create sequence seq_tmp

2 increment by 1

3 start with 1

4 nomaxvalue

5 nocycle

6 ;

序列已创建。

相关参数说明:

INCREMENT BY 1 -- 每次加几个

START WITH 1 -- 从1开始计数

NOMAXvalue -- 不设置最大值

NOCYCLE -- 一直累加,不循环

CACHE 10; --设置缓存cache个序列

CURRVAL=返回 sequence的当前值

NEXTVAL=增加sequence的值,然后返回 sequence 值

更多信息,参考Oracle 联机文档:

CACHE CACHE(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)
CACHENote:

CACHENOCACHE NOCACHECACHENOCACHEORDER to guarantee that sequence numbers are generated in order of request. This clause is useful if you are using the sequence numbers as timestamps. Guaranteeing order is usually not important for sequences used to generate primary keys.

ORDER
NOORDER if you do not want to guarantee sequence numbers are generated in order of request. This is the default.

查看user_sequences 表的结构:

SQL> desc user_sequences;

名称 是否为空? 类型

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

SEQUENCE_NAME NOT NULL VARCHAR2(30)

MIN_VALUE NUMBER

MAX_VALUE NUMBER

INCREMENT_BY NOT NULL NUMBER

CYCLE_FLAG VARCHAR2(1)

ORDER_FLAG VARCHAR2(1)

CACHE_SIZE NOT NULL NUMBER

LAST_NUMBER NOT NULL NUMBER

查看刚才创建的序列seq_tmp 的值:

SQL> select * from user_sequences where sequence_name='SEQ_TMP';

SEQUENCE_N MIN_VALUE MAX_VALUE INCREMENT_BY C O CACHE_SIZE LAST_NUMBER

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

SEQ_TMP 1 1.0000E+28 1 N N 20 21

这里有个CACHE_SIZE的值。 我们在创建sequence的时候,启用了cache,但是没有给它值。 所以这里的cache_size 就是系统的模式值。 即20个。

取下一个sequence的值:

SQL> select seq_tmp.nextval from dual;

NEXTVAL

----------

1

SQL> select seq_tmp.nextval from dual;

NEXTVAL

----------

2

查看当前sequence的值:

SQL> select seq_tmp.currval from dual;

CURRVAL

----------

修改Cache 大小:

如果Cache已经指定,我们可以修改Cache 大小。 alter 命令可以修改sequence中除了start 以外的所有参数。

如:

alter sequence emp_sequence cache 100;

select * from user_sequences where sequence_name=upper('emp_sequence');

二. 实验

一个网友RAC 系统上的测试时结果:

nocache:    2100s

cache =1000:   55s

差别很明显。

测试一:

SQL> create sequence seq_1 nocache;

序列已创建。

SQL> set timing on;

SQL> declare

2 x number;

3 begin

4 for i in 1 .. 10000 loop

5 select seq_1.nextval into x from dual;

6 end loop;

7 end;

8 /

PL/SQL 过程已成功完成。

已用时间: 00: 00: 02.26

测试二:

SQL> create sequence seq_2 cache 20;

序列已创建。

已用时间: 00: 00: 00.01

SQL> declare

2 x number;

3 begin

4 for i in 1 .. 10000 loop

5 select seq_2.nextval into x from dual;

6 end loop;

7 end;

8 /

PL/SQL 过程已成功完成。

已用时间: 00: 00: 00.46

测试三:

SQL> create sequence seq_3 cache 100;

序列已创建。

已用时间: 00: 00: 00.05

SQL> declare

2 x number;

3 begin

4 for i in 1 .. 10000 loop

5 select seq_3.nextval into x from dual;

6 end loop;

7 end;

8 /

PL/SQL 过程已成功完成。

已用时间: 00: 00: 00.37

测试四:

SQL> create sequence seq_4 cache 1000;

序列已创建。

已用时间: 00: 00: 00.04

SQL> declare

2 x number;

3 begin

4 for i in 1 .. 40000 loop

5 select seq_4.nextval into x from dual;

6 end loop;

7 end;

8 /

PL/SQL 过程已成功完成。

已用时间: 00: 00: 01.31

SQL> declare

2 x number;

3 begin

4 for i in 1 .. 40000 loop

5 select seq_1.nextval into x from dual;

6 end loop;

7 end;

8 /

PL/SQL 过程已成功完成。

已用时间: 00: 00: 09.33

SQL>

小结:

在自己的本本上测试的,Oracle 11gR2. 单Instance数据库单会话循环不间断取1-4万个值。

nocache: 2.26s 10000   

cache:20 0.46s 10000

cache:100 0.37s 10000

cache:1000 1.31s 40000

nocache: 9.33s 40000

基本上cache 大于20的时候性能基本可以接受,nocache的时候性能确实很差.

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

以下是本人添加的官网文档说明

Sequence Pseudocolumns

A sequence is a schema object that can generate unique sequential values. These values are often used for primary and unique keys. You can refer to sequence values in SQL statements with these pseudocolumns:

CURRVAL
: Returns the current value of a sequence

NEXTVAL
: Increments the sequence and returns the next value

You must qualify
CURRVAL
and
NEXTVAL
with the name of the sequence:

sequence.CURRVAL
sequence.NEXTVAL

To refer to the current or next value of a sequence in the schema of another user, you must have been granted either
SELECT
object privilege on the sequence or
SELECT
ANY
SEQUENCE
system privilege, and you must qualify the sequence with the schema containing it:

schema.sequence.CURRVAL
schema.sequence.NEXTVAL

To refer to the value of a sequence on a remote database, you must qualify the sequence with a complete or partial name of a database link:

schema.sequence.CURRVAL@dblink
schema.sequence.NEXTVAL@dblink

A sequence can be accessed by many users concurrently with no waiting or locking.

See Also:

"References to Objects in Remote Databases" for more information on referring to database links

Where to Use Sequence Values

You can use
CURRVAL
and
NEXTVAL
in the following locations:

The select list of a
SELECT
statement that is not contained in a subquery, materialized view, or view

The select list of a subquery in an
INSERT
statement

The
VALUES
clause of an
INSERT
statement

The
SET
clause of an
UPDATE
statement

Restrictions on Sequence Values You cannot use
CURRVAL
and
NEXTVAL
in the following constructs:

A subquery in a
DELETE
,
SELECT
, or
UPDATE
statement

A query of a view or of a materialized view

A
SELECT
statement with the
DISTINCT
operator

A
SELECT
statement with a
GROUP
BY
clause or
ORDER
BY
clause

A
SELECT
statement that is combined with another
SELECT
statement with the
UNION
,
INTERSECT
, or
MINUS
set operator

The
WHERE
clause of a
SELECT
statement

The
DEFAULT
value of a column in a
CREATE
TABLE
or
ALTER
TABLE
statement

The condition of a
CHECK
constraint

Within a single SQL statement that uses
CURRVAL
or
NEXTVAL
, all referenced
LONG
columns, updated tables, and locked tables must be located on the same database.

How to Use Sequence Values

When you create a sequence, you can define its initial value and the increment between its values. The first reference to
NEXTVAL
returns the initial value of the sequence. Subsequent references to
NEXTVAL
increment the sequence value by the defined increment and return the new value. Any reference to
CURRVAL
always returns the current value of the sequence, which is the value returned by the last reference to
NEXTVAL
.

Before you use
CURRVAL
for a sequence in your session, you must first initialize the sequence with
NEXTVAL
. Refer to CREATE SEQUENCE for information on sequences.

Within a single SQL statement containing a reference to
NEXTVAL
, Oracle increments the sequence once:

For each row returned by the outer query block of a
SELECT
statement. Such a query block can appear in the following places:

A top-level
SELECT
statement

An
INSERT
...
SELECT
statement (either single-table or multitable). For a multitable insert, the reference to
NEXTVAL
must appear in the
VALUES
clause, and the sequence is updated once for each row returned by the subquery, even though
NEXTVAL
may be referenced in multiple branches of the multitable insert.

A
CREATE
TABLE
...
AS
SELECT
statement

A
CREATE
MATERIALIZED
VIEW
...
AS
SELECT
statement

For each row updated in an
UPDATE
statement

For each
INSERT
statement containing a
VALUES
clause

For each
INSERT
... [
ALL
|
FIRST
] statement (multitable insert). A multitable insert is considered a single SQL statement. Therefore, a reference to the
NEXTVAL
of a sequence will increase the sequence only once for each input record coming from the
SELECT
portion of the statement. If
NEXTVAL
is specified more than once in any part of the
INSERT
... [
ALL
|
FIRST
] statement, then the value will be the same for all insert branches, regardless of how often a given record might be inserted.

For each row merged by a
MERGE
statement. The reference to
NEXTVAL
can appear in the
merge_insert_clause
or the
merge_update_clause
or both. The
NEXTVALUE
value is incremented for each row updated and for each row inserted, even if the sequence number is not actually used in the update or insert operation. If
NEXTVAL
is specified more than once in any of these locations, then the sequence is incremented once for each row and returns the same value for all occurrences of
NEXTVAL
for that row.

For each input row in a multitable
INSERT
ALL
statement.
NEXTVAL
is incremented once for each row returned by the subquery, regardless of how many occurrences of the
insert_into_clause
map to each row.

If any of these locations contains more than one reference to
NEXTVAL
, then Oracle increments the sequence once and returns the same value for all occurrences of
NEXTVAL
.

If any of these locations contains references to both
CURRVAL
and
NEXTVAL
, then Oracle increments the sequence and returns the same value for both
CURRVAL
and
NEXTVAL
.

Finding the next value of a sequence: Example This example selects the next value of the employee sequence in the sample schema
hr
:

SELECT employees_seq.nextval
FROM DUAL;

Inserting sequence values into a table: Example This example increments the employee sequence and uses its value for a new employee inserted into the sample table
hr.employees
:

INSERT INTO employees
VALUES (employees_seq.nextval, 'John', 'Doe', 'jdoe', '555-1212',
TO_DATE(SYSDATE), 'PU_CLERK', 2500, null, null, 30);

Reusing the current value of a sequence: Example This example adds a new order with the next order number to the master order table. It then adds suborders with this number to the detail order table:

INSERT INTO orders (order_id, order_date, customer_id)
VALUES (orders_seq.nextval, TO_DATE(SYSDATE), 106);

INSERT INTO order_items (order_id, line_item_id, product_id)
VALUES (orders_seq.currval, 1, 2359);

INSERT INTO order_items (order_id, line_item_id, product_id)
VALUES (orders_seq.currval, 2, 3290);

INSERT INTO order_items (order_id, line_item_id, product_id)
VALUES (orders_seq.currval, 3, 2381);


CREATE SEQUENCE

Purpose

Use the
CREATE
SEQUENCE
statement to create a sequence, which is a database object from which multiple users may generate unique integers. You can use sequences to automatically generate primary key values.

When a sequence number is generated, the sequence is incremented, independent of the transaction committing or rolling back. If two users concurrently increment the same sequence, then the sequence numbers each user acquires may have gaps, because sequence numbers are being generated by the other user. One user can never acquire the sequence number generated by another user. After a sequence value is generated by one user, that user can continue to access that value regardless of whether the sequence is incremented by another user.

Sequence numbers are generated independently of tables, so the same sequence can be used for one or for multiple tables. It is possible that individual sequence numbers will appear to be skipped, because they were generated and used in a transaction that ultimately rolled back. Additionally, a single user may not realize that other users are drawing from the same sequence.

After a sequence is created, you can access its values in SQL statements with the
CURRVAL
pseudocolumn, which returns the current value of the sequence, or the
NEXTVAL
pseudocolumn, which increments the sequence and returns the new value.

Note on Using Sequences with Deferred Segments If you attempt to insert a sequence value into a table that uses deferred segment creation, the first value that the sequence returns will be skipped.

See Also:

Chapter 2, "Pseudocolumns" for more information on using the
CURRVAL
and
NEXTVAL


"How to Use Sequence Values" for information on using sequences

ALTER SEQUENCE or DROP SEQUENCE for information on modifying or dropping a sequence

Prerequisites

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

To create a sequence in another user's schema, you must have the
CREATE
ANY
SEQUENCE
system privilege.

Syntax

create_sequence::=



Description of the illustration create_sequence.gif

Semantics

schema

Specify the schema to contain the sequence. If you omit
schema
, then Oracle Database creates the sequence in your own schema.

sequence

Specify the name of the sequence to be created. The name must satisfy the requirements listed in "Database Object Naming Rules".

If you specify none of the following clauses, then you create an ascending sequence that starts with 1 and increases by 1 with no upper limit. Specifying only
INCREMENT
BY
-1 creates a descending sequence that starts with -1 and decreases with no lower limit.

To create a sequence that increments without bound, for ascending sequences, omit the
MAXVALUE
parameter or specify
NOMAXVALUE
. For descending sequences, omit the
MINVALUE
parameter or specify the
NOMINVALUE
.

To create a sequence that stops at a predefined limit, for an ascending sequence, specify a value for the
MAXVALUE
parameter. For a descending sequence, specify a value for the
MINVALUE
parameter. Also specify
NOCYCLE
. Any attempt to generate a sequence number once the sequence has reached its limit results in an error.

To create a sequence that restarts after reaching a predefined limit, specify values for both the
MAXVALUE
and
MINVALUE
parameters. Also specify
CYCLE
.

INCREMENT BY Specify the interval between sequence numbers. This integer value can be any positive or negative integer, but it cannot be 0. This value can have 28 or fewer digits for an ascending sequence and 27 or fewer digits for a descending sequence. The absolute of this value must be less than the difference of
MAXVALUE
and
MINVALUE
. If this value is negative, then the sequence descends. If the value is positive, then the sequence ascends. If you omit this clause, then the interval defaults to 1.

START WITH Specify the first sequence number to be generated. Use this clause 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 minimum value of the sequence. For descending sequences, the default value is the maximum value of the sequence. This integer value can have 28 or fewer digits for positive values and 27 or fewer digits for negative values.

Note:

This value is not necessarily the value to which an ascending or descending cycling sequence cycles after reaching its maximum or minimum value, respectively.
MAXVALUE Specify the maximum value the sequence can generate. This integer value can have 28 or fewer digits for positive values and 27 or fewer digits for negative values.
MAXVALUE
must be equal to or greater than
START
WITH
and must be greater than
MINVALUE
.

NOMAXVALUE Specify
NOMAXVALUE
to indicate a maximum value of 1028-1 for an ascending sequence or -1 for a descending sequence. This is the default.

MINVALUE Specify the minimum value of the sequence. This integer value can have 28 or fewer digits for positive values and 27 or fewer digits for negative values.
MINVALUE
must be less than or equal to
START
WITH
and must be less than
MAXVALUE
.

NOMINVALUE Specify
NOMINVALUE
to indicate a minimum value of 1 for an ascending sequence or -(1027 -1) for a descending sequence. This is the default.

CYCLE Specify
CYCLE
to indicate 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 value.

NOCYCLE Specify
NOCYCLE
to indicate that the sequence cannot generate more values after reaching its maximum or minimum value. This is the default.

CACHE Specify how many values of the sequence the database preallocates and keeps in memory for faster access. This integer value can have 28 or fewer digits. 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. You cannot cache more values than will fit in a given cycle of sequence numbers. Therefore, the maximum value allowed for
CACHE
must be less than the value determined by the following formula:

(CEIL (MAXVALUE - MINVALUE)) / ABS (INCREMENT)

If a system failure occurs, then all cached sequence values that have not been used in committed DML statements are lost. The potential number of lost values is equal to the value of the
CACHE
parameter.

Note:

Oracle recommends using the
CACHE
setting to enhance performance if you are using sequences in an Oracle Real Application Clusters environment.
NOCACHE Specify
NOCACHE
to indicate that values of the sequence are not preallocated. If you omit both
CACHE
and
NOCACHE
, then the database caches 20 sequence numbers by default.

ORDER Specify
ORDER
to guarantee that sequence numbers are generated in order of request. This clause is useful if you are using the sequence numbers as timestamps. Guaranteeing order is usually not important for sequences used to generate primary keys.

ORDER
is necessary only to guarantee ordered generation if you are using Oracle Real Application Clusters. If you are using exclusive mode, then sequence numbers are always generated in order.

NOORDER Specify
NOORDER
if you do not want to guarantee sequence numbers are generated in order of request. This is the default.

Example

Creating a Sequence: Example The following statement creates the sequence
customers_seq
in the sample schema
oe
. This sequence could be used to provide customer ID numbers when rows are added to the
customers
table.

CREATE SEQUENCE customers_seq
START WITH     1000
INCREMENT BY   1
NOCACHE
NOCYCLE;

The first reference to
customers_seq.nextval
returns 1000. The second returns 1001. Each subsequent reference will return a value 1 greater than the previous reference.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: