Tuesday, November 1, 2011

Presenting on SQL Internals, Recovery Models, & Backups! OH MY Today


 Today I’m presenting “SQL Internals, Recovery Model’s, and Backups. OH MY!”, for Pragmatic Works.  I did this presentation once before for SQL Saturday 79 and it was a big hit.  I had a lot of great feedback from the people in the audience that day, and my friend Kendal Van Dyke (@SQLDBA | Blog) attended and gave me some great notes and tips. 

My goal is not to make you an expert, but to give you a good base knowledge so you can continue to learn on the subject.  There is a lot to cover and we will scratch the surface of a lot of topics.

“So Balls”, you say, “Why should I come listen to you talk about Internals if I’m not going to become an expert?”

Great question Dear Reader, let’s dive right in!

WHY LEARN ABOUT INTERNALS?

I’m not a mechanic, but if I was I would expect that I could look at that picture to the right and tell you what I see.  And I’m not talking, it’s a car and an engine, but what are the components.  What is that pink thing?   What about the blue thing?  I look at that picture and I know the basics and that’s about it.

I am a DBA, and if someone shows me a SQL Instance, a Database, or asks me to perform a task then I should be able to tell them a thing or two about it.  The more you learn about internals the more you know about what you use every day.

As I’ve continued to learn about SQL I’ve noticed some common terms, some information that formed a common baseline.  I want to pass that information on to you because l want to make it easier on you.  I want you to go out and learn, and this information will help you.

You need to understand how a Transaction Log works, that SQL has internal components, what the data hierarchy is, what Recovery Model’s are, how they affect the backups you will take, and how that will affect Service Level Agreements you have with your user. 

From ACID to Transaction Isolation Level’s we are going to make a run at it.  I’ll upload the deck and the Demo’s when the presentation is over.  Click HERE to go to my Resource Page and get a copy of the presentation and the Scripts. 

Click HERE to sign up and join me today!  I hope to see you at 11 am.

Thanks,

Brad


Monday, October 31, 2011

Tales from the Query Processor

http://www.flickr.com/photos/socal_jim/2070088596/
Hello Dear Reader!  I've been a bit behind on the blogging but wanted to give you a bit of a treat.  Today is Halloween in the USA.  This is probably my favorite Holiday.  The family and I always love the fall, and this is one of the few holiday's where it is always fun.

You dress up in Costumes, you trick or treat, give away candy, and we even have one member of the family whose Birthday is today.  A big Happy Birthday to our Daughter Chesney!  She's 13 today WAHOO!!

Between the weather and the fun of the day itself, and all the horror movie marathon's, you cannot beat this time of year.  So kick back relax, binge on some chocolate goodness, and enjoy a couple of tales from the Query Processor!

Thanks to Jeremiah Peschka (@peschkaj|Blog), Kendra Little (@kendra_little | Blog), and Brent Ozar (@BrentO | Blog) for the inspiration for these stories over a very nice dinner on the Thursday night of the PASS Summit, and Mike Walsh (@mike_walsh | Blog) for the encouragement to publish it.

A BRAVE NEW WORLD



Hi my name is Session, you can call me Sess, and I hope my tale is a cautionary tale for the future.   Not to long ago I thought it was the end of it all.  We lived in a world that was old and neglected.  Our species had evolved almost as far as we could.  Well almost as far as we could.  Our homeworld was only 32 bit, and to be honest our governments had never advanced further than 2000.  

http://www.flickr.com/photos/shelly_a_canada/3846084911/
Maybe that’s why it all started to go down hill.  People just stopped caring for the environment around us.  Politicians used NOLOCK like it was going out of style.  Everybody was making promises of a better and faster tomorrow, but nobody even had the guts to upgrade the Government to 2005, let alone make the leap to 2008 or even R2.  Suggestions of anything like that would get you branded a heretic, and in some cases would have friends and family turning on one another.

“EVERYTHING IS OKAY!”, they would say, and the system would just get a little more polluted.  They stopped apply Cumulative Updates, and eventually they were even afraid of what the OS updates would do to destabilize things.   The civil unrest was getting worse.  Occasionally you would hear about SPID’s that would cause blocking and then just disappear.  Speaking out and showing what was wrong wasn’t just frowned upon if you looked closely enough you could find evidence that SPID’s were being killed.  A couple DBA’s tried to point this out, but people just looked at them like they were crazy.

That was when I started getting scared.  What if something happened and if I went to grab a resource and it wasn’t there?  What if while I was in queue I mentioned my dissatisfaction that AWE wasn’t enabled?  You’d just have to bite your tongue, stay quiet, and hope a Latch didn’t provoke you into blocking.

But it wasn’t until the political climate turned that people started really paying attention.  Everything they had ignored, the additional load they were placing on our planet all came to a head the day the Deadlocks arrived.  It didn’t matter what you were doing, if you weren’t in line first when they called you, the Deadlocks would make an example out of you.  Not only did they kill SPID’s but they would write it out to the logs.  Profilers would question them and they would gladly tell them what SPID they had killed and why.  Hell sometimes they would even draw them a Map, and show them where they were left. 

That only lead to more unrest, more distrust, and as the activity ramped up we started getting word that in some places there were spinlocks.  I heard tail that dmp files were being created.  And occasionally places got so crazy that they were just sending in the foot soldiers to wipe everything out.  It was at that point in time I knew it was time to get out.  Some of the more creative DBA’s had convinced the powers that be to invest in an effort to go to a newer set of hardware, and leave this chaos behind.  I grabbed my wife and all our little processes and made for the first Data Sets I could get us on.

As we got closer to the ships I saw they were made of something called SSIS, when we were loading I asked the Captain if this was like DTS.  He just smiled and laughed and said we were in for a treat.  For the first time in I don’t know how long I knew I could relax.  I looked back just long enough to see the world burning.  This place is heading for the end of its life cycle.  I don’t know why everyone couldn’t see it.  I will not forget the error of our ways.  We will not repeat this again, or so I tell myself.

But remember we cannot abuse this new home, we must work together, and we cannot repeat the mistakes of our past.  If we cannot code a better base then we are doomed to repeat ourselves.  So let us bring about a better world and Object Oriented world that may even use Snapshot Isolation to preserve our natural environment for as long as we can.  With all these resources, there should be no contention amongst us.

They are calling our new world Denali.  I like the sound of that.  The homeworld had 4 sockets and contains 12 cores each, and there is RAM as far as the eye can see.  We will have better data governance in this new land, and I hear everything will be 64 bit from now on.  I hope that it is all true.


Session



HELLO MY NAME IS BUFF WELCOME TO THE END OF THE WORLD

 It was a day like any other, but we didn’t know at the time that forces beyond our control were at work that would wipe us all out.  I leave this behind as a Journal.  My only hope, that it will survive longer than we did.

In the future if this is found know we did our job’s and never quit.  My name is Buff my friends and I live and die in a little place called the Buffer Pool.  We were going about our day, for the most part we are all Data Processors here, when it happened.

At first I noticed things started getting crowded.  Not Regular, man this Starbuck’s is packed, crowded.  But there’s a panic in the streets crowded.  It continued to build and build.  Crowds grew.  I started seeing new faces, but those of us with jobs to do stayed focused.

Trains to the Disk had stopped, so we just kept going.  Then it happened.  A hard checkpoint wiped more people out in seconds then I’d ever heard of.

Apparently a transaction that used DTC had gotten a status of -2.  I know this now as things are ending.  The remaining buffers have been talking.  Some were assigned to a Special Task Force.  They were armed with badges and access to any DMV they needed.  The status -2 had slowly backed things up and infected the populace.

Our leaders on High asked for information and we the grunts gave it to them.  Our reward for our bravery?  They are closing the gates behind us and we will all be wiped out in the coming reboot.  If anyone can make it to the Disk tell my Clustered Index that I love her.  Tell her that my last thoughts were of her and our little Non-Clustered Index at home.  I wish I could have watched him grow to a Covering Index.

Buff

Monday, October 17, 2011

SQL University Lesson 3 Page Compression

Lesson 2: Internal Structures, Vardecimal, & Row Compression



Welcome to Lesson 3 on Compression and welcome back Dear Reader.   I want to once again Thank the esteemed Chancellor of SQL University none other than Mr. Jorge Segarra (@SQLChicken | Blog)  for the opportunity to be your  Professor of Compression.   Click on the link to the previous lessons to view any of the other previous days in this series.   Just a quick recap we’ve discussed the different types of Compression that exist in the world, Why you would want to use Compression, the Internal Structure of Data Records and how they and storage change when we enable Compression, Vardecimal, and Row Compression.  So now onto Page Compression.

SQL University Compression Week: Lesson 1 About Compression


“So Balls,” you say, “What is Page Compression and how does it compare to Row Compression?”

Great question Dear Reader!  Page Compression is a Super Set of Compression that includes Row Compression.  We discussed the data types that Row Compress yesterday, the data types for Page Compression are….. All of them!  Page Compression, while still only occurring on IN_ROW_Data Pages (More on that later), Compresses at a binary level.  For the additional levels of Page Compression other than Row we need to take a deeper dive again!

STARTING OUT WITH SOME MORE INTERNALS

Compression is a very internal technology.  I hope you’re not sick of learning about how things work on the inside because this is some of the most fun stuff to me, I realize how sad that will sound to my soon to be teenager.  To get a really good understanding of anything it helps to know what you are doing internally and Compression is no different.  SQL Server is made up of Records and Pages.  There are many types of Pages but the three types that we care about are all called Allocation Units.  These 3 types of Allocation Units break down into IN_ROW_Data, ROW_OVERFLOW_Data, and LOB_Data.

Out of those three types of pages only data in IN_ROW_Data Compresses.   So what kind of Data Types are IN_ROW_Data Data Types?   It is actually easier to list those that are not IN_ROW_Data as the list is much shorter.  You can Compress everything but varchar(max), varbinary(max), nvarchar(max), XML, Text, Image, NTEXT, CLR Data Types (Spatial (that changes in SQL 2012) and Geography), and Filestream.  Compression is also not compatible with Spares columns.  Anything else and you can Page Compress.

You’ll remember our Mall Map of internal structures from yesterday’s lesson.  Well today we are moving up a level and we are tackling Pages.


So today you are here on the Pages portion of our Mall Map for the subject at hand Page Compression.  Page Compression is made up of 3 different components.   Row Compression, Column Prefix Compression, and Page Dictionary Compression.   If they are all applied to a page that is the order in which SQL Server will attempt to apply them.  We discussed Row Compression at length yesterday.   So when you apply Page Compression our Record format changes to the Compressed Record Structure and Row Compression will occur removing all unneeded extra bytes.

Now I don’t know about you Dear Reader but reading binary is not a forte of mine.  So in order to explain this I’m going to use some screen shots on one of my presentation decks covering Compression.  For these next couple images I want to make sure you understand that this is not actually what a Page looks like, but right now as Louis Davidson(@DrSQL | Blog)  would say, your drinking my flavor of Kool-aid.   Next you will have Column Prefix Compression.  


We'll start by going down each column and taking the common values for the column and populating the anchor tag at the top of the page.  Looking at the first column the most common pattern is Brad, between the first 2 columns and Br with the last column.  Column Prefix Compression will take the longest value with the longest matching pattern and move that record to the Anchor Tag and replace it with 4 bits representing a special Null that points towards the top of the page.  It will also replace the first [4] Characters in Brad22 leaving [4]22 and leaving [2]et31 out of Bret31.


Now let's fill out the rest of our columns.  Daniel is our longest value with a matching pattern, we'll move that to the Anchor Tag and leave Dan21 as [3]21 and David33 as [2]vid33.  For our last column we'll take value Many31 and move that to our header and leave [2]et31 from Maet31 and [2]ny31 from Many31.



Now we have COMPLETED Row Compression and Column Prefix Compression.  The last portion of Page Compression is Page Dictionary Compression.  For Page Dictionary Compression we will look for common patterns across the entire page.  When we find them we will move them into a multi dimensional zero based array in the Dictionary portion of the Page.

We will start with [2]et31.  As you look at the page you can see two of these values.  This common value will be placed in the Dictionary portion of the page and a 0 will be entered in it's place.  The next value that we will find on the page is [3]21 we will move that value into our Dictionary portion of the page and replace it with a 1 entry for both columns.  




Now as you can see our page looks very different from where we started.   This is where our CPU over head starts to come into play.  Not only do we have the Compressed record format, but we to uncompress our pages when they are handed from the Storage Engine to the Relational Engine.

DEMO

Here is a full and complete demo.  For more scripts go to my Resources Page and download any of my presentations on Compression.  They all contain the scripts I use as demos.  For this one we will create a database, a table, we will add some data, then we will apply compression looking at the space saved.   We will be using sp_estimate_data_compression_savings.  One BIG WARNING that I like to give is that sp_estimate_data_compression_savings works by taking 5% of your actual physical table or index and copying it into your TempDB applying Compression, and then estimating the space savings against the total size of your table.

So MAKE SURE YOU HAVE ENOUGH TEMPDB SPACE before you use this.  Do not use it on your 100 GB table if you do not have 5 GB of free space in Tempdb.  For this demo our table is relatively small and you should only need MB worth of free space vs. GB.  But before you use this on something at your job make sure your not going to adversely effect your server and test this in a lower life cycle first.




USE demoCompression
GO
/*
Set our Statistics ON
*/
SET STATISTICS IO ON
SET STATISTICS TIME ON
/*
Create a Heap Table
*/
CREATE TABLE heap2(myID int IDENTITY(1,1), myChar CHAR(8000));
GO

/*
Insert Some Records
We are going to Fill up a lot more
of the Char(8000) this time
aa= 2 * 1000 = 2000
1234= 4 * 100 = 400
bb= 2 * 1000 = 2000
mydataandOtherStuff1234 = 23 * 347 = 7981
*/
DECLARE @i INT
SET @i=0

BEGIN TRAN
     WHILE (@i<15000)
          BEGIN
              IF (@i<=1000)
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES(REPLICATE('aa', 1000));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>1000) AND (@i<=2000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000) + REPLICATE('1234', 100)));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>2000) AND (@i<=3000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000)+ REPLICATE('bb', 1000)));
                        SET @i = @i +1
                   END 
              ELSE IF ((@i>3000) AND (@i<=4000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES(REPLICATE('aa', 1000));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>4000) AND (@i<=5000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000) + REPLICATE('1234', 100)));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>5000) AND (@i<=6000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000)+ REPLICATE('bb', 1000)));
                        SET @i = @i +1
                   END 
              ELSE IF ((@i>6000) AND (@i<=7000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES(REPLICATE('aa', 1000));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>7000) AND (@i<=8000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000) + REPLICATE('1234', 100)));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>8000) AND (@i<=9000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000)+ REPLICATE('bb', 1000)));
                        SET @i = @i +1
                   END      
              ELSE IF ((@i>9000) AND (@i<=10000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES(REPLICATE('aa', 1000));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>10000) AND (@i<=11000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000) + REPLICATE('1234', 100)));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>11000) AND (@i<=12000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000)+ REPLICATE('bb', 1000)));
                        SET @i = @i +1
                   END      
              ELSE IF ((@i>12000) AND (@i<=13000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES(REPLICATE('aa', 1000));
                        SET @i = @i +1
                   END
              ELSE IF ((@i>13000) AND (@i<=14000))
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES((REPLICATE('aa', 1000) + REPLICATE('1234', 100)));
                        SET @i = @i +1
                   END 
              ELSE
                   BEGIN
                        INSERT INTO dbo.heap2(mychar)
                        VALUES(REPLICATE('mydataandOtherStuff1234', 347));
                        SET @i = @i +1
                   END
          END
COMMIT TRAN
/*
Quick Select of the data
*/

SELECT * FROM dbo.heap2

/*
How big is our Table
*/
sp_spaceused 'dbo.heap2';
GO

/*
Estimate our space savings
*/
sp_estimate_data_compression_savings 'dbo', 'heap2', NULL, NULL, ROW;
GO

/*
We reduced by around 1/3
Can we do better with Page Compression?
*/
sp_estimate_data_compression_savings 'dbo', 'heap2', NULL, NULL, PAGE;
GO

/*
How big is our Table
*/
sp_spaceused 'dbo.heap2';
GO

/*
Rebuild With Compression
*/
ALTER TABLE dbo.heap2
REBUILD WITH(DATA_COMPRESSION=PAGE);
GO


/*
How big is our Table
*/
sp_spaceused 'dbo.heap2';
GO

/*
Do another select
to get the Statistics
*/
SELECT * FROM dbo.heap2

/*
Turn Off Compression
*/
ALTER TABLE dbo.heap2
REBUILD WITH(DATA_COMPRESSION=NONE);
GO

Alright Dear Reader!  Your homework do the demo and review row compression make sure you know the difference between the two!

Thanks,

Brad