DB2資料庫關於delete in id和batch delete的效能對比

來源:互聯網
上載者:User

標籤:finally   long   batch   cut   使用   db2   type   span   cee   

刪除量:一次刪除10000條。

第一種方式,通過delete from 表 where id in(一堆ID)的方式刪除資料,首先把需要刪除的資料從資料庫查詢出來,將ID傳給mybatis架構,展示如下:

      <delete id="deleteById" parameterType="Map">        DELETE FROM TBL_ACC_HIS_SOURCE        WHERE ID IN        <foreach item="id" index="index" collection="idList" open="(" separator="," close=")">            #{id}        </foreach>    </delete>

每次耗時資料如下:

[374, 206, 206, 205, 482, 231, 460, 297, 277, 218, 232, 203, 228, 272, 207, 280, 203, 195, 272, 374, 523, 314, 207, 222, 247, 275, 269, 203, 216, 190, 310, 225, 277, 238, 237, 213, 228, 235, 236, 204, 240, 217, 213, 191, 209, 260, 268, 317, 231, 207, 200, 203, 204, 202, 209, 217, 212, 310, 216, 253, 201, 204, 212, 191, 211, 200, 237, 234, 279, 194, 203, 250, 199, 228, 224, 238, 259, 279, 257, 299, 196, 264, 299, 210, 228, 227, 219, 203, 207, 328, 213, 237, 348, 315, 210, 219, 325, 236, 196, 197, 211, 229, 187, 276]

共執行104次,平均耗時243毫秒。

第二種方式,使用batch delete,代碼如下:

    private void batchDeleteA(List<Long> ids){        SqlSession batchSqlSession = getBatchSession();        try {            for (Long id : ids) {                batchSqlSession.delete("AccHisSourceEntity.delete", id);            }            batchSqlSession.commit();        } finally {            BatchSqlSessionUtils.closeSqlSession(batchSqlSession);        }    }
    private SqlSession getBatchSession() {        return BatchSqlSessionUtils.getSqlSession(sqlSessionFactory, ExecutorType.BATCH);    }

其中AccHisSourceEntity.delete如下:

  <delete id="delete" parameterType="java.lang.Long" >    delete from      TBL_ACC_HIS_SOURCE    where      ID = #{id,jdbcType=BIGINT}  </delete>

執行119次,每次耗時如下:

[454, 253, 230, 224, 219, 214, 217, 215, 203, 204, 240, 250, 243, 230, 272, 208, 205, 223, 450, 235, 282, 336, 266, 209, 212, 222, 214, 431, 256, 238, 209, 456, 199, 616, 207, 336, 249, 196, 232, 230, 291, 240, 212, 266, 245, 229, 217, 211, 218, 210, 211, 218, 222, 215, 200, 219, 208, 256, 208, 208, 248, 226, 220, 234, 219, 299, 478, 501, 251, 223, 510, 244, 265, 246, 278, 279, 244, 255, 251, 236, 260, 273, 246, 503, 280, 238, 256, 244, 255, 215, 217, 224, 220, 230, 236, 198, 252, 246, 270, 262, 263, 252, 233, 223, 262, 230, 478, 240, 269, 238, 232, 260, 232, 222, 228, 223, 231, 246, 247]

平均耗時:258

結論:幾乎相同。

PS:如果採用查出一批資料,刪除一批資料的方式。採用5000一批,比10000一批要快一些。

DB2資料庫關於delete in id和batch delete的效能對比

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.