Mybatis batch inserts a large number of oracle data records to solve the performance problem. mybatisoracle

Source: Internet
Author: User

Mybatis batch inserts a large number of oracle data records to solve the performance problem. mybatisoracle

Environment: mybatis + oracle11g r2


1. Use "direct path insertion" ("/* + append_values */" in the following SQL statement), and use the keyword "union all ":


<insert id="addUidCodeBatch"  parameterType="java.util.List">      insert into /*+append_values */        T_UID_CODE(C_UID_CODE,         C_SERAIL_LEN,         C_BATCH_CODE,         C_TYPE,         C_CREATE_TIME,         C_SUPER_CODE,         c_security_code,         C_SERIAL_CODE         )       <foreach collection="list" item="item" index="index" separator="union all" >    select #{item.uidCode},         #{item.kCode},               #{item.batchCode},         #{item.type},         sysdate,         #{item.superCode},         #{item.securityCode},         #{item.serialCode}  from dual       </foreach>      </insert>  

2. dao layer implementation: Previously, it was a one-time commit. As the number of inserts increases, the execution speed suddenly slows down. Therefore, insert data in batches:


Public void save (List <UidCodeBean> uidCodeList) throws Exception {SqlSession batchSqlSession = null; try {batchSqlSession = sqlSessionFactory. openSession (ExecutorType. BATCH, false); // obtain sqlsessionint batchCount = 1000; // The number of commit entries in each BATCH int batchLastIndex = batchCount-1; // The subscript for (int index = 0; index <uidCodeList. size ()-1;) {if (batchLastIndex> uidCodeList. size ()-1) {batchLastIndex = uidCodeList. size ()-1; batchSqlSession. insert (NAMESPACE + ". addUidCodeBatch ", uidCodeList. subList (index, batchLastIndex); batchSqlSession. commit (); System. out. println ("index:" + index + "batchLastIndex:" + batchLastIndex); break; // after data is inserted, exit the loop} else {batchSqlSession. insert (NAMESPACE + ". addUidCodeBatch ", uidCodeList. subList (index, batchLastIndex); batchSqlSession. commit (); System. out. println ("index:" + index + "batchLastIndex:" + batchLastIndex); index = batchLastIndex + 1; // set the next batch of subscripts batchLastIndex = index + (batchCount-1) ;}} finally {batchSqlSession. close ();}}






Mybatis uses oracle to insert data and returns the primary key.

Which of the following rows should be placed after SQL?
<SelectKey resultType = "java. lang. Integer" keyProperty = "ID" order = "BEFORE">
SELECT IBOKEE_COMM_TAG_LIBRARY_SEQ.nextval AS Id FROM DUAL
</SelectKey>

Mybatis implements oracle batch insert java method

When inserted in java code, it is cyclically placed into batch. Then, you only need to process the batch once ..
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.