mybatis批量插入oracle大量数据记录性能问题解决
生活随笔
收集整理的這篇文章主要介紹了
mybatis批量插入oracle大量数据记录性能问题解决
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
環(huán)境: mybatis + oracle11g r2
1.使用"直接路徑插入"(下面sql語句中的"/*+append_values */"),并且使用關(guān)鍵字"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層實(shí)現(xiàn): 之前是一次性commit,這樣做會隨著插入數(shù)目的增大,執(zhí)行速度陡然變慢,所以應(yīng)該分批次進(jìn)行插入:
public void save(List<UidCodeBean> uidCodeList) throws Exception {SqlSession batchSqlSession = null;try {batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);//獲取批量方式的sqlsession int batchCount = 1000;//每批commit的個(gè)數(shù)int batchLastIndex = batchCount - 1;//每批最后一個(gè)的下標(biāo)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;//數(shù)據(jù)插入完畢,退出循環(huán)}else{batchSqlSession.insert(NAMESPACE+".addUidCodeBatch", uidCodeList.subList(index, batchLastIndex)); batchSqlSession.commit();System.out.println("index:"+index+" batchLastIndex:"+batchLastIndex);index = batchLastIndex + 1;//設(shè)置下一批下標(biāo)batchLastIndex = index + (batchCount - 1); } } }finally{batchSqlSession.close();} }總結(jié)
以上是生活随笔為你收集整理的mybatis批量插入oracle大量数据记录性能问题解决的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 第四章、项目整合管理【PMP】
- 下一篇: linux环境安装Kafka最新版本