jbogard / Respawn

Intelligent database cleaner for integration tests

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Use `TRUNCATE TABLE` with Sql Server

giggio opened this issue · comments

If we truncate the tables the performance should be better, as no logs are written.

See:
https://docs.microsoft.com/en-us/sql/t-sql/statements/truncate-table-transact-sql?view=sql-server-2016

It is supported in all supported Sql Server versions.

My understanding is that all we need is to change this line, right?

builder.AppendLine($"DELETE {table.GetFullName(QuoteCharacter)};");

Truncate table requires elevated rights and all constraints must be removed.

The README mentions truncation:

In benchmarks, a deterministic deletion of tables is faster than truncation, since truncation requires disabling or deleting foreign key constraints.

Also the blog post touches on truncation vs deletion: https://lostechies.com/jimmybogard/2013/06/18/strategies-for-isolating-the-database-in-tests/

Based on the research and benchmarks the maintainer has performed, DELETE seems like the faster approach.