title: Performance of "INSERT ... ON DUPLICATE KEY UPDATE" compared to "UPDATE"
body: <p>99% of the time a single <code>UPDATE</code> will suffice, but less than 0.01% of the time I need to follow up with an <code>INSERT</code> on the same key.  This can be written as a single <code>INSERT ... ON DUPLICATE KEY UPDATE</code> but, in terms of performance, should I?</p>
  
  <p>The question is basically if <code>INSERT ... ON DUPLICATE KEY UPDATE</code>s are expected to be slower than <code>UPDATE</code>s in MySQL.</p>
  
  <p>Some code to demonstrate:</p>
  
  <p><strong>Code using <code>UPDATE</code></strong></p>
  
  <pre><code>UPDATE foo SET seq = @new_seq WHERE id = X AND seq = @prev_seq;
  
  IF rows_changed = 0 THEN
      INSERT INTO foo (id, seq) VALUES (X, @new_seq)
      IF &lt;duplicate key error&gt; THEN
          -- handle collusion
      END IF
  END IF
  </code></pre>
  
  <p><strong>Code using <code>INSERT ... ON DUPLICATE KEY UPDATE</code></strong></p>
  
  <pre><code>INSERT INTO foo (id, seq) VALUES (X, @new_seq)
      ON DUPLICATE KEY UPDATE seq = IF(seq = @prev_seq, VALUES(seq), seq)
  
  IF rows_changed = 0 THEN
      -- handle collusion
  END IF
  </code></pre>