SQL Clone
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in

Is SELECT Identity() … INTO Minimally Logged in Simple Recovery?

Is SELECT Identity() … INTO Minimally Logged in Simple Recovery?

Extreme Logging

Extreme Logging

I was having a discussion with Idera (@idera_software) dev lead Vicky Harp (blog|@vickyharp) about minimizing log impact when performing schema changes on really large tables. One option discussed was using SELECT INTO to create a new table and populate it in a minimally logged operation. We would also need to add an identity column to the new table definition, and we would include the IDENTITY() function to create it.

Later that evening, Vicky forwarded a link to me for a blog post by Kalen Delaney (site|blog|@sqlqueen) about how the IDENTITY function changes the way SQL Server logs the SELECT INTO clause when in the full recovery model or bulk-logged recovery model. Kalen clearly demonstrated that you would not get the same savings in logging if you used the IDENTITY function.

There is great information in this blog post, I highly recommend that you pause reading this blog post long enough to go read Kalen’s blog post. Just be sure to come back, becaue I’m going to add on to what Kalen says.

The blog post: Geek City: What gets logged for SELECT INTO operations?

What About Simple Recovery?

So if you just read Kalen’s blog post, you may be wondering what I was wondering when I read it. What about simple recovery? For the schema changes that we are planning, we were considering switching the database to simple recovery for the duration of the upgrade. I wanted to verify what impact using IDENTITY with SELECT INTO would have so I set up my own little test.

First step of the test is to create a database for the test and make sure it is in simple recovery model.

Use master;

If DB_ID('TestSimple') Is Not Null
    Drop Database TestSimple;

Create Database TestSimple;

Alter Database TestSimple Set Recovery Simple;

Use TestSimple;

Next thing is to see how much gets logged with just a Select. For this part of the test, I create the table using SELECT INTO with criteria of 1 = 2 so that no rows are inserted. The schema gets created, but the table is left empty. Then I clear out the log (checkpoint) and perform the insert.

-- First off, let's do the same insert without SELECT INTO
-- Use SELECT INTO to create an empty table, then insert data
Select IDENTITY(int, 1,1) as ID, * into dbo.AllDatabases
From sys.databases
where 1 = 2;

-- Clear the active log

-- Insert the data
Insert Into dbo.AllDatabases (
Select *
From sys.databases

-- Let's look at the log records
Select [Current LSN], Operation, Context, [Transaction ID], [Log Record Length], [Log Reserve]
from fn_dblog(null, null)

Expand the result set below and look at the log records (or just run the code and look at real result sets. I call out a few rows in it. Throughout the transaction, you will notice that for each row inserted, there are a pair of log entries, one for the identity value (LOP_IDENT_NEWVAL) and one for the row insert(LOP_INSERT_ROWS). the actual row inserts are easy to pick out because they have a longer record length and an additional log reserved.

[[Visit blog to check out this spoiler]]

Now let’s see how much log is created for the SELECT INTO with IDENTITY. I’ll clear the log and then run the insert and take another look at the log file.

-- Clear the active log

Select IDENTITY(int,1,1) as ID Into dbo.AllDatabases2
From sys.databases;

-- Let's look at the log records
Select [Current LSN], Operation, Context, [Transaction ID], [Log Record Length], [Log Reserve]
from fn_dblog(null, null);

You will see a lot more log records here because we are not able to filter out the creation fo the table. You see a lot of records for creating the table and managing the allocations. What you don’t see though are log records for each individual row. If you scroll down to the section with the identity values being created (LOP_IDENT_NEWVAL operations), you will see a long stream of identity increments.

[[Visit blog to check out this spoiler]]


Long story short, inserting the log records is minimally logged as the individual rows are not being logged individually. The identity values are being fully logged (individually). Even in simple recovery model, using the IDENTITY function still creates additional overhead for logging. It is, for the most part, still minimally logged, however.


Leave a comment on the original post [www.sqlsoldier.com, opens in a new window]

Loading comments...