MySQL performance DELETE or UPDATE?

前端 未结 5 1670
旧巷少年郎
旧巷少年郎 2021-02-14 22:14

I have a MyISAM table with more than 10^7 rows. When adding data to it, I have to update ~10 rows at the end. Is it faster to delete them and then insert the new ones, or is it

5条回答
  •  囚心锁ツ
    2021-02-14 22:57

    UPDATE is by far much faster.

    When you UPDATE, the table records are just being rewritten with new data.

    When you DELETE, the indexes should be updated (remember, you delete the whole row, not only the columns you need to modify) and datablocks may be moved (if you hit the PCTFREE limit)

    And all this must be done again on INSERT.

    That's why you should always use

    INSERT ... ON DUPLICATE KEY UPDATE
    

    instead of REPLACE.

    The former one is an UPDATE operation in case of a key violation, while the latter one is DELETE / INSERT.

提交回复
热议问题