Merge into a sequence of reference limit

Copyright attribution: Tiankai Technology
This link: https://blog.csdn.net/dbs_service/article/details/102760824

Encountered the phenomenon:
the PLSQL block service is running normally, but the main construction of the table S_VipShopStock number of serious jump, run for the first time max (id) 600, the next execution id 1200, after reconstruction of the sequence and the cache parameter to empty We can not solve the problem.

Original service (2 seconds executed once):

begin
merge into mbs7_oms.XS_VipShopStock a using
(Select
c.stylecode,
c.warecode,
sum(b.amount) qty,
0,
c.websitecode,
33
From mbs7_oms.cg_batch a
Join mbs7_oms.cg_batchsub b
join mbs7_oms.xs_ware c
on b.warecode = c.warecode
and c.prodlinecode = '33' On a.batchcode = b.batchcode
Where b.amount > 0
And a.status In (0, 1)
And a.arrivaldate > Sysdate
group by c.stylecode, c.warecode, c.websitecode) b
on(a.warecode=b.warecode)
WHEN MATCHED THEN 
update set a.stockqty=b.qty where a.stockqty<>b.qty
WHEN NOT MATCHED THEN
INSERT values(SEQ_XS_VipShopStock.Nextval,b.stylecode,b.warecode,b.qty,0,b.stylecode,33);
commit;

View original defined sequence:

create sequence SEQ_XS_VIPSHOPSTOCK
minvalue 1
maxvalue 1000000000000000
start with 1
increment by 1
cache 20

Later changed to:

create sequence SEQ_XS_VIPSHOPSTOCK
minvalue 1
maxvalue 1000000000000000
start with 1
increment by 1
nocache
order;

The problem is still!

Analysis:
Use Merge Into call is similar to the pre-compiled, will direct the call to the appropriate value assigned combination Sequence, regardless of whether the matched or not matched the success of the implementation. The use of a function will be closed in the future, because they can not know the value of pre-compile time, so no pretreatment, and therefore will not waste sequence of values. However, the use of function is concerned, it will bring another problem, because the performance increases a bit gratuitous call. So, according to their actual situation, to choose their own is the best.

Solution:
Create a serial number transfer function:

create or replace function get_sequence_nextval(f_schema in varchar2, f_sequence_name in  varchar2) return number  is  
  v_nextval         number;  
begin  
  execute immediate 'select ' || f_schema || '.'||f_sequence_name||'.nextval from dual' into v_nextval;  
 return v_nextval;  
exception  
  when others then  
   raise_application_error(sqlcode,sqlerrm);  
end;  

Change the service reference function:

begin
    merge into mbs7_oms.XS_VipShopStock a using
     (Select
           c.stylecode,
           c.warecode,
           sum(b.amount) qty,
           0,
           c.websitecode,
           33
      From mbs7_oms.cg_batch a
      Join mbs7_oms.cg_batchsub b
      join mbs7_oms.xs_ware c
        on b.warecode = c.warecode
       and c.prodlinecode = '33' On a.batchcode = b.batchcode
     Where b.amount > 0
       And a.status In (0, 1)
       And a.arrivaldate > Sysdate
     group by  c.stylecode, c.warecode, c.websitecode) b
     on(a.warecode=b.warecode)
     WHEN  MATCHED THEN
        update set a.stockqty=b.qty where a.stockqty<>b.qty
      WHEN  NOT MATCHED THEN
        INSERT  values(get_sequence_nextval('MBS7_OMS','SEQ_XS_VIPSHOPSTOCK'),b.stylecode,b.warecode,b.qty,0,b.websitecode,33);
    commit;  

Problem solving, serial numbers did not jump phenomenon!


DBA cases more updates, follow us CSDN blog!
Https://topdbs.blog.csdn.net

Guess you like

Origin blog.csdn.net/dbs_service/article/details/102760824