When incrementing a table that manages a count are you an
optimist or a pessimist? If the row is likely to exist, you
should first try to update it and do an insert when the update
fails. Otherwise, use insert ... on duplicate key udpate.
It is more expensive to use insert ... on duplicate key
update when update is sufficient. That is fair given
that insert ... on duplicate key update must do more work
(first try the insert, then try the update). You can read the
code for this in sql/sql_insert.cc. It is in the functions
mysql_insert and write_record.
I ran a simple test using MySQL 5.0.84, InnoDB and an 8-core
server. The test measures the time to run the statements above
using 1, 2, 4, 8, 16 and 32 concurrent sessions. Each session ran
the statement 131,072 times. These statements were tested:
…
[Read more]