Oracle创建Sequence时Order/NoOrder选项

来源:互联网 发布:java中死循环语句 编辑:程序博客网 时间:2024/05/21 16:23

问题

Oracle创建Sequence时会有Order/NoOrder两个选项,那么到底什么场景用到Order,什么场景又用到NoOrder呢?

官方文档

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. 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. 

分析

Order:

保证序列号按请求顺序产生。如果想以序列号作为timestamp(时间戳)类型的话,可以采用该选项。对于将序列用于生成主键来说,约定顺序通常并不重要。

NOORDER:
此选项跟Order相对应,并不按照请求的顺序进行生成。

举例

双CPU对同一个oracle DB 中的 ABC sequence申请序号时, 这时就有两个请求A和B,假设A请求在前B在后, 现在 ABC序列中的值为9。 如果添加了ORDER选项,那么一定是A请求到9, B请求到10。但如果没有添加此选项,则有可能B请求到9, A请求到 10。

总结

无论使用哪个选项,sequence中生成的数据都是唯一的。因此,我们可以得出结论,在用sequence中的数据作为ID时,无论选择哪个选项都能确保ID的唯一性。但如果,用sequence中的数据作为时间戳时,则需要使用Order选项,确保先到的请求时钟排序在前面。

1 0
原创粉丝点击