SQL事务与多个语句 [英] SQL Transaction vs. multiple statements

查看:148
本文介绍了SQL事务与多个语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将一些数据插入到两个不同的表中。我目前使用2个不同的SQL语句,但我从这个网站读到,这是可能在一个事务中这样做。



哪种方法更好,数据的大小?目前我只是在同一时间插入2个单独的行到2个不同的表,无序,所以我认为2个语句更好。



编辑:对不起,我不是非常清楚:我工作的数据库是微不足道的,我只是想知道一个事务与2个单独的语句的性能差异,以及性能差异,因为你做更多的语句。

交易。以这种方式,如果其中一个查询遇到问题,您可以 rollback 。您可以在 try-catch 语句中实现此操作。如果查询是完全不相关的每个其他,你可以去两个单独的SQL语句。



但我认为,由于数据是相同的两个表,最好做一个事务。因此,如果您将来必须进行连接,删除等操作,那么您的数据库仍然一致。


I am inserting some data into 2 different tables. I'm currently using 2 different SQL statements but I've read from this site that it's possible to do this in a transaction.

Which method is better, with regards to the size of the data? Currently I'm just inserting 2 seperate rows at the same time into 2 different tables, unordered, so I think 2 statements is better.

EDIT: Sorry I wasn't very clear: the database I'm working on is trivial, I just wanted to know the performance difference of a transaction vs. 2 seperate statements, and the performance difference as you do more statements.

解决方案

If the data are correlated, which means that the data stored by the second sql depend, are linked in some way with the data which are stored by the first query and/or vice-versa, then you should consider to make a transaction. In such this way, you could rollback if one of the queries encounter a problem. You can implement this in a try-catch statement. If the queries are totally unrelated each-other, you can go with two separate SQL statements.

However I think that, as the data are the same for the two tables, it would be better to do a transaction. So, if you'll have to make operations like joins, deletes, etc. in the future, you have a consistent database yet.

这篇关于SQL事务与多个语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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