Oracle Sequence Nocache

chncaesar發表於2013-12-03
預設情況下,建立Sequence時,緩衝(cache)是20.意即:20次.nextval才會觸發一次對sys.seq$的更新(update)操作。這樣就能提高SQL執行效能。
當使用者指定nocache時,如:
create sequence test_seq nocache;
每一次test_seq.nextval都會觸發一次sys.seq$更新(update)。
我們可以比較下cache 20 和nocache的效能:
create sequence test_seq1 nocache;
create sequence test_seq2 cache 20;

create table tt1 (id number);
create table tt2 (id number);

declare
  v_start_time number :=dbms_utility.get_cpu_time; 
  v_time1 number;
begin
  insert into tt1 select test_seq1.nextval from dual connect by level <= 100000;
  v_time1 := dbms_utility.get_cpu_time;
  dbms_output.put_line('Nocache sequence: ' || (v_time1 - v_start_time) / 100 || ' seconds');
  insert into tt2 select test_seq2.nextval from dual connect by level <= 100000;
  dbms_output.put_line('Caceh 20 sequence: ' || (dbms_utility.get_cpu_time - v_time1) / 100 || ' seconds');
end;

anonymous block completed
Nocache sequence: 28.37 seconds
Caceh 20 sequence: 1.83 seconds

效能差異還是相當明顯的。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/638844/viewspace-1062022/,如需轉載,請註明出處,否則將追究法律責任。

相關文章