在更新表时防止读取 [英] Prevent read when updating the table

查看:80
本文介绍了在更新表时防止读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MySQL中:

每一分钟,我清空表格并填入新的资料。现在我想用户不应该在填充过程中读取数据,之前或之后是确定。

Every one minute I empty the table and fill it with a new data. Now I want that users should not read data during the fill process, before or after is ok.

如何实现这一点?
是事务的方式?

How do I achieve this? Is transaction the way?

推荐答案

假设你使用一个事务引擎(通常Innodb),清除并重新填充表

Assuming you use a transactional engine (Usually Innodb), clear and refill the table in the same transaction.

请确保您的读者使用READ_COMMITTED或更高的事务隔离级别(默认值为REPEATABLE READ更高)。

Be sure that your readers use READ_COMMITTED or higher transaction isolation level (the default is REPEATABLE READ which is higher).

这样,读者在更新过程中仍然能够读取表格的旧内容。

That way readers will continue to be able to read the old contents of the table during the update.

有几件事情要做小心:


  • 如果表非常大,以至于耗尽了回滚区域 - 如果您更新整个一个1M行表。当然这是可调的但有限制

  • 如果事务部分失败并被回滚 - 回滚大事务在InnoDB中非常低效(它是针对提交进行优化的,而不是回滚)

  • 注意死锁和锁等待超时,如果您使用大交易,则更有可能。

这篇关于在更新表时防止读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆