package
0.0.0-20241212195640-2e19e05a42f5
Repository: https://github.com/addi-11/system-design-excercises.git
Documentation: pkg.go.dev

# README

Benchmark MySQL's UPSERT

  • ON DUPLICATE KEY UPDATE
  • REPLACE INTO
CREATE TABLE IF NOT EXISTS test_table (id INT PRIMARY KEY, data VARCHAR(20));
nON DUPLICATE KEY UPDATE Time TakenREPLACE INTO Time Taken
12.525ms1.267ms
102.8037ms17.238ms
10027.2609ms139.3891ms
500105.1636ms706.9529ms
1000217.6239ms1.2950787s
50001.235172s6.8721067s
100002.3808408s12.8781352s
5000011.9876923s3m2.1582408s

Reason for Performance Difference

  • ON DUPLICATE KEY UPDATE only updates existing rows on conflict, which minimizes locking and reduces index operations.
  • REPLACE INTO first deletes any existing row and then inserts a new one, causing additional I/O overhead, index adjustments, and potentially larger locks, which slow down performance, particularly as the data volume grows.

Conclusion

  • ON DUPLICATE KEY UPDATE is generally more efficient than REPLACE INTO for large-scale operations, especially when avoiding unnecessary deletions and insertions.