What the heck is the SQL Server Version Store?
Tags: sql, sqlserver
Published: 2020-01-01 14:10:55

---
title: What the heck is the SQL Server Version Store?
published: true
date: 2019-10-15 14:30:13 UTC
tags: sql, sqlserver
canonical_url: https://am2.co/2019/10/what-the-heck-is-the-sql-server-version-store/
---

[![](http://am2.co/wp-content/uploads/MicrobusVersioning.jpg) ](https://www.youtube.com/watch?v=zEEXetPS_lY)<figcaption id="caption-attachment-2222">Sure, Microsoft does versioning, but check out this Microbus versioning!</figcaption>

Maybe you’ve read about the [Read Committed Snapshot](https://sqlperformance.com/2014/05/t-sql-queries/read-committed-snapshot-isolation) (RCSI) or [Snapshot](https://sqlperformance.com/2014/06/sql-performance/the-snapshot-isolation-level) Isolation levels. Or maybe you were trying to figure out [the difference between the two snapshot isolation levels](https://littlekendra.com/2016/02/18/how-to-choose-rcsi-snapshot-isolation-levels/). Perhaps you’ve looked into [read-only secondary replicas in your Availability Group](https://docs.microsoft.com/en-us/sql/database-engine/availability-groups/windows/active-secondaries-readable-secondary-replicas-always-on-availability-groups), or even [had some problems with AG secondaries](https://dba.stackexchange.com/questions/237671/long-running-query-on-read-only-replica-that-takes-moments-on-the-primary). Along the way, people and articles have mentioned this “version store” thing.

## What the heck is the version store?

If I don’t know what something is, I’m going to [Google it](https://lmgtfy.com/?q=version+store). Turns out, the results for “version store” were not what I expected:

[![Google suggests Verizon Store](http://am2.co/wp-content/uploads/VersionStore-1024x521.png)](https://www.youtube.com/watch?v=QK8mJJJvaes)<figcaption id="caption-attachment-2221">I’m not looking for a cell phone, I want a bargain on some used row versions.</figcaption>

  

Yikes. That’s not a great start. Let’s see if I can help. Let’s talk about [what it is, what it does, what it is, what it isn’t](https://youtu.be/2zNSgSzhBfM?t=85).

### What it is

When using an [optimistic locking](https://docs.microsoft.com/en-us/dotnet/framework/data/adonet/optimistic-concurrency) isolation level, SQL Server introduces row versions, kept in the **version store** which allow read queries to not block or be blocked. When a row is updated or deleted, the “stale” _version_ gets _stored_ in the version store. The version store isn’t part of a database itself, but rather is [in tempdb](https://ozar.me/2013/02/why-not-everybody-loves-my-sessions/). This means that every database on the instance shares a single version store. This also means that all your snapshot-enabled databases will need to write to the tempdb version store–a new shared bottleneck for your separate databases. The overhead of this isn’t huge, but it _is_ extra I/O on tempdb.

### What it does

Before we talk about how the version store works, there’s one other ingredient we’ll need: the row timestamp/version itself. I already mentioned that the “stale” version of the row goes into the version store, but how do we tell which version is which? If I update a row twice, and send two stale versions into the version store, I need to know which row is newer. SQL Server does this by storing a 14-byte timestamp as the version identifier on the row. This timestamp tells SQL Server when the row was written. This completely solves the “Which version is newer? Which version do I want?” problem.

Now that we have the version timestamp on the row, every time we update the row, SQL Server quietly copies the old version of the row into the version store. Every update in the database generates an entry in the version store. Now, when you query under RCSI or Snapshot isolation, SQL Server can notice “whoops, the current version is too new! I better read an older one from the version store!” then it can go read the right one from the version store. (For a more accurate & detailed description of how this works, check out the [series from Paul White](https://sqlperformance.com/2014/07/t-sql-queries/isolation-levels) ([blog](http://sql.kiwi)|[twitter](https://twitter.com/SQL_Kiwi)) that I referenced earlier.)

But wait, if all these updates are writing to the version store, isn’t tempdb going to be huge? In order to prevent you from needing a 500 Petabyte tempdb, SQL Server will clean up row versions that it doesn’t need. SQL Server will keep track of the oldest open transaction or query, and clean up anything in the version store older than that point in time using a background process. This means that a [long running transaction](https://am2.co/2018/01/finding-active-transactions/) could both grow your transaction log and grow your tempdb. It doesn’t even have to be a transaction. It could just be a very long running `SELECT` statement that will prevent the version store from being cleaned up, as even read-only queries (ie without a transaction) still need to access the version store to ensure consistent reads.

### What it isn’t

It’s not magical, that’s for sure. It’s also not guaranteed to be fast. When the version store gets _really_, _ **really** _ large, performance can take a hit. I’ve recently witnessed a server have its version store grow to 200+GB, and at the same time watched read queries that use that version store slow to a crawl. That one read-only analytical query running in the background (using RCSI… readers don’t cause blocking!), caused the version store growth to impact _everything_ else running on the server. It wasn’t causing blocking, but the overhead of reading the massive version store still caused impact.

The post [What the heck is the SQL Server Version Store?](https://am2.co/2019/10/what-the-heck-is-the-sql-server-version-store/) appeared first on [Andy M Mallon - AM²](https://am2.co).