批量插入資料(基於Mybatis的實現-Oracle)
-----------------------------------
20170528 第二次編輯:主要是補充mysql相關內容。
-----------------------------------
mysql支援batch提交改進方案:宣告:mysql仍然沒有內部遊標,讓資料庫支援executeBatch的方式處理。
MySql 的批量操作,要加rewriteBatchedStatements引數
引用“
MySql的JDBC連線的url中要加rewriteBatchedStatements引數,並保證5.1.13以上版本的驅動,才能實現高效能的批量插入。
例如: String connectionUrl="jdbc:mysql://192.168.1.100:3306/test?rewriteBatchedStatements=true" ;
還要保證Mysql JDBC驅的版本。MySql的JDBC驅動的批量插入操作效能是很優秀的。
”
原文連結:http://elf8848.iteye.com/blog/770032 (建議去看看這個連結的評論區)
參考資料:https://www.oschina.net/question/2553117_2162171?sort=time
20170528親測,插入26663條資料,
加上rewriteBatchedStatements後,耗時:3734毫秒
不加rewriteBatchedStatements前,耗時:672551毫秒
mysql裝在本機上,行欄位數多,僅從本次測試看,效能提高了180倍。
---------------------------------------------------
第一版:20170526 原創
---------------------------------------------------
前言:做一個數據同步專案,要求:同步資料不丟失的情況下,提高插入效能。
專案DB框架:Mybatis。DataBase:Oracle。
----------------------------------------------------------------------------
批量插入資料方式:
一、Mybatis 全域性設定批處理;
二、Mybatis 區域性設定批處理;
三、Mybatis foreach批量插入:
①SELECT UNION ALL;
②BEGIN INSERT INTO ...;INSERT INTO...;...;END;
四、java自帶的批處理插入;
五、其他方式
-----------------------------------------------------------------------------
先說結論:Mybatis(全域性/區域性)批處理和java自帶的批處理 效能上差不多,屬於最優處理辦法,我這邊各種測試後,最後採用Mybatis區域性批處理方式。
一、Mybatis 全域性設定批處理
先上Spring-Mybatis.xml 配置資訊
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" 5 xsi:schemaLocation="http://www.springframework.org/schema/beans 6 http://www.springframework.org/schema/beans/spring-beans.xsd 7 http://www.springframework.org/schema/context 8 http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> 9 10 <!-- 自動掃描(自動注入) --> 11 <context:annotation-config/> 12 <context:component-scan base-package="com.company.dao"/> 13 14 <!-- 動態資料來源 --> 15 <bean id="dataSource" class="com.company.dao.datasource.DataSource"> 16 <property name="myConfigFile" value="mySource.xml"/> 17 </bean> 18 19 <!-- mybatis配置 --> 20 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> 21 <property name="dataSource" ref="dataSource"/> 22 <property name="mapperLocations" value="classpath*:mapper/*/*/*.xml"/> 23 <property name="configLocation" value="classpath:/mybatisConfig.xml"/> 24 </bean> 25 26 <!-- 自動建立對映器,不用單獨為每個 mapper對映--> 27 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 28 <property name="basePackage" value="com.company.dao.mapper"/> 29 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> 30 </bean> 31 32 <!-- 事務管理器配置,單資料來源事務 --> 33 <bean id="transactionManager" 34 class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 35 <property name="dataSource" ref="dataSource"/> 36 </bean> 37 38 <tx:annotation-driven transaction-manager="transactionManager"/> 39 40 </beans>Spring-Mybatis.xml
再上mybatisConfig.xml(在本專案中,我沒有設定setting。最終採用的區域性批處理,因此未設定全域性批處理,具體原因後面再說。)
1 <?mapper.xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> 3 <configuration> 4 5 <settings> 6 <!-- 配置預設的執行器。SIMPLE 就是普通的執行器;REUSE 執行器會重用預處理語句(prepared statements); BATCH 執行器將重用語句並執行批量更新。--> 7 <setting name="defaultExecutorType" value="BATCH"/> 8 <!--詳見:http://www.mybatis.org/mybatis-3/zh/configuration.html--> 9 </settings> 10 11 <!-- 別名列表 --> 12 <typeAliases> 13 <!-- typeAliases 中的配置都是配置別名,在此就不貼出來了 --> 14 </typeAliases> 15 16 </configuration>mybatisConfig.xml
這樣子設定好後,在BaseService開放saveBatch(List<T> list)方法
1 @Override 2 public void save(List<T> list) { 3 for (int i = 0;i < list.size();i++){ 4 mapper.insert(list.get(i)); 5 } 6 } 7 8 @Override 9 public void saveBatch(List<T> list) { 10 int size = list.size(); 11 int unitNum = 500; 12 int startIndex = 0; 13 int endIndex = 0; 14 while (size > 0){ 15 if(size > unitNum){ 16 endIndex = startIndex+unitNum; 17 }else { 18 endIndex = startIndex+size; 19 } 20 List<T> insertData = list.subList(startIndex,endIndex); 21 save(insertData); 22 size = size - unitNum; 23 startIndex = endIndex; 24 } 25 }BaseService.saveBatch(List list)
雖然看上去是500條記錄,一次次INSERT INTO,但由於在全域性已經設定Mybatis是批處理執行器,所以這500條INSERT INTO只會與Oracle資料庫通訊一次。
全域性設定批處理的侷限性在哪裡呢?
先附上mybatis官方的討論列表中最很關鍵的一句:“If the BATCH executor is in use, the update counts are being lost. ”
設定全域性批處理後,DB裡的insert、Update和delete方法,都無法返回進行DML影響DB_TABLE的行數。
1.insert 無法返回影響的行數,這個好解決,一個批處理放在一個事務裡,記錄批處理失敗次數,總數-批處理失敗次數*單位批處理資料量,就能得到insert 影響DB_TABLE的行數;
2.但是update和delete就無法很簡單的去統計影響行數了,如果做反覆查詢,反而降低了效率,得不償失。
雖現在的專案尚未有需要反饋影響DB_TABLE行數的需求,但是為了更靈活,我們放棄了全域性批處理的方式。
!這裡提個疑問:為什麼Mybatis官方,不將批處理的選擇方式下沉到方法級別?方便開發者根據實際情況,靈活選擇。我覺得這是個可以改進的地方,如有機會,可看原始碼去進行改進。
---------------------------------------------------------------------------------------------------------
二、Mybatis區域性批處理方式
由於領導說全域性批處理方式,不夠靈活,不適宜專案所需,要另想辦法支援。但是java自帶的批處理,因為專案程式碼管理的要求,也不能採用。因此,在仔細閱讀官方文件後,設想自己能否獲取SQLSession後openSession,將這個會話設定為批處理呢?
1 SqlSession session = sqlSessionFactory.openSession(); 2 try { 3 BlogMapper mapper = session.getMapper(BlogMapper.class); 4 // do work 5 } finally { 6 session.close(); 7 }官方建議的寫法
現在你有一個 SqlSessionFactory,可以用來建立 SqlSession 例項。
SqlSessionFactory
SqlSessionFactory 有六個方法可以用來建立 SqlSession 例項。通常來說,如何決定是你 選擇下面這些方法時:
- Transaction (事務): 你想為 session 使用事務或者使用自動提交(通常意味著很多 資料庫和/或 JDBC 驅動沒有事務)?
- Connection (連線): 你想 MyBatis 獲得來自配置的資料來源的連線還是提供你自己
- Execution (執行): 你想 MyBatis 複用預處理語句和/或批量更新語句(包括插入和 刪除)
過載的 openSession()方法簽名設定允許你選擇這些可選中的任何一個組合。
1 SqlSession openSession() 2 SqlSession openSession(boolean autoCommit) 3 SqlSession openSession(Connection connection) 4 SqlSession openSession(TransactionIsolationLevel level) 5 SqlSession openSession(ExecutorType execType,TransactionIsolationLevel level) 6 SqlSession openSession(ExecutorType execType) 7 SqlSession openSession(ExecutorType execType, boolean autoCommit) 8 SqlSession openSession(ExecutorType execType, Connection connection) 9 Configuration getConfiguration();官方提供的openSession方法
因此出來了局部批處理第一套程式碼實現方式:
1 public static void sqlSession(List<Student> data) throws IOException { 2 String resource = "mybatis-dataSource.xml"; 3 InputStream inputStream = null; 4 SqlSession batchSqlSession = null; 5 try{ 6 inputStream = Resources.getResourceAsStream(resource); 7 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); 8 batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); 9 int batchCount = 500;//每批commit的個數 10 for(int index = 0; index < data.size();index++){ 11 Student stu = data.get(index); 12 batchSqlSession.getMapper(Student.class).insert(stu); 13 if(index !=0 && index%batchCount == 0){ 14 batchSqlSession.commit(); 15 } 16 } 17 batchSqlSession.commit(); 18 }catch (Exception e){ 19 e.printStackTrace(); 20 }finally { 21 if(batchSqlSession != null){ 22 batchSqlSession.close(); 23 } 24 if(inputStream != null){ 25 inputStream.close(); 26 } 27 } 28 }sqlSession(List data)
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE configuration 3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 4 "http://mybatis.org/dtd/mybatis-3-config.dtd"> 5 <configuration> 6 <environments default="development"> 7 <environment id="development"> 8 <transactionManager type="JDBC"/> 9 <dataSource type="POOLED"> 10 <property name="driver" value="${driver}"/> 11 <property name="url" value="${url}"/> 12 <property name="username" value="${username}"/> 13 <property name="password" value="${password}"/> 14 </dataSource> 15 </environment> 16 </environments> 17 <mappers> 18 <mapper resource="org/mybatis/example/Student.xml"/> 19 </mappers> 20 </configuration>mybatis-dataSource.xml
已經在Spring-Mybatis.xml 中配置了SQLSessionFactory,那我為何還要自己去建立SQLSessionFactory呢?因此繼續改良程式碼
1 public static void mybatisSqlSession(List<Student> data){ 2 DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) ServiceBeanConstant.CTX.getBean("sqlSessionFactory"); 3 SqlSession batchSqlSession = null; 4 try{ 5 batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); 6 int batchCount = 500;//每批commit的個數 7 for(int index = 0; index < data.size();index++){ 8 Student stu = data.get(index); 9 batchSqlSession.getMapper(StudentMapper.class).insert(stu); 10 if(index !=0 && index%batchCount == 0){ 11 batchSqlSession.commit(); 12 } 13 } 14 batchSqlSession.commit(); 15 }catch (Exception e){ 16 e.printStackTrace(); 17 }finally { 18 if(batchSqlSession != null){ 19 batchSqlSession.close(); 20 } 21 } 22 }mybatisSqlSession(List data)
這個版本的區域性批處理插入是比較滿意的,最終採用的方式也是這個版本。
下面放出在IService介面定義和Service的具體實現程式碼:
IService介面定義
1 /** 2 * 批處理插入資料(方法內部定義500條為一個批次進行提交) 3 * 使用注意事項:必須在XxxMappper.xml中實現<insert id="insert" ...>....<insert/>的sql 4 * @param data 批量插入的資料 5 * @param mClass 呼叫的XxxMaperr.class 6 * @auth robin 7 * Created on 2016/3/14 8 */ 9 void saveBatch(List<T> data,Class mClass);saveBatch(List data,Class mClass)
Service實現
1 @Override 2 public void saveBatch(List<T> data,Class mClass) { 3 DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) ServiceBeanConstant.CTX.getBean("sqlSessionFactory"); 4 SqlSession batchSqlSession = null; 5 try{ 6 batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); 7 int batchCount = 500;//每批commit的個數 8 for(int index = 0; index < data.size();index++){ 9 T t = data.get(index); 10 ((BaseMapper)batchSqlSession.getMapper(mClass)).insert(t); 11 if(index !=0 && index%batchCount == 0){ 12 batchSqlSession.commit(); 13 } 14 } 15 batchSqlSession.commit(); 16 }catch (Exception e){ 17 e.printStackTrace(); 18 }finally { 19 if(batchSqlSession != null){ 20 batchSqlSession.close(); 21 } 22 } 23 }saveBatch(List data,Class mClass)
區域性和全域性批處理插入對比:區域性批處理,可以對特定一類的方法,進行資料批處理,不會影響其他DML語句,其他DML語句,可以正常返回影響DB_TABLE的行數。
!這樣既能針對特殊需求(批處理)支援,也能支援未來需要返回影響資料行的要求。
注意:使用批處理方式進行DML操作,是無法反饋影響DB_TABLE行數的資料。無論是區域性批處理還是java自帶的批處理方式,皆無法反饋DB_TABLE count。
補充完善:
在我的Service實現中,通過注入的方式,獲取mapper的例項
1 public class BaseService<MAPPER extends BaseMapper, T, PK extends Serializable> implements IBaseService<T, PK> { 2 3 protected T tt; 4 /** 5 * 實體操作的自動注入Mapper(隨初始化一同注入,必須用set方法) 6 */ 7 protected MAPPER mapper; 8 9 public MAPPER getMapper() { 10 return mapper; 11 } 12 13 @Autowired 14 public void setMapper(MAPPER mapper) { 15 this.mapper = mapper; 16 } 17 //後續程式碼略 18 }Service
前面的Service saveBatch方法中,還需要傳入指定的Mapper.class.對本專案其他開發者來說,與之前的環境相比,多傳一個引數感覺彆扭。
那麼為何我不繼續封裝,外部無需傳入Mapper.class,而是通過內部注入的mapper例項獲取Mapper.class.
改良後的程式碼:
1 @Override 2 public T saveBatch(List<T> data) { 3 T back = null; 4 DefaultSqlSessionFactory sqlSessionFactory = (DefaultSqlSessionFactory) ServiceBeanConstant.CTX.getBean("sqlSessionFactory"); 5 SqlSession batchSqlSession = null; 6 try{ 7 batchSqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false); 8 int batchCount = 500;//每批commit的個數 9 for(int index = 0; index < data.size();index++){ 10 T t = data.get(index); 11 back = t; 12 Class<?>[] interfaces=mapper.getClass().getInterfaces(); 13 Class clazz = null; 14 for (int i=0;i<interfaces.length;i++){ 15 if(BaseMapper.class.isAssignableFrom(interfaces[i])){ 16 clazz = interfaces[i]; 17 } 18 } 19 if(clazz == null){ 20 throw new Exception("user-defined exception:mapper not implements interfaces com.company.dao.mapper.BaseMapper"); 21 } 22 BaseMapper baseMapper = (BaseMapper) batchSqlSession.getMapper(clazz); 23 baseMapper.insert(t); 24 if(index !=0 && index%batchCount == 0){ 25 batchSqlSession.commit(); 26 } 27 } 28 batchSqlSession.commit(); 29 }catch (Exception e){ 30 e.printStackTrace(); 31 }finally { 32 if(batchSqlSession != null){ 33 batchSqlSession.close(); 34 } 35 return back; 36 } 37 }saveBatch(List data)
這裡對mapper例項進行一個簡短的說明:
1.mapper例項是通過java動態代理來例項化的;
2.mapper的SQLSession是使用mybatis統一的配置例項的;
3.mapper的預設執行器是SIMPLE(普通的執行器);
-------------------------------------------------------------------------------------
三、Mybatis foreach批量插入
Mybatis foreach 批量插入,如果批量插入的資料量大,不得不說這真是一個非常糟糕的做法。
無論是SELECT ** UNION ALL 還是BEGIN ...;END; ,相對而言後者比前者稍微好點。
放出DB和我測試的結果:
耗時 | 佔當時整個資料庫CPU百分比 | 說明 |
15.5 | 98.33 | union all方式拼接插入 |
16.4 | 97.75 | begin end方式插入塊 |
1.54 | 64.81 | java 自帶的batch方式插入 |
①foreach union all的批量插入,現已有大量的部落格資源可供參考,我就不貼出自己的實現方式了。
如果有興趣可以參閱:http://blog.csdn.net/sanyuesan0000/article/details/19998727 (開啟瀏覽器,複製url)
這篇部落格。BEGIN END的方式,也是從這篇部落格中得到啟發。只不過他是把BEGIN END用在update中。
②foreach begin end 語句塊
我的實現:
1 <insert id="insertBatch" parameterType="java.util.List"> 2 BEGIN 3 <foreach collection="list" item="item" index="index" separator=";" > 4 INSERT INTO TABLE.STUDENT (ID,AGE,NAME,STU_ID) VALUES 5 ( DEMO.SEQ_EID.NEXTVAL,#{item.age},#{item.name},#{item.stuId} ) 6 </foreach> 7 ;END ; 8 </insert>insertBatch
呼叫方式:
1 @Override 2 public相關推薦
批量插入資料(基於Mybatis的實現-Oracle)
----------------------------------- 20170528 第二次編輯:主要是補充mysql相關內容。 ----------------------------------- mysql支援batch提交改進方案:宣告:mysql仍然沒有內部遊標,讓資料庫支援execu
MyBatis+Oracle用foreach標籤實現批量插入資料以及主鍵自增
1.前言 最近做一個批量匯入影像的需求,將多條記錄批量插入資料庫中。解決思路:在程式中封裝一個List集合物件,然後把該集合中的實體插入到資料庫中,因為專案使用了MyBatis,所以打算使用MyBatis的foreach功能進行批量插入。資料庫用的是Ora
Oracle使用Mybatis實現List批量插入資料(轉載)
專案中會遇到這樣的情況,查詢出多條記錄(一個List物件集合),一次性要插入多條資料到資料庫中,下面就拿Data類來看看兩種插入方法: 方法一: Mybatis本身只支援逐條插入,比較笨的方法,就是遍歷一個List,迴圈中逐條插入,比如下面這段程式碼 [java] view plain co
mybatis oracle兩種方式批量插入資料
<insert id="addBatch" parameterType="java.util.List"> BEGIN &
【mybatis】mybatis oracle批量插入資料
參考:mybatis 對 oracle 的批量操作:https://blog.csdn.net/dzhuqiaolun/article/details/54864521 寫法一: <insert id="insertAttractionsBatch" parameterType="ja
Mybatis 實現Mysql批量插入資料,判斷資料是否存在
常見插入資料的SQL insert into 插入資料庫時會檢查主鍵是否存在,存在會報錯 replace into 替換資料庫記錄,需要表中有主鍵或者unique索引,如果資料庫已存在的資料,會先刪除該資料然後新增。不存在的資料效果和insert into
Mybatis批量插入註解方式示例(oracle + mysql)
場景: 匯入20萬條資料,for迴圈方式一條條插入巨慢(太low)。拼接長SQL的話,oracle根本無法支援(有SQL長度限制),經測試資料多於2000基本就不行了。故改用批量插入,在mapper中註解使用mybatis的foreach標籤寫。Mybatis框架會自動拼接生成批插的sql。
Mybatis 進行批量插入資料時,處理已經存在的資料
假設存在一張 學生表:{姓名,年齡,年級} 假設已經存在了小明,小王等學生 這時候將姓名設定為唯一的主鍵或者索引 insert into 學生表 (姓名,年齡,年級) values <foreach collection="list" item="item" separator=
用SQL儲存過程實現批量插入資料
1,單條插入 INSERT INTO time_by_day (time_id, the_date, the_year, month_of_year, quarter,day_of_month) VALUES ('1101', '1999-10-1', '1999', '10', 'Q4','1'
批量插入資料 Oracle
在使用 Oracle 開發期間,或許我們會為編寫一條一條插入語句而困惱,這裡給出 對資料表進行簡單批量插入的例子。 (以下均是Oracle 資料庫操作) 向CBAY_USER_T 批量
Oracle 批量插入資料 insert all into 用法
專案需要用到匯入excel表,並解析資料批量插入到oracle資料庫中。 1)直接解析excel,迴圈行,拼了sql,executeUpdate。 執行一波… 咦,這效率很低啊,有多少行資料就執行
MyBatis批量插入資料
1.Service int add(List<BillManagement> billManagement); 2.ServiceImpl public int add(List<B
java mybatis mapper批量插入資料,insert/update/對映屬性欄位
@MapperEngine public interface BBINBetsLstMapper { public void replaceBBinBet (List<BBINbet> list); } mapper.xml檔案 <resul
Mybatis動態SQL之foreach標籤批量插入資料(MySQL版)
我們在MySQL中可以使用foreach標籤進行批量插入資料。 foreach中可以有兩種方式批量插入資料: 方式1: INTERT INTO TableName Values(值1,值2,值3….),(值1,值2,值3….)…. 方式2:
利用oracle儲存過程向資料庫批量插入資料
create or replace procedure test is begin for i in 1 ..100 loop insert into 表名(id,name) values
Mybatis 批量插入資料
public class Users implements Serializable{/*** */private static final long serialVersionUID = -7958407366908741797L;private String id;private String name;
Mybatis批量插入資料問題
在專案中使用mybatis批量插入資料時,使用到了 <foreach >標籤,批量merge 進入ORACLE資料庫。入參為List<String>,取資料時使用 <foreach collection="list" item="item" i
MyBatis批量插入資料配置檔案
mybatis批量插入資料到MySQL資料庫資料能夠插入成功但後臺會報BindException,經過分析發現為mapper配置檔案出錯引發,現將完整配置貼出,如下所示: <insert id="insertBatch" useGeneratedKeys="tru
JDBC操作Vertica資料庫,用PreparedStatements物件實現批量插入資料
import java.sql.*; import java.util.Properties; public class BatchInsertExample { public static void main(String[] args) {
oracle與mysql資料庫批量插入資料
批量插入資料時,入坑了,特總結如下: mysql資料庫批量插入方法sql: INSERT INTO users(name, age) VALUES('ccc', 333), ('aaa', 222), ('bbb', 111); oracle資料庫批量插入方法sql