Mysql batch insert transaction insert performance optimization

https://blog.csdn.net/zdw19861127/article/details/78597523?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522161543365116780262512853%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=161543365116780262512853&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~baidu_landing_v2~default-1-78597523.first_rank_v2_pc_rank_v29_10&utm_term=%E5%9C%A8%E4%B8%80%E4%B8%AA%E4%BA%8B%E5%8A%A1%E4%B8%AD%E6%8F%90%E4%BA%A4%E5%A4%A7%E9%87%8FINSERT%E8%AF%AD%E5%8F%A5%E5%8F%AF%E4%BB%A5%E6%8F%90%E9%AB%98%E6%80%A7%E8%83%BD

 

to sum up:

1. Merge SQL

INSERT INTO `insert_table` (`datetime`, `uid`, `content`, `type`)  VALUES ('0', 'userid_0', 'content_0', 0), ('1', 'userid_1', 'content_1', 1);

2. Use transactions

3. The primary keys are ordered when inserting

4. Use insertBatch of mybatis-plus

 

Mybatis realizes inserting multiple records at once

<!--id is a custom method name, parameterType is List-->
<insert id="insertChatHistoryList" useGeneratedKeys="true" parameterType="java.util.List">
    <selectKey resultType="long" keyProperty=" id">
      SELECT LAST_INSERT_ID()
    </selectKey>
    insert into n_chat_history (id,from_name,to_name,message_type, content)
    values
    <!--item is the object name of each item in the List, use "," to separate each piece of data , And finally ";" at the end -->
    <foreach collection="list" item="item" index="index" separator="," close=";">
      (#{item.id,jdbcType=BIGINT}, #{item.fromName,jdbcType=VARCHAR}, #{item.toName,jdbcType=VARCHAR}, #{item.content,jdbcType=VARCHAR},
      #{item.sendType,jdbcType=INTEGER})
    </foreach>
</insert>

sql realizes querying multiple records at a time

insert into Custumer(cust_id, 
cust_cintact, 
cust_name, 
cust_email, 
cust_address, 
cust_country) 

select cust_id, 
cust_cintact, 
cust_name, 
cust_email, 
cust_address, 
cust_country 
from CustNew; 

Guess you like

Origin blog.csdn.net/qq_24271537/article/details/114658320