Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How much does wrapping inserts in a transaction help performance on Sql Server?

Ok so say I have 100 rows to insert and each row has about 150 columns (I know that sounds like a lot of columns, but I need to store this data in a single table). The inserts will occur at random, (ie whenever a set of users decide to upload a file containing the data), about a 20 times a month. However the database will be under continuous load processing other functions of a large enterprise application. The columns are varchars, ints, as well as a variety of other types.

Is the performance gain of wrapping these inserts in a transaction (as opposed to running them one at a time) going to be huge, minimal, or somewhere in between?

Why?

EDIT: This is for Sql Server 2005, but I'd be interested in 2000/2008 if there is something different to be said. Also I should mention that I understand the point about transactions being primarily for data-consistency, but I want to focus on performance effects.

like image 858
Mark Rogers Avatar asked Feb 06 '09 19:02

Mark Rogers


People also ask

How increase SQL Server insert performance?

Drop Index before Insertion of Data We should drop the index before insertion of a large amount of data. This makes the insert statement run faster.

Why bulk insert is faster?

In case of BULK INSERT, only extent allocations are logged instead of the actual data being inserted. This will provide much better performance than INSERT. The actual advantage, is to reduce the amount of data being logged in the transaction log.

How many inserts per second can SQL Server handle?

On decent "commodity hardware" (unless you invest into high performance SSDs) this is about what you can expect: SQLite: 30 inserts/s. MySQL: 80 inserts/s.

Which is faster insert or update SQL Server?

Insert would be faster because in case of update you need to first search for the record that you are going to update and then perform the update.


1 Answers

While transactions are a mechanism for keeping data consistent they actually have a massive impact on performance if they are used incorrectly or overused. I've just finished a blog post on the impact on performance of explicitly specifying transactions as opposed to letting them occur naturally.

If you are inserting multiple rows and each insert occurs in its own transaction there is a lot of overhead on locking and unlocking data. By encapsulating all inserts in a single transactions you can dramatically improve performance.

Conversely if you have many queries running against your database and have large transactions also occurring they can block each other and cause performance issues.

Transactions are definitively linked with performance, regardless of their underlying intent.

like image 143
Not loved Avatar answered Oct 09 '22 19:10

Not loved