Tibero insert all Sequence 질문 드립니다. 0 7 6,540

by 동네시비꾼 [Tibero] insertall sequence [2019.09.02 19:30:02]


안녕하세요 현재 Tibero DB 포팅 작업을 진행하고 있습니다.

시퀀스가 있는 테이블에 insert all이 잘 되지 않아 도움 구하고자 게시물 남깁니다.

mysql/postgres의 경우,

insert into table ( t1 ) values (1), values(2), values(3)

과 같은 SQL문이 지원이 되었습니다.

Tibero의 경우, 오라클과 많이 유사하여 오라클 문법으로 확인한 후, Tibero DB로 테스트해봤습니다.

insert all 

into table ( t1 ) values ( 1 )

into table ( t1 ) values ( 2 ) 

into table ( t1 ) values ( 3 ) 

select 1 from dual;

 

위와 같은 SQL 문은 이상없습니다.

그러나

insert all 

into table ( t1 ) values ( seq.nextval )

into table ( t1 ) values ( seq.nextval ) 

into table ( t1 ) values ( seq.nextval ) 

select 1 from dual;

위와 같은 SQL문장은 seq.nextval이 제 기능을 하지 못하더라구요..

 

오라클의 경우 아래 URL과 같이 해결된다는데, 티베로는 해당되지 않는 것 같더라구요..

https://stackoverflow.com/questions/31968093/inserting-multiple-rows-with-sequence-in-oracle?noredirect=1&lq=1

 

insert all with sequence 문제를 어떻게 해결하면 좋을까요..

현재 tibero 5를 사용하며, mybatis를 사용하고 있습니다.

글 읽어주셔서 감사드립니다. 또한 댓글 달아주시고 도움주시는 분들께 미리 감사드립니다.

도와주시면 정말 감사하겠습니다.

by 동네시비꾼 [2019.09.02 19:43:14]

https://m.blog.naver.com/PostView.nhn?blogId=necall&logNo=80187965665&proxyReferer=https%3A%2F%2Fwww.google.com%2F

 

이것도 아이디어가 좋은것 같습니다.. Thread-Safe하게 인서트할 수 있지만, Application과 DBMS의 Round Trip이 결국 Single 처리와 같은 것 같습니다만.. 

 insert into table ( t1 ) values (1), values(2), values(3) 

위와 같이 한방에 보낼 방법이 없을까요........;


by 동네시비꾼 [2019.09.02 20:06:58]

방법을 찾았습니다.

https://stackoverflow.com/questions/228221/how-can-i-insert-multiple-rows-into-oracle-with-a-sequence-value

해당글과 같이 진행하니까 되었습니다.

insert into service_code (code,serv_info_seq_id,name)
select service_code_seq.nextval, a, b
from
(SELECT 1 as a, 'SOME VALUE' as b FROM DUAL
 UNION ALL
 SELECT 1, 'ANOTHER VALUE' FROM DUAL)

select ~~ from dual 부분을 foreach 돌리며 될 것 같습니다..

 

근데 궁금한게... 성능이 정말 궁금하네요.. 한번 확인해봐야겠습니다..

 

감사합니다!!!


by 동네시비꾼 [2019.09.02 20:26:01]
Execution Plan
--------------------------------------------------------------------------------
   1  INSERT: SERVICE_CODE (Cost:211, %CPU:0, Rows:8) 
   2    UNION ALL (Cost:211, %CPU:0, Rows:8) 
   3      UNION ALL (Cost:105, %CPU:0, Rows:4) 
   4        UNION ALL (Cost:52, %CPU:0, Rows:2) 
   5          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
   6          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
   7        UNION ALL (Cost:52, %CPU:0, Rows:2) 
   8          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
   9          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
  10      UNION ALL (Cost:105, %CPU:0, Rows:4) 
  11        UNION ALL (Cost:52, %CPU:0, Rows:2) 
  12          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
  13          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
  14        UNION ALL (Cost:52, %CPU:0, Rows:2) 
  15          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 
  16          DPV: _VT_DUAL (Cost:26, %CPU:0, Rows:1) 

확실히 union all 자체에 cost가 필요하네요.. ㅠㅠㅠㅠ


by 동네시비꾼 [2019.09.03 16:56:16]

위와 같이 했더니 

	<insert id="insertServiceCodeList" parameterType="serviceCodeVo" useGeneratedKeys="true" keyColumn="code" keyProperty="code">
		insert into service_code (
			code,
			serv_info_seq_id,
			name,
			default_key_type,
			cre_date,
			creator,
			description,
			state
		) select 
			service_code_seq.nextval,
			servInfoSeqId,
			name,
			defaultKeyType,
			systimestamp,
			creator,
			description,
			0
		from (
		<foreach collection="list" item="item" separator="union all"> 
			select
				#{item.servInfoSeqId} servInfoSeqId,
				#{item.name} name,
				#{item.defaultKeyType} defaultKeyType,
				#{item.creator} creator,
				#{item.description} description
			from
				dual
		</foreach>
		)
	</insert>

useGeneratedKeys가 제기능을 하지 못하고 syntax error 뿜뿜...

usegenKey 관련 프로퍼티 지우면 정상가동되지만.. sequence id write back이 안되네요 ㅠㅠ..


by 동네시비꾼 [2019.09.04 14:14:20]

Tibero 6.0에서는 아래의 URL이 잘됩니다. 참고하세요~

https://stackoverflow.com/questions/31968093/inserting-multiple-rows-with-sequence-in-oracle?noredirect=1&lq=1

문제는 5.0이네요....

 


by 동네시비꾼 [2019.09.04 16:54:17]
create or replace function get_seq2 
return number is
  v_num number;
begin
  select postal_code_seq.nextval
  into v_num
  from dual;
  return v_num;
end;
/

해당 function으로 insert all 하면 되네요 ㅋㅋ


by 동네시비꾼 [2019.09.04 18:10:09]

해당 function 응용하면 아래와 같습니다.

create or replace function get_seq( seq_name in varchar2 ) 
return 
  number 
is
  v_num number;
  sql_stmt varchar2(64);
begin
  sql_stmt := 'select ' || seq_name || '.nextval from dual';
  execute immediate sql_stmt into v_num;
  return v_num;
end;
/

 

댓글등록
SQL문을 포맷에 맞게(깔끔하게) 등록하려면 code() 버튼을 클릭하여 작성 하시면 됩니다.
로그인 사용자만 댓글을 작성 할 수 있습니다. 로그인, 회원가입