女人荫蒂被添全过程13种图片,亚洲+欧美+在线,欧洲精品无码一区二区三区 ,在厨房拨开内裤进入毛片

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

MyBatis批量插入別再亂用foreach了

jf_ro2CN3Fa ? 來源:CSDN ? 2023-03-13 09:47 ? 次閱讀
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

c1d45698-c02f-11ed-bfe3-dac502259ad0.jpg


近日,項(xiàng)目中有一個(gè)耗時(shí)較長的Job存在CPU占用過高的問題,經(jīng)排查發(fā)現(xiàn),主要時(shí)間消耗在往MyBatis中批量插入數(shù)據(jù)。mapper configuration是用foreach循環(huán)做的,差不多是這樣。(由于項(xiàng)目保密,以下代碼均為自己手寫的demo代碼)

<insertid="batchInsert"parameterType="java.util.List">
insertintoUSER(id,name)values
<foreachcollection="list"item="model"index="index"separator=",">
(#{model.id},#{model.name})
foreach>
insert>

這個(gè)方法提升批量插入速度的原理是,將傳統(tǒng)的:

INSERTINTO`table1`(`field1`,`field2`)VALUES("data1","data2");
INSERTINTO`table1`(`field1`,`field2`)VALUES("data1","data2");
INSERTINTO`table1`(`field1`,`field2`)VALUES("data1","data2");
INSERTINTO`table1`(`field1`,`field2`)VALUES("data1","data2");
INSERTINTO`table1`(`field1`,`field2`)VALUES("data1","data2");

轉(zhuǎn)化為:

INSERTINTO`table1`(`field1`,`field2`)
VALUES("data1","data2"),
("data1","data2"),
("data1","data2"),
("data1","data2"),
("data1","data2");

在MySql Docs中也提到過這個(gè)trick,如果要優(yōu)化插入速度時(shí),可以將許多小型操作組合到一個(gè)大型操作中。理想情況下,這樣可以在單個(gè)連接中一次性發(fā)送許多新行的數(shù)據(jù),并將所有索引更新和一致性檢查延遲到最后才進(jìn)行。

乍看上去這個(gè)foreach沒有問題,但是經(jīng)過項(xiàng)目實(shí)踐發(fā)現(xiàn),當(dāng)表的列數(shù)較多(20+),以及一次性插入的行數(shù)較多(5000+)時(shí),整個(gè)插入的耗時(shí)十分漫長,達(dá)到了14分鐘,這是不能忍的。在資料中也提到了一句話:

Of course don't combine ALL of them, if the amount is HUGE. Say you have 1000 rows you need to insert, then don't do it one at a time. You shouldn't equally try to have all 1000 rows in a single query. Instead break it into smaller sizes.

它強(qiáng)調(diào),當(dāng)插入數(shù)量很多時(shí),不能一次性全放在一條語句里??墒菫槭裁床荒芊旁谕粭l語句里呢?這條語句為什么會(huì)耗時(shí)這么久呢?我查閱了資料發(fā)現(xiàn):

Insert inside Mybatis foreach is not batch, this is a single (could become giant) SQL statement and that brings drawbacks:

  • some database such as Oracle here does not support.
  • in relevant cases: there will be a large number of records to insert and the database configured limit (by default around 2000 parameters per statement) will be hit, and eventually possibly DB stack error if the statement itself become too large.

Iteration over the collection must not be done in the mybatis XML. Just execute a simple Insertstatement in a Java Foreach loop. The most important thing is the session Executor type.

SqlSessionsession=sessionFactory.openSession(ExecutorType.BATCH);
for(Modelmodel:list){
session.insert("insertStatement",model);
}
session.flushStatements();

Unlike default ExecutorType.SIMPLE, the statement will be prepared once and executed for each record to insert.

從資料中可知,默認(rèn)執(zhí)行器類型為Simple,會(huì)為每個(gè)語句創(chuàng)建一個(gè)新的預(yù)處理語句,也就是創(chuàng)建一個(gè)PreparedStatement對(duì)象。在我們的項(xiàng)目中,會(huì)不停地使用批量插入這個(gè)方法,而因?yàn)镸yBatis對(duì)于含有的語句,無法采用緩存,那么在每次調(diào)用方法時(shí),都會(huì)重新解析sql語句。

Internally, it still generates the same single insert statement with many placeholders as the JDBC code above.

MyBatis has an ability to cache PreparedStatement, but this statement cannot be cached because it contains element and the statement varies depending on the parameters. As a result, MyBatis has to 1) evaluate the foreach part and 2) parse the statement string to build parameter mapping [1] on every execution of this statement.

And these steps are relatively costly process when the statement string is big and contains many placeholders.

[1] simply put, it is a mapping between placeholders and the parameters.

從上述資料可知,耗時(shí)就耗在,由于我foreach后有5000+個(gè)values,所以這個(gè)PreparedStatement特別長,包含了很多占位符,對(duì)于占位符和參數(shù)的映射尤其耗時(shí)。并且,查閱相關(guān)資料可知,values的增長與所需的解析時(shí)間,是呈指數(shù)型增長的。

c1e8ecb6-c02f-11ed-bfe3-dac502259ad0.png

所以,如果非要使用 foreach 的方式來進(jìn)行批量插入的話,可以考慮減少一條 insert 語句中 values 的個(gè)數(shù),最好能達(dá)到上面曲線的最底部的值,使速度最快。一般按經(jīng)驗(yàn)來說,一次性插20~50行數(shù)量是比較合適的,時(shí)間消耗也能接受。

重點(diǎn)來了。上面講的是,如果非要用的方式來插入,可以提升性能的方式。而實(shí)際上,MyBatis文檔中寫批量插入的時(shí)候,是推薦使用另外一種方法。(可以看 http://www.mybatis.org/mybatis-dynamic-sql/docs/insert.html 中 Batch Insert Support 標(biāo)題里的內(nèi)容)

SqlSessionsession=sqlSessionFactory.openSession(ExecutorType.BATCH);
try{
SimpleTableMappermapper=session.getMapper(SimpleTableMapper.class);
Listrecords=getRecordsToInsert();//notshown

BatchInsertbatchInsert=insert(records)
.into(simpleTable)
.map(id).toProperty("id")
.map(firstName).toProperty("firstName")
.map(lastName).toProperty("lastName")
.map(birthDate).toProperty("birthDate")
.map(employed).toProperty("employed")
.map(occupation).toProperty("occupation")
.build()
.render(RenderingStrategy.MYBATIS3);

batchInsert.insertStatements().stream().forEach(mapper::insert);

session.commit();
}finally{
session.close();
}

即基本思想是將 MyBatis session 的 executor type 設(shè)為 Batch ,然后多次執(zhí)行插入語句。就類似于JDBC的下面語句一樣。

Connectionconnection=DriverManager.getConnection("jdbc//127.0.0.1:3306/mydb?useUnicode=true&characterEncoding=UTF-8&useServerPrepStmts=false&rewriteBatchedStatements=true","root","root");
connection.setAutoCommit(false);
PreparedStatementps=connection.prepareStatement(
"insertintotb_user(name)values(?)");
for(inti=0;i1,name);
ps.addBatch();
}
ps.executeBatch();
connection.commit();
connection.close();

經(jīng)過試驗(yàn),使用了 ExecutorType.BATCH 的插入方式,性能顯著提升,不到 2s 便能全部插入完成。

總結(jié)一下,如果MyBatis需要進(jìn)行批量插入,推薦使用 ExecutorType.BATCH 的插入方式,如果非要使用 的插入的話,需要將每次插入的記錄控制在 20~50 左右。

基于 Spring Boot + MyBatis Plus + Vue & Element 實(shí)現(xiàn)的后臺(tái)管理系統(tǒng) + 用戶小程序,支持 RBAC 動(dòng)態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能

  • 項(xiàng)目地址:https://github.com/YunaiV/ruoyi-vue-pro
  • 視頻教程:https://doc.iocoder.cn/video/

基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實(shí)現(xiàn)的后臺(tái)管理系統(tǒng) + 用戶小程序,支持 RBAC 動(dòng)態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能

  • 項(xiàng)目地址:https://github.com/YunaiV/yudao-cloud
  • 視頻教程:https://doc.iocoder.cn/video/


審核編輯 :李倩


聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 參數(shù)
    +關(guān)注

    關(guān)注

    11

    文章

    1867

    瀏覽量

    32947
  • MySQL
    +關(guān)注

    關(guān)注

    1

    文章

    850

    瀏覽量

    27720

原文標(biāo)題:求求你們了,MyBatis 批量插入別再亂用 foreach 了,5000 條數(shù)據(jù)花了 14 分鐘。。

文章出處:【微信號(hào):芋道源碼,微信公眾號(hào):芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請(qǐng)注明出處。

收藏 人收藏
加入交流群
微信小助手二維碼

掃碼添加小助手

加入工程師交流群

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    MyBatis Oracle解析Excel文件

    MyBatis Oracle批量插入數(shù)據(jù)
    發(fā)表于 09-06 09:10

    MyBatis的整合

    SpringBoot-15-之整合MyBatis-注解篇+分頁
    發(fā)表于 10-28 08:09

    Mybatis是什么

    Mybatis第一講
    發(fā)表于 06-04 15:33

    接地磁珠不要亂用

    開關(guān)電源的相關(guān)知識(shí)學(xué)習(xí)教材資料——接地磁珠不要亂用
    發(fā)表于 09-20 16:10 ?0次下載

    mybatis快速入門

    本文詳細(xì)介紹mybatis相關(guān)知識(shí),以及mybatis快速入門步驟詳解。
    的頭像 發(fā)表于 02-24 09:41 ?3806次閱讀
    <b class='flag-5'>mybatis</b>快速入門

    MyBatis的實(shí)現(xiàn)原理

    本文主要詳細(xì)介紹MyBatis的實(shí)現(xiàn)原理。mybatis底層還是采用原生jdbc來對(duì)數(shù)據(jù)庫進(jìn)行操作的,只是通過 SqlSessionFactory,SqlSession Executor
    的頭像 發(fā)表于 02-24 11:25 ?6704次閱讀
    <b class='flag-5'>MyBatis</b>的實(shí)現(xiàn)原理

    Java的iterator和foreach遍歷集合源代碼

    Java的iterator和foreach遍歷集合源代碼
    發(fā)表于 03-17 09:16 ?9次下載
    Java的iterator和<b class='flag-5'>foreach</b>遍歷集合源代碼

    MySQL 批量插入不重復(fù)數(shù)據(jù)的解決方法

    業(yè)務(wù)很簡單:需要批量插入一些數(shù)據(jù),數(shù)據(jù)來源可能是其他數(shù)據(jù)庫的表,也可能是一個(gè)外部excel的導(dǎo)入
    的頭像 發(fā)表于 07-02 15:28 ?2482次閱讀
    MySQL <b class='flag-5'>批量</b><b class='flag-5'>插入</b>不重復(fù)數(shù)據(jù)的解決方法

    PHP教程:foreach使用引用注意的問題

    PHP教程:foreach使用引用注意的問題(電源技術(shù)期刊查詢)-該文檔為PHP教程:foreach使用引用注意的問題總結(jié)文檔,是一份不錯(cuò)的參考資料,感興趣的可以下載看看,,,,,,,,,,,,,,,,,
    發(fā)表于 09-22 12:28 ?9次下載
    PHP教程:<b class='flag-5'>foreach</b>使用引用注意的問題

    MyBatis批量插入數(shù)據(jù)的3種方法你知道幾種

    批量插入功能是我們?nèi)粘9ぷ髦斜容^常見的業(yè)務(wù)功能之一, 今天 來一個(gè) MyBatis 批量插入的匯總篇,同時(shí)對(duì) 3 種實(shí)現(xiàn)方法做一個(gè)性能測(cè)試,
    的頭像 發(fā)表于 12-08 17:56 ?4525次閱讀
    <b class='flag-5'>MyBatis</b><b class='flag-5'>批量</b><b class='flag-5'>插入</b>數(shù)據(jù)的3種方法你知道幾種

    Fluent Mybatis、原生MybatisMybatis Plus對(duì)比

    mapper中再組裝參數(shù)。那對(duì)比原生Mybatis, Mybatis Plus或者其他框架,F(xiàn)luentMybatis提供哪些便利呢?
    的頭像 發(fā)表于 09-15 15:41 ?1652次閱讀

    for循環(huán)和forEach的差異

    for循環(huán)是js提出時(shí)就有的循環(huán)方法。forEach是ES5提出的,掛載在可迭代對(duì)象原型上的方法,例如Array Set Map。forEach是一個(gè)迭代器,負(fù)責(zé)遍歷可迭代對(duì)象。那么遍歷 ,迭代 ,可迭代對(duì)象 分別是什么呢。
    的頭像 發(fā)表于 10-11 11:10 ?1603次閱讀

    MyBatis、JDBC等做大數(shù)據(jù)量數(shù)據(jù)插入的案例和結(jié)果

    30萬條數(shù)據(jù)插入插入數(shù)據(jù)庫驗(yàn)證 實(shí)體類、mapper和配置文件定義 不分批次直接梭哈 循環(huán)逐條插入 MyBatis實(shí)現(xiàn)插入30萬條數(shù)據(jù) JD
    的頭像 發(fā)表于 05-22 11:23 ?1298次閱讀
    <b class='flag-5'>MyBatis</b>、JDBC等做大數(shù)據(jù)量數(shù)據(jù)<b class='flag-5'>插入</b>的案例和結(jié)果

    如何調(diào)優(yōu)MyBatis 25倍性能

    最近在壓測(cè)一批接口,發(fā)現(xiàn)接口處理速度慢的有點(diǎn)超出預(yù)期,感覺很奇怪,后面定位發(fā)現(xiàn)是數(shù)據(jù)庫批量保存這塊很慢。 這個(gè)項(xiàng)目用的是 mybatis-plus,批量保存直接用的是 mybatis
    的頭像 發(fā)表于 05-30 09:56 ?832次閱讀
    如何調(diào)優(yōu)<b class='flag-5'>MyBatis</b> 25倍性能

    mybatis框架的主要作用

    。MyBatis框架的主要作用包括以下幾個(gè)方面。 數(shù)據(jù)庫操作的簡化和標(biāo)準(zhǔn)化: MyBatis框架提供一種簡單的方式來執(zhí)行數(shù)據(jù)庫操作,包括插入、更新、刪除和查詢等操作。通過使用
    的頭像 發(fā)表于 12-03 14:49 ?2456次閱讀
    主站蜘蛛池模板: 新乐市| 黎川县| 洛隆县| 盐边县| 桃江县| 玉田县| 固原市| 南通市| 佛山市| 平罗县| 手机| 齐齐哈尔市| 河北省| 开封市| 庆城县| 南城县| 常熟市| 大宁县| 来凤县| 炉霍县| 阿克陶县| 平昌县| 繁峙县| 石景山区| 镇宁| 绿春县| 海南省| 柳州市| 抚州市| 保定市| 白朗县| 潼南县| 嘉善县| 天气| 通海县| 兴山县| 霍州市| 万宁市| 大同县| 瑞丽市| 葫芦岛市|