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 也比以前成熟了很多。
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.
查看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');
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的时候性能确实很差.
----------------------------------------------------------------------------------------
以下是本人添加的官网文档说明
You must qualify
To refer to the current or next value of a sequence in the schema of another user, you must have been granted either
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:
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
The select list of a
The select list of a subquery in an
The
The
Restrictions on Sequence Values You cannot use
A subquery in a
A query of a view or of a materialized view
A
A
A
The
The
The condition of a
Within a single SQL statement that uses
Before you use
Within a single SQL statement containing a reference to
For each row returned by the outer query block of a
A top-level
An
A
A
For each row updated in an
For each
For each
For each row merged by a
For each input row in a multitable
If any of these locations contains more than one reference to
If any of these locations contains references to both
Finding the next value of a sequence: Example This example selects the next value of the employee sequence in the sample schema
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
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:
Use the
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
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
"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
To create a sequence in another user's schema, you must have the
Syntax
create_sequence::=
Description of the illustration create_sequence.gif
Semantics
schema
Specify the schema to contain the sequence. If you omit
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
To create a sequence that increments without bound, for ascending sequences, omit the
To create a sequence that stops at a predefined limit, for an ascending sequence, specify a value for the
To create a sequence that restarts after reaching a predefined limit, specify values for both the
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
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.
NOMAXVALUE Specify
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.
NOMINVALUE Specify
CYCLE Specify
NOCYCLE Specify
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
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
Note:
Oracle recommends using the
NOCACHE Specify
ORDER Specify
NOORDER Specify
Example
Creating a Sequence: Example The following statement creates the sequence
The first reference to
之前整理的一篇文章:
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.
ORDERNOORDER 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
CURRVALand
NEXTVALwith 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
SELECTobject privilege on the sequence or
SELECT
ANY
SEQUENCEsystem 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 useCURRVALand
NEXTVALin the following locations:
The select list of a
SELECTstatement that is not contained in a subquery, materialized view, or view
The select list of a subquery in an
INSERTstatement
The
VALUESclause of an
INSERTstatement
The
SETclause of an
UPDATEstatement
Restrictions on Sequence Values You cannot use
CURRVALand
NEXTVALin the following constructs:
A subquery in a
DELETE,
SELECT, or
UPDATEstatement
A query of a view or of a materialized view
A
SELECTstatement with the
DISTINCToperator
A
SELECTstatement with a
GROUP
BYclause or
ORDER
BYclause
A
SELECTstatement that is combined with another
SELECTstatement with the
UNION,
INTERSECT, or
MINUSset operator
The
WHEREclause of a
SELECTstatement
The
DEFAULTvalue of a column in a
CREATE
TABLEor
ALTER
TABLEstatement
The condition of a
CHECKconstraint
Within a single SQL statement that uses
CURRVALor
NEXTVAL, all referenced
LONGcolumns, 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 toNEXTVALreturns the initial value of the sequence. Subsequent references to
NEXTVALincrement the sequence value by the defined increment and return the new value. Any reference to
CURRVALalways returns the current value of the sequence, which is the value returned by the last reference to
NEXTVAL.
Before you use
CURRVALfor 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
SELECTstatement. Such a query block can appear in the following places:
A top-level
SELECTstatement
An
INSERT...
SELECTstatement (either single-table or multitable). For a multitable insert, the reference to
NEXTVALmust appear in the
VALUESclause, and the sequence is updated once for each row returned by the subquery, even though
NEXTVALmay be referenced in multiple branches of the multitable insert.
A
CREATE
TABLE...
AS
SELECTstatement
A
CREATE
MATERIALIZED
VIEW...
AS
SELECTstatement
For each row updated in an
UPDATEstatement
For each
INSERTstatement containing a
VALUESclause
For each
INSERT... [
ALL|
FIRST] statement (multitable insert). A multitable insert is considered a single SQL statement. Therefore, a reference to the
NEXTVALof a sequence will increase the sequence only once for each input record coming from the
SELECTportion of the statement. If
NEXTVALis 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
MERGEstatement. The reference to
NEXTVALcan appear in the
merge_insert_clauseor the
merge_update_clauseor both. The
NEXTVALUEvalue 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
NEXTVALis 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
NEXTVALfor that row.
For each input row in a multitable
INSERT
ALLstatement.
NEXTVALis incremented once for each row returned by the subquery, regardless of how many occurrences of the
insert_into_clausemap 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
CURRVALand
NEXTVAL, then Oracle increments the sequence and returns the same value for both
CURRVALand
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
PurposeUse the
CREATE
SEQUENCEstatement 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
CURRVALpseudocolumn, which returns the current value of the sequence, or the
NEXTVALpseudocolumn, 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
CURRVALand
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
SEQUENCEsystem privilege.
To create a sequence in another user's schema, you must have the
CREATE
ANY
SEQUENCEsystem 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
MAXVALUEparameter or specify
NOMAXVALUE. For descending sequences, omit the
MINVALUEparameter or specify the
NOMINVALUE.
To create a sequence that stops at a predefined limit, for an ascending sequence, specify a value for the
MAXVALUEparameter. For a descending sequence, specify a value for the
MINVALUEparameter. 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
MAXVALUEand
MINVALUEparameters. 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
MAXVALUEand
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.
MAXVALUEmust be equal to or greater than
START
WITHand must be greater than
MINVALUE.
NOMAXVALUE Specify
NOMAXVALUEto 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.
MINVALUEmust be less than or equal to
START
WITHand must be less than
MAXVALUE.
NOMINVALUE Specify
NOMINVALUEto indicate a minimum value of 1 for an ascending sequence or -(1027 -1) for a descending sequence. This is the default.
CYCLE Specify
CYCLEto 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
NOCYCLEto 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
CACHEmust 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
CACHEparameter.
Note:
Oracle recommends using the
CACHEsetting to enhance performance if you are using sequences in an Oracle Real Application Clusters environment.
NOCACHE Specify
NOCACHEto indicate that values of the sequence are not preallocated. If you omit both
CACHEand
NOCACHE, then the database caches 20 sequence numbers by default.
ORDER Specify
ORDERto 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.
ORDERis 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
NOORDERif 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_seqin the sample schema
oe. This sequence could be used to provide customer ID numbers when rows are added to the
customerstable.
CREATE SEQUENCE customers_seq START WITH 1000 INCREMENT BY 1 NOCACHE NOCYCLE;
The first reference to
customers_seq.nextvalreturns 1000. The second returns 1001. Each subsequent reference will return a value 1 greater than the previous reference.
相关文章推荐
- oracle的sequence出现gap的问题
- Oracle增加表空间大小的四种方法
- Oracle数据库学习总结
- centos6.0 x64下安装oracle11gR2
- Oracle【ORA-01513】ora-01513 invalid current time returned by operating system
- oracle执行计划
- oracle scott用户解锁
- oracle数据库性能
- Oracle 11gR2 exp命令无法导出空表
- ORACLE 和 Weblogic info
- oracle PL/SQL
- oracle 使用连接符 报错 ORA-01034 ORA-27101
- 2013年周历(Oracle)
- Oracle 学习Record
- 评估和提升oracle数据库性能的主要工具
- oracle注册
- 甲骨文客户端配置 ORALCE PLSQL 远程 连接
- 用AutoConfig管理Oracle 电子商务套件第12版的系统配置
- oracle 10g 学习之.NET使用Oracle数据库(14)
- Oracle常用函数:IDCARD15TO18() 身份证15位转18位函数