I wonder how SqlDataAdapter works internally, especially when using UpdateCommand for updating a huge DataTable (since it's usually a lot faster that just sending sql statements from a loop).
Here is some idea I have in mind :
- It creates a prepared sql statement (using
SqlCommand.Prepare()) withCommandTextfilled and sql parameters initialized with correct sql types. Then, it loops on datarows that need to be updated, and for each record, it updates parameters values, and callSqlCommand.ExecuteNonQuery(). - It creates a bunch of
SqlCommandobjects with everything filled inside (CommandTextand sql parameters). Several SqlCommands at once are then batched to the server (depending ofUpdateBatchSize). - It uses some special, low level or undocumented sql driver instructions that allow to perform an update on several rows in a effecient way (rows to update would need to be provided using a special data format and a the same sql query (
UpdateCommandhere) would be executed against each of these rows).
It uses an internal facility of the SQL Server client classes which is called command sets. You can send multiple batches with a single command to SQL Server. This cuts down on per-call overhead. You have less server roundtrips and such.
A single row is updated per statement, and one statement per batch is sent, but multiple batches per roundtrip are send. The last point in this list is the magic sauce.
Unfortunately, this facility is not publicly exposed. Ayende took a hack on this and built a private-reflection bases API for it.
If you want more information I encourage you to look at the internal SqlCommandSet class.
That said, you can go faster than this by yourself: Transfer the update data using a TVP and issue a single UPDATE that updates many rows. That way you save all per-batch, per-roundtrip and per-statement overheads.
Such a query would look like this:
update T set T.x = @src.x from T join @src on T.ID = @src.ID
来源:https://stackoverflow.com/questions/13962704/how-sqldataadapter-works-internally