問題
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選項,確保先到的請求時鐘排序在前面。