Back in April I started a Series on the Backwards Compatibility level. There are a lot of myths around Compatibility Level, in Books Online, (BOL), you can find plenty of literature that states that Compatibility Level determines the set of rules that the Relation Engine uses when judging syntax, creating Query Trees, and Execution Plans. However I’ve seen plenty of places where in the forums people will say that you cannot use a feature managed by the Storage Engine, when the database is set to 80 or 90 Compatibility, and that is flat out wrong.
Previously on SQLBalls we established that you can use Transparent Data Encryption and Page & Row Compression with Databases set to 80 and 90 Compatibility Level. Today we’ll do a quick exercise to show that we can use Snapshots against an 80 Compatible Database.
“But Balls,” You say, “You don’t care about 90 Compatibility and Snapshots?”
Great Question Dear Reader, Database Snapshots were introduced in SQL 2005. When you look at Compatibility levels, level 90 was introduced in 2005, therefore the only Compatibility level we would need to test out is 80.
UNLIKE HARRY POTTER THESE SNAPSHOTS DON’T MOVE
Okay I needed something to go with the picture, but hang with me. A Database Snapshot is like a picture. You take a Picture and you have that moment in time captured. Database Snapshots are a very similar concept.
Behind the Scenes a Snapshot is Managed by the Buffer Manager inside of the Storage Engine. When Data is changed in memory a copy of that Data, before it is modified is written out to the Snapshot. You see initially a Snapshot doesn’t have anything to it. It isn’t until you change Data that you start populating a Snapshot. As a matter of fact if you request a page that hasn’t changed, even if you query the snapshot you will be reading from the Datafile of the Database the Snapshot was taken of.
Snapshots will start out with very little actual space used, and their maximum size is the size of the Database the Snapshot was taken of, at the moment the Snapshot was taken.
Enough Explaining let’s get to some doing!
First we will take our database from our SQL 2000 instance and we will restore it. How do you know it is from 2000, because the Database Internal Version number is incremented when moving from one version to the next at the end of Recovery. And as you can see from the picture below we start at 539 and we go to 661, or in other words our internal version is going from SQL 2000 to SQL 2008 R2 RTM.
So now we’ve got our database restored.
I’ve got one table dbo.heap1, and a quick count will show we have 15,000 records.
COUNT(*) AS [dbo.heap1 Count]
Let’s take our snapshot.
CREATE DATABASE snapShotTest_Snapshot ON
( NAME = snapShotTest_Data, FILENAME = 'C:\Program Files\Microsoft SQL Server\MSSQL10_50.BRADSQL2008R2\MSSQL\DATA\snapShotCompatLvl.ss')
AS SNAPSHOT OF snapShotTest ;
Then we’ll delete 10,000 records from our database.
DELETE FROM dbo.heap1 WHERE myid BETWEEN 1 AND 10000
Now let’s do a select count from our table, and we’ll see we only have 5,000 records left.
COUNT(*) AS [dbo.heap1 Count]
Now let’s do the same count against our Snapshot.
COUNT(*) AS [dbo.heap1 Snapshot Count]
And we will see that our Count is still 15,000. We dropped the records and our Snapshot still reflects the number of records we had before we dropped it.
Let’s confirm that our database is still in 80 Compatibility Level.
And it is. So just to summarize you can use Transparent Data Encryption, Page & Row Data Compression, and Snapshots with a database in 80 Compatibility level. Tune in next time Dear Reader when we wrap up the Series.