oracle inserts multiple pieces of data at a time (insert all)

question

In the company's project, there is a function that needs to insert a lot of data into the database each time it is used, which causes the page to wait for a long time to get the result.
Database: oracle11g
id: Self-incrementing sequence is used. In
each loop, the sequence will be queried once, and then a piece of data will be inserted. The performance is very low.

Improve

It is changed to insert multiple pieces of data at one time, and the id is automatically set by the trigger, instead of querying the sequence every time, the efficiency is greatly improved.

How to insert multiple entries in oracle at a time

In oracle, it is not supported to spell multiple records directly behind it like mysql. There are two methods in oracle to achieve the effect of batch insertion:

Method 1: Use union all splicing query method

This article does not do a detailed introduction, you can check the relevant information on the Internet.

insert into pager (PAG_ID,PAG_PARENT,PAG_NAME,PAG_ACTIVE)
          select 8000,0,'Multi 8000',1 from dual
union all select 8001,0,'Multi 8001',1 from dual

Method 2: Use the insert all method

Because when inserting multiple entries in the insert all method, the value obtained through the sequence is the same, and multiple values ​​will not be obtained automatically, so the id needs to be set in other ways, (I use the trigger method to automatically set the id here)

1. Create a test table:

create table test_insert(
       data_id number(10) primary key,
       user_name varchar2(30),
       address varchar2(50)
)

data_id is the primary key, and the primary key value is generated by sequence.

2. Create a sequence:

create sequence seq_test_insert 
minvalue 1
maxvalue 999999999999999999999999
start with 1
increment by 1
cache 20;

3. Create
a trigger to automatically set the id value for the insert statement through the trigger

create or replace trigger tr_test_insert
before insert on test_insert
for each row
begin
  select seq_test_insert.nextval into :new.data_id from dual;
end;  

4. Insert test data:

insert all 
into test_insert(user_name,address) values('aaa','henan')
into test_insert(user_name,address) values('bbb','shanghai')
into test_insert(user_name,address) values('ccc','beijing')
select * from dual;

Equivalent to the following three insert into statements, but the performance is much higher than a single one.

insert into test_insert(user_name,address) values('aaa','henan');
insert into test_insert(user_name,address) values('bbb','shanghai');
insert into test_insert(user_name,address) values('ccc','beijing');

It should be noted that it cannot be used directly in the insert all statement seq_test_insert.nextval, because even if you add it to each into statement, you seq_test_insert.nextvalwill not get multiple values.

5. View test data

select * from test_insert;

The result is as follows:
write picture description here

In addition, insert all also supports inserting data into different tables, such as:

insert all 
into table1(filed1,filed2)values('value1','value2')
into table2(字段1,字段2,字段3) values(值1,值2,值3)
select * from dual;

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326075535&siteId=291194637