Oracle sequence caching

前端 未结 3 764
心在旅途
心在旅途 2021-02-15 18:22

I am trying to implement a sequence in an Oracle database to act as a surrogate key creator for a table. For performance reasons, I want this sequence to be cached. I have rea

相关标签:
3条回答
  • 2021-02-15 18:46

    Say the cache has the values 101-200. The value written on disk is 201. Your insert uses 101-150. Instance goes down. Instance starts up. Next time the sequence is used 201-300 will be cached.

    0 讨论(0)
  • 2021-02-15 18:47

    Turns out that this is not (or no longer true). Shut down and restart of instance does not lose cached values. Simple test with cache = 1000.

    SQL> select ordered.currval from dual;

    CURRVAL

        22
    

    SQL> select unordered.currval from dual

    CURRVAL

        24
    

    SQL> SHUTDOWN IMMEDIATE SQL> STARTUP

    NEXTVAL

        23
    

    SQL> select unordered.nextval from dual;

    NEXTVAL

        25
    

    Also, ALL_SEQUENCES.LAST_NUMBER does not hold the last last number provided by the sequence except on startup and before first NEXTVAL. After first NEXTVAL, it holds last number served plus CACHE_SIZE. This does not change until new cache is generated. However, on shutdown, it apparently gets reset to just the last number served.

    0 讨论(0)
  • 2021-02-15 18:57

    No, this will not be the case.

    Your sequence will continue at 101, the values between 50 and 100 will be missing.

    The only reason to disable sequence caching is when trying to avoid gaps in your sequence, which is not relevant for most Primary Keys.

    You might be interested in this article, which states that

    The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.

    0 讨论(0)
提交回复
热议问题