Generating Unique Sequence Numbers Across Shards
You can generate globally unique sequence numbers across shards for non-primary key columns, and it is handled by the sharded database.
You may need to generate unique IDs for non-primary key columns, for example order_id, when the customer_id is the sharding key. For this case among others, this feature lets you generate unique sequence numbers across shards, while not requiring you to manage the global uniqueness of a given non-primary key column in your application.
This functionality is supported by the SHARDED SEQUENCE
object. A
sharded sequence is created on the shard catalog but has an instance on each shard. Each
instance generates monotonically increasing numbers that belong to a range which does not
overlap with ranges used on other shards. Therefore, every generated number is globally
unique.
A sharded sequence can be used, for example, to generate a unique order number for a table sharded by a customer ID. An application that establishes a connection to a shard using the customer ID as a key can use a local instance of the sharded sequence to generate a globally unique order number.
Note that the number generated by a sharded sequence cannot be immediately used as a sharding key for a new row being inserted into this shard, because the key value may belong to another shard and the insert will result in an error. To insert a new row, the application should first generate a value of the sharding key and then use it to connect to the appropriate shard. A typical way to generate a new value of the sharding key would be use a regular (non-sharded) sequence on the shard catalog.
If a single sharding key generator becomes a bottleneck, a sharded sequence can be used for this purpose. In this case, an application should connect to a random shard (using the global service without specifying the sharding key), get a unique key value from a sharded sequence, and then connect to the appropriate shard using the key value.
To support this feature, the SEQUENCE
object clauses,
SHARD
and NOSHARD
, are included in the
SEQUENCE
object DDL syntax, as shown in the following
CREATE
statement syntax.
CREATE | ALTER SEQUENCE [ schema. ]sequence
[ { INCREMENT BY | START WITH } integer
| { MAXVALUE integer | NOMAXVALUE }
| { MINVALUE integer | NOMINVALUE }
| { CYCLE | NOCYCLE }
| { CACHE integer | NOCACHE }
| { ORDER | NOORDER }
| { SCALE {EXTEND | NOEXTEND} | NOSCALE}
| { SHARD {EXTEND | NOEXTEND} | NOSHARD}
]
NOSHARD
is the default for a sequence. If the SHARD
clause is specified, this property is registered in the sequence object’s dictionary table, and is shown using the DBA_SEQUENCES
, USER_SEQUENCES
, and ALL_SEQUENCES
views.
When SHARD
is specified, the EXTEND
and NOEXTEND
clauses define the behavior of a sharded sequence. When EXTEND
is specified, the generated sequence values are all of length (x+y), where x is the length of a SHARD
offset of size 4 (corresponding to the width of the maximum number of shards, that is, 1000) affixed at beginning of the sequence values, and y is the maximum number of digits in the sequence MAXVALUE
/MINVALUE
.
The default setting for the SHARD
clause is NOEXTEND
. With the NOEXTEND
setting, the generated sequence values are at most as wide as the maximum number of digits in the sequence MAXVALUE
/MINVALUE
. This setting is useful for integration with existing applications where sequences are used to populate fixed width columns. On invocation of NEXTVAL
on a sequence with SHARD NOEXTEND
specified, a user error is thrown if the generated value requires more digits of representation than the sequence’s MAXVALUE
/MINVALUE
.
If the SCALE
clause is also specified with the SHARD
clause, the sequence generates scalable values within a shard for multiple instances and sessions, which are globally unique. When EXTEND
is specified with both the SHARD
and SCALE
keywords, the generated sequence values are all of length (x+y+z), where x is the length of a prepended SHARD
offset of size 4, y is the length of the scalable offset (default 6), and z is the maximum number of digits in the sequence MAXVALUE
/MINVALUE
.
Note:
When using the SHARD
clause, do not specify ORDER
on the sequence. Using SHARD
generates globally unordered values. If ORDER
is required, create the sequences locally on each node.
The SHARD
keyword will work in conjunction with CACHE
and NOCACHE
modes of operation.
See Also:
Parent topic: Sharded Database Schema Design