Many Individual Queries v. One Big One

百般思念 提交于 2019-12-24 16:26:15

问题


I'm in a situation where an entire column in a table (used for user tokens) needs to be wiped, i.e., all user tokens are reset simultaneously. There are two ways of going about it: reset each user's token individually with a separate UPDATE query; or make one big query that affects all rows.

The advantage of one big query is that it will obviously be much faster, but I'm worried about the implications of a large UPDATE query when the database is big. Will requests that occur during the query be affected?


回答1:


Afraid it's not that simple. Even if you enable dirty reads, running one big update has a lot of drawbacks:

  • long running transaction that updates one column will effectively block other insert, update and delete transactions.
  • long running transaction causes enourmous load on disk because server is having to write to a log file everything that is taking place so that you can roll back that huge transaction.
  • if a transaction fails, you would have to rerun it entirely, it is not restartable.

So if simultaneous requirement can be interpreted "in one batch that may take a while to run", I would opt for batching it. A good research write up on performance of DELETEs in MySql is here: http://mysql.rjweb.org/doc.php/deletebig, and I think most of the findings are applicable to UPDATE.

The trick will be finding the optimal "batch size".

Added benefits of batching is that you can make this process resilient to failures and restart-friendly.




回答2:


The answer depends on the transaction and isolation level you've established.

You can set isolation to allow "dirty reads", "phantom reads", or force serialization of reads and writes.

However you do that UPDATE, you'll want it to be a single unit of work.

I'd recommend minimizing network latency and updating all the user tokens in one network roundtrip. This means either writing a single query or batching many into one request.



来源:https://stackoverflow.com/questions/13505415/many-individual-queries-v-one-big-one

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!