简体   繁体   English

ADO.NET SQL事务-仅1个对数据库的调用?

[英]ADO.NET SQL Transaction - Only 1 call to database?

I am reviewing some code which is inserting some data into a MSSQL Database (MSSQL2014) using a Stored Procedure. 我正在查看一些代码,该代码使用存储过程将一些数据插入MSSQL数据库(MSSQL2014)。

Each row of data results in a call to that database. 每一行数据都会导致对该数据库的调用。

This is obviously inefficient for a number of reasons; 显然,由于多种原因,这种方法效率低下。 not least of which is the fact that a call has to be made over the network for each line of data. 不仅如此,事实是必须通过网络为每一行数据进行呼叫。

I am surmising that if they simply wrap all these individual sqlcommand calls (ExecuteNonQuery) into a transaction then this will ultimately result in a "batch insert" of sorts. 我猜想,如果它们只是将所有这些单独的sqlcommand调用(ExecuteNonQuery)包装到一个事务中,那么最终将导致某种“批处理插入”。 When you actually "COMMIT" the transaction, the call accross the connection is made. 当您实际“提交”事务时,将在整个连接上进行调用。

Is this correct? 这个对吗? Will this send all the sqlcommands to the server in a single call? 这会在一次调用中将所有sqlcommand发送到服务器吗? I have not been able to find a diagram or documentation which outlines the communication between the client and server when a transaction is used. 我找不到能够概述使用事务时客户端和服务器之间的通信的图表或文档。

--> Begin Transaction

-> ExecuteNonQuery
-> ExecuteNonQuery
-> ExecuteNonQuery
-> ExecuteNonQuery
-> ExecuteNonQuery
-> ExecuteNonQuery
-> ExecuteNonQuery
-> ...

--> Commit Transaction (my assumption is that each of the SqlCommands 
    are sent over the connection object at this point)

On a sidenote I am more inclined to recommend that the developer rewrites the routine to make use of SQLBulkCopy or TableValued Parameters. 在旁注中,我更倾向于建议开发人员重写例程以使用SQLBulkCopy或TableValued参数。 This would however necessitate the re-factoring of the database stored procedure. 但是,这将需要对数据库存储过程进行重构。

Thanks in Advance 提前致谢

There will be no batch insert. 将没有批量插入。 Neither the client nor the server optimize across statements. 客户端和服务器都不会跨语句优化。 ADO.NET has no machinery to understand the SQL that you are sending. ADO.NET没有机制来理解您要发送的SQL。 It cannot optimize anything. 它无法优化任何东西。 The server could but does not. 服务器可以但不能。

There will be a performance gain by using a transaction like this because the inserts do not need to flush the log as long as the transaction is pending. 通过使用这种事务将获得性能提升,因为只要事务挂起,插入就不需要刷新日志。

There will be no batch insert. 将没有批量插入。 All the transaction will do is give you the ability to commit the inserts so others would be able to see the changes. 事务要做的就是使您能够提交插入内容,以便其他人可以看到更改。 No performance gains and if anything a performance hit. 没有性能提升,并且如果有任何性能下降。 Not knowing what you are wanting to do here it would be hard to give you a proper answer 不知道你想在这里做什么,很难给你一个正确的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM