问题描述
我已经阅读了手册,并提到每个事务在开始进行转储之前都会添加一个BEGIN
语句.有人可以用更容易理解的方式阐述这一点吗?
I have gone through the manual and it was mentioned that every transaction will add a BEGIN
statement before it starts taking the dump. Can someone elaborate this in a more understandable manner?
这是我读到的:
可以详细说明一下吗?
推荐答案
由于转储在一个事务中,因此您可以获得数据库中所有表的一致视图.最好用一个反例来解释.假设您转储具有两个表Orders
和OrderLines
Since the dump is in one transaction, you get a consistent view of all the tables in the database. This is probably best explained by a counterexample. Say you dump a database with two tables, Orders
and OrderLines
- 您无需进行单个交易即可开始转储.
- 另一个进程在
Orders
表中插入一行. - 另一个进程在
OrderLines
表中插入一行. - 转储处理
OrderLines
表. - 另一个进程将删除
Orders
和OrderLines
记录. - 转储处理
Orders
表.
- You start the dump without a single transaction.
- Another process inserts a row into the
Orders
table. - Another process inserts a row into the
OrderLines
table. - The dump processes the
OrderLines
table. - Another process deletes the
Orders
andOrderLines
records. - The dump processes the
Orders
table.
在此示例中,您的转储将具有OrderLines
的行,但没有Orders
的行.如果Orders
和OrderLines
之间有外键,则数据将处于不一致状态,并且还原时将失败.
In this example, your dump would have the rows for OrderLines
, but not Orders
. The data would be in an inconsistent state and would fail on restore if there were a foreign key between Orders
and OrderLines
.
如果您是在单个事务中完成的,则转储将既没有顺序也没有行(但是将是一致的),因为在事务开始之后,两者都被插入然后被删除了.
If you had done it in a single transaction, the dump would have neither the order or the lines (but it would be consistent) since both were inserted then deleted after the transaction began.
这篇关于为什么具有单事务的mysqldump比没有事务的mysqldump更一致?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!