How SNAPSHOT ISOLATION works
2
votes
1
answer
184
views
On SQL Server 2019 with ADR disabled:
SET TRANSACTION ISOLATION LEVEL SNAPSHOT;
BEGIN TRAN;
WAITFOR DELAY '00:00:20'; -- insert of one record to dbo.MARA_CT in different session
SELECT
*
FROM dbo.MARA_CT AS mc;
WAITFOR DELAY '00:00:20'; -- insert of one record to dbo.Album in different session
SELECT
*
FROM dbo.Album AS a;
WAITFOR DELAY '00:00:20';
SELECT
*
FROM dbo.MARA_CT AS mc;
SELECT
*
FROM dbo.Album AS a;
COMMIT;
I inserted one row into dbo.MARA_CT
and during the second WAITFOR DELAY
, I inserted a record into dbo.Album
.
According to Microsoft documentation:
> The term "snapshot" reflects the fact that all queries in the transaction see the same version, or snapshot, of the database, based on the state of the database at the moment in time when the transaction begins.
The results from my query:
1. dbo.MARA_CT
**contains** the record inserted during the first WAITFOR DELAY '00:00:20'.
2. dbo.Album
**does not contain** the record inserted during the second WAITFOR DELAY '00:00:20'.
Does the first touch of any table in the query create a "snapshot" for the entire transaction rather than capturing the state of the database at the moment the transaction begins?
Does SNAPSHOT ISOLATION store all versions from the beginning of the transaction for all rows in the database, or only for the tables used in the query?
How does this work?
If RCSI is enabled and we subsequently enable SNAPSHOT ISOLATION, does SQL Server store any new values/versions in tempdb?
My use of SNAPSHOT ISOLATION is to test new versions of procedures and views (new schemas). Both the new and old procedures need to return the same number of records and utilize the same data, even as the tables involved experience constant inserts, updates, and deletes.
Would SNAPSHOT ISOLATION be more advantageous than a database snapshot since RCSI is already enabled on the databases?
Asked by adam.g
(465 rep)
Apr 18, 2025, 01:28 PM
Last activity: Apr 24, 2025, 05:03 PM
Last activity: Apr 24, 2025, 05:03 PM