Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the consequences of using the table lock option with a fast load destination?

I'm trying to speed up a fact table load as part of an overall performance project. The table is just about 120 million rows about 100k are added each evening. The table is pretty heavily indexed.

Currently I'm using an SSIS Fast Load OLE DB destination and loading the 100,000 rows takes about 15 minutes. This seemed really high to me to insert 100k rows, so I altered the package to dump it's results into a staging table, then did an T-SQL insert into the fact table from that staging table. The insert now runs in less than 1 minute.

I found it quite odd that a plain old T-SQL insert would be faster than SSIS Fast Load, so I started looking at which boxes were checked on the OLEDB destination. It turns out Table Lock was NOT checked. When I checked this option, the SSIS load is now under 1 minute. My questions are:

  • What are the implications of leaving Table Lock checked?
  • Does the T-SQL insert statement issue a table lock by default and that's why it was initially faster?
like image 557
Jeffrey Bane Avatar asked May 20 '13 16:05

Jeffrey Bane


People also ask

What is table lock in SSIS?

The LOCK TABLE statement allows you to explicitly acquire a shared or exclusive table lock on the specified table. The table lock lasts until the end of the current transaction. To lock a table, you must either be the database owner or the table owner.

What does a table lock do?

A lock is a flag associated with a table. MySQL allows a client session to explicitly acquire a table lock for preventing other sessions from accessing the same table during a specific period. A client session can acquire or release table locks only for itself.

What is SQL table lock?

Locks are held on SQL Server resources, such as rows read or modified during a transaction, to prevent concurrent use of resources by different transactions. For example, if an exclusive (X) lock is held on a row within a table by a transaction, no other transaction can modify that row until the lock is released.

Does inserting lock a table?

When inserting a record into this table, does it lock the whole table? Not by default, but if you use the TABLOCK hint or if you're doing certain kinds of bulk load operations, then yes.


1 Answers

Well, I think the explanation is straightforward (see a more detailed reference here):

For your first question:

Table Lock – By default this setting is checked and the recommendation is to let it be checked unless the same table is being used by some other process at same time. It specifies a table lock will be acquired on the destination table instead of acquiring multiple row level locks, which could turn into lock escalation problems.

As for the insert statement, considering the rather large number of rows that need to be inserted, then SQL Server will most likely choose to make a table lock.

To confirm this you can check what kind of locks are held on the table by using the sys.dm_tran_locks DMV. Here are few good samples on how to interpret the results (and also good reading on lock escalation): http://aboutsqlserver.com/2012/01/11/locking-in-microsoft-sql-server-part-12-lock-escalation/.

like image 191
Marcel N. Avatar answered Nov 05 '22 08:11

Marcel N.