问题
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?
Here is what I read:
This option issues a BEGIN SQL statement before dumping data from the server. It is useful only with transactional tables such as InnoDB and BDB, because then it dumps the consistent state of the database at the time when BEGIN was issued without blocking any applications."
Can some elaborate on this?
回答1:
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
- 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.
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.
回答2:
I used to run into problems where mysqldump without the --single-transaction parameter would consistently fail due to data being changed during the dump. As far as I can figure, when you run it within a single transaction, it is preventing any changes that occur during the dump from causing a problem. Essentially, when you issue the --single-transaction, it is taking a snapshot of the database at that time and dumping it rather than dumping data that could be changing while the utility is running.
回答3:
This can be important for backups because it means you get all the data, exactly as it is at one point in time.
So for example, imagine a simple blog database, and a typical bit of activity might be
- Create a new user
- Create a new post by the user
- Delete a user which deletes the post
Now when you backup your database, the backup may backup the tables in this order
- Posts
- Users
What happens if someone deletes a User, which is required by the Posts, just after your backup reaches #1?
When you restore your data, you'll find that you have a Post, but the user doesn't exist in the backup.
Putting a transaction around the whole thing means that all the updates, inserts and deletes that happen on the database during the backup, aren't seen by the backup.
来源:https://stackoverflow.com/questions/10705149/why-is-a-mysqldump-with-single-transaction-more-consistent-than-a-one-without