What's the best way to do an insert/update in MySQL in one query?
I'm looking to optimize a single query 开发者_开发知识库to my server (which will be used by tens or hundreds of outside servers) that will allow players (minecraft) stats to be inserted remotely and read without issue.
Currently, I'm using:
INSERT DELAYED INTO servers_players (a,b,c) VALUES(1,2,3) ON DUPLICATE KEY UPDATE c=c+3, ...;
Would this be the best way to optimize the query or is there a better way?
Thank you,
Billy
Please note that DELAYED won't work, as per http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html:
The DELAYED option is ignored when you use ON DUPLICATE KEY UPDATE.
As for how to optimize, it is already pretty optimal compared to separate INSERT/UPDATE combo in general. However, precise usage patterns might affect it (e.g. if the VAST majority of your operations would be UPDATEs (very little newly inserted rows), doing an UPDATE first and then inserting if UPDATE updates zero rows might be more efficient.
精彩评论