简体   繁体   中英

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM