MyBatis BULK Insert Oracle Large data logging performance Problem Resolution __oracle

Source: Internet
Author: User

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: Before a one-time commit, this will increase the number of inserts, the execution rate is suddenly slow, so should be inserted in batches:


	public void Save (list<uidcodebean> uidcodelist) throws Exception {sqlsession = null; try {batchsqlsession = Sqlsessionfactory.opensession (Executortype.batch, false);//Get the sqlsession int Batchcoun of the batch method t = 1000;//Number of commits per batch int batchlastindex = batchcount-1;//subscript for the last of each batch (int index = 0; index < Uidcodelist.siz
					E ()-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;//data is inserted, exit cycle}else{Batchsqlsession.insert (namespace+ ". Adduidcodebatch", Uidcodelist.sublist (Index,					                Batchlastindex));
					Batchsqlsession.commit ();
					System.out.println ("Index:" +index+ "Batchlastindex:" +batchlastindex); index = Batchlastindex + 1;//Set next batch subscript BatchlasTindex = index + (batchCount-1);
		}}finally{Batchsqlsession.close (); }		
	}





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.