Oracle RAC and sequences

后端 未结 3 2210
萌比男神i
萌比男神i 2020-12-02 16:09

I have various database applications that use sequences, I´m migrating these applications to Oracle RAC from 10g without RAC to 11g with RAC. I need ordered sequences and ga

3条回答
  •  抹茶落季
    2020-12-02 16:50

    Summary

    CACHE can significantly improve the performance of a sequence that uses ORDER, even on RAC.

    It's still not as fast as NOORDER, but it can be surprisingly close. Especially if the sequence is only used on one of the nodes at a time.

    Test Case

    SQL> create sequence cache_order cache 20 order;
    
    Sequence created.
    
    SQL> create sequence cache_noorder cache 20 noorder;
    
    Sequence created.
    
    SQL> create sequence nocache_order nocache order;
    
    Sequence created.
    
    SQL> create sequence nocache_noorder nocache noorder;
    
    Sequence created.
    
    SQL> set timing on
    SQL> declare
      2     v_temp number;
      3  begin
      4     for i in 1 .. 100000 loop
      5             v_temp := cache_order.nextval;
      6     end loop;
      7  end;
      8  /
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:08.44
    SQL> declare
      2     v_temp number;
      3  begin
      4     for i in 1 .. 100000 loop
      5             v_temp := cache_noorder.nextval;
      6     end loop;
      7  end;
      8  /
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:07.46
    SQL> declare
      2     v_temp number;
      3  begin
      4     for i in 1 .. 100000 loop
      5             v_temp := nocache_order.nextval;
      6     end loop;
      7  end;
      8  /
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:35.15
    SQL> declare
      2     v_temp number;
      3  begin
      4     for i in 1 .. 100000 loop
      5             v_temp := nocache_noorder.nextval;
      6     end loop;
      7  end;
      8  /
    
    PL/SQL procedure successfully completed.
    
    Elapsed: 00:00:35.10
    

    Test Case Notes

    My results were obtained on a 2-node RAC. Only one result set is shown, but I ran the test case multiple times, on different databases, and obtained almost identical results.

    I also ran the tests concurrently, on different nodes. The CACHE still significantly improves ORDER, although the CACHE NOORDER is more than twice as fast as CACHE ORDER.

    I've also noticed similar behavior in other environments in the past, although I do not have any results for them.

    Why?

    I don't understand why CACHE would make so much of a difference when ORDER is used. The amount of time to generate a number should be irrelevant compared to the time to send data over a network. This makes me think that either Oracle is using a poor algorithm, or my test case is wrong. (If anyone can find a problem with my test case, please let me know.)

    Also, this answer only discusses the time to generate the sequence. There may be other benefits of using NOORDER. For example, reduced index contention, as described here.

提交回复
热议问题