3

我才意识到我多年来一直头疼。嗯,打个比方。实际上,我正在查看我的数据库结构,不知何故才意识到我从不使用事务。多哈。

互联网上有很多关于交易的数据(begin transactionrollbackcommit等),但令人惊讶的是,关于它们为何如此重要以及究竟有多重要的细节却并不多。

如果出现问题,我理解处理的概念。当一个人进行多次更新时,这是有道理的,例如,一次在多个表中,但据我所知,这是一种不好的做法,我不这样做。我所有的查询都只更新一张表。如果查询出错,它会取消,交易或不交易。除了我从服务器中拔出插头之外,还有什么可能出错或可能破坏一个表更新?

换句话说,我的问题是,

我在所有表上实现事务究竟有多重要——我完全亵渎了没有它们,或者它真的那么重要吗?

更新

+1 to invisal,他指出查询会自动包装为事务,我不知道。指出了关于我的问题的多个很好的参考资料。

4

5 回答 5

4

This made a lot of sense when one is doing multiple updates, for example, in multiple tables in one go. But basically all of my queries just update one table at a time. If a query errors, it cancels, transaction or no transaction.

In your case, it does nothing. A single statement has its own transaction itself. For more information you can read the existed question and answers:

于 2013-10-06T05:06:48.283 回答
1

Most important property of the database is to keep your data, reliably.

Database reliability is assured by conforming to ACID principles (Atomicity, Consistency, Isolation, Durability). In the context of databases, a single logical operation on the data is called a transaction. Without transactions, such reliability would not be possible.

In addition to reliability, using transactions properly lets you improve performance of some data operations considerably. For example, you can start transaction, insert a lot of data (say 100k rows), and only then commit. Server does not have to actually write to disk until commit is called, effectively batching data in memory. This allows to improve performance a lot.

于 2013-10-06T05:06:51.927 回答
1

您应该知道,针对您的数据库的每个更新操作都是在一个事务中执行的,即使只有一个表(SQL 服务器会自动为其创建一个事务)。 正如其他人提到的那样,总是进行事务处理的原因是为了确保 ACID。这里我想详细说明一下隔离点。如果没有事务隔离,您可能会遇到以下问题:未提交读、不可重复读、幻读、..

于 2013-10-06T05:13:46.060 回答
0

Well it depends, SQL is most of the times used for supporting data for some host languages like c, c++, java, php, c# and others. Well I have not worked with much technologies.. but if you are using following combinations then here is my point of view:

SQL with C / C++ : Commit Required

SQL with Java : Not Required

SQL with C# : Not Required

SQL with PHP : Not Required

And it also depends which SQL you are using. It would also depend from different flavors of SQL like Oracle SQL, SQL Server, SQLite, MySQL etc...

When you are using Oracle SQL in its console, like Oracle 11g, Oracle 10g etc... COMMIT is required.

And as far as corruption of table and data is concerned. YES it happens, I had a very bad experience with it. So, if you pull out your wire or something while you are updating in your table, then you might end up with a massive disaster.

Well concluding, I will suggest you to do commit.

于 2013-10-06T05:07:59.880 回答
0

这取决于您是否要更新一个表和一行,那么唯一的优势就是在日志记录中...但是如果您一次更新表中的多行...如果没有事务,您仍然可能会遇到一些损坏

于 2013-10-06T04:58:22.280 回答