Question

I read about minimal logging and that one should use with (tablock) for insert selects. I had high expectations when I added this but it does not seem to make a difference. I use it in ETL and tables are empty (truncated) and the recovery model is "simple".

Am I missing something? I also see tablockx sometimes but when I read about minimal logging it mentioned specifically tablock. E.g. here it does not mention tablockx https://docs.microsoft.com/en-us/sql/relational-databases/import-export/prerequisites-for-minimal-logging-in-bulk-import?view=sql-server-ver15

Was it helpful?

Solution

How many rows are you inserting at one time? Despite what the comments say, minimal logging does affect performance, but it's very minute and maybe a moot thing to worry about.

Using the TABLOCK query hint moreso helps with large data insert performance because it tells the SQL Optimizer to take a Table Lock upfront, as opposed to going through Lock Escalation which can be costly at runtime for large batches of data.

Generally without that hint, an insert might cause a bunch of Row Level Locks to occur first, and then when the SQL Engine realizes it crossed a certain threshold of Row Level Locks, it escalates to a Table Lock anyway, which means it wasted it's time spinning it's wheels acquiring Row Level Locks to start with. This is why for a large insert of rows, using that hint can be helpful.

Typically you need to be inserting at least a certain number of rows (I believe at least 102,400 rows per parallel thread executing the insert) for the TABLOCK query hint to be helpful with performance, otherwise it doesn't matter or in some cases can even hinder performance as Erik Darling wrote up in this interesting article.

Licensed under: CC-BY-SA with attribution
Not affiliated with dba.stackexchange
scroll top