sql语句批量执行多条语句和执行一句IN语句对比

--一共费时0.005秒
DELETE from pt_data_inbound_cms_h where id in(1,2,3,4,5);
--一共费时0.004*5
DELETE from pt_data_inbound_cms_h where id=1;
DELETE from pt_data_inbound_cms_h where id=2;
DELETE from pt_data_inbound_cms_h where id=3;
DELETE from pt_data_inbound_cms_h where id=4;
DELETE from pt_data_inbound_cms_h where id=5;

 

--每句执行时间平均0.005,共费时0.005*5
INSERT into pt_data_inbound_cms_h (select * from pt_data_inbound_cms where id=1);
INSERT into pt_data_inbound_cms_h (select * from pt_data_inbound_cms where id=2);
INSERT into pt_data_inbound_cms_h (select * from pt_data_inbound_cms where id=3);
INSERT into pt_data_inbound_cms_h (select * from pt_data_inbound_cms where id=4);
INSERT into pt_data_inbound_cms_h (select * from pt_data_inbound_cms where id=5);
 

--共费时0.005s
INSERT into pt_data_inbound_cms_h (select * from pt_data_inbound_cms where id in(1,2,3,4,5));

mybatis 用foreach建议使用

<insert id="insertById" parameterType="com.newtv.search.core.entity.TaskClearContentType">
    insert into ${toTable}
    ( select * from ${fromTable} where id in
    <foreach collection="ids" index="index" item="item" open="(" separator="," close=")">
        #{item,jdbcType=INTEGER}
    </foreach>
    )
</insert>
<delete id="deleteById" parameterType="com.newtv.search.core.entity.TaskClearContentType">
    delete from ${fromTable}
    where
    id in
    <foreach collection="ids" item="item" index="index" open="(" separator="," close=")">
        #{item,jdbcType=INTEGER}
    </foreach>
</delete>

不建议使用如下:

<insert id="insertById" parameterType="com.newtv.search.core.entity.TaskClearContentType">
    <foreach collection="ids" index="index" item="item" separator=";">
        insert into ${toTable}
        ( select * from ${fromTable} where id=#{item,jdbcType=INTEGER})
    </foreach>
</insert>

<delete id="deleteById" parameterType="com.newtv.search.core.entity.TaskClearContentType">
    <foreach collection="ids" item="item" index="index" separator=";">
    delete from ${fromTable}
    where
    id = #{item,jdbcType=INTEGER}
    </foreach>
</delete>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值