What are practical differences between `REPLACE` and `INSERT … ON DUPLICATE KEY UPDATE` in MySQL?

前端 未结 8 1482
清酒与你
清酒与你 2020-11-28 06:59

What I need is to set the values of all the fields of a record with a particular key (the key is composite actually), inserting the record if there is no record with such a

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 07:34

    To answer the question in terms of performance, I did a test using both the methods

    Replace Into involves:
    1.Try insert on the table
    2. If 1 fails, delete row and insert new row

    Insert on Duplicate Key Update involves:
    1.Try insert on table
    2.If 1 fails, update row

    If all the steps involved are inserts, there should be no difference in performance. The speed has to depend on the number of updates involved. Worst case is when all the statements are updates

    I have tried both the statements on my InnoDB table involving 62,510 entries (only updates). On camparing speeds:
    Replace Into: 77.411 seconds
    Insert on Duplicate Key Update: 2.446 seconds

    Insert on Duplicate Key update is almost 32 times faster.
    

    Table Size: 1,249,250 rows with 12 columns on an Amazon m3.medium

提交回复
热议问题