Wednesday, July 1, 2015

TempDB - Round Robin and Proportional Fill

Proportional Fill

My post from last week sparked quite a bit of discussion at work about how the data files for TempDB fill up when they are not of equal size. Pretty much all of the documentation and reccomendations out there say to keep the data files the same size so that the round-robin data flow works properly. What we were having trouble deciding is what happens when they are not the same size, for example if you have one file that is 5GB and three files that were 500 MB.

The discussions ended up in two different camps.

  1. TempDB would allocate pages in the first larger file until the free space was equal to the three smaller files then the round robin would continue.
  2. The allocations would still be handled in a round robin fashion with the larger file just getting more so that they would fill up evenly (from a percentage standpoint). 
Like any good discussion we decided to take this to our test boxes and see if we could figure it out The answer I found comes down to a little guy named proportional fill.

The Test

I took my test server and created four TempDB data files. Look at the image below to see how they looked when I started out. Notice they all have a very similar free space percentage
(The script I used to view this info came from Dave Turpin)


Then I wrote a simple script that would create a temp table and add data to it
DECLARE @x int
SET @x = 1

CREATE TABLE #MyTempTable
(  id BIGINT, SomeValue char(5000))

WHILE @x < 362846
BEGIN;
INSERT INTO #MyTempTable ( id, SomeValue )
VALUES  ( 99999999999, 'ddddddddddddddddd')
SET @x = @x +1
END; 

I picked the number 362846 because I knew that would get the files near 50% full and I wanted to add something to the test half way through. After this ran I checked TempDB again using my script and got the below results

As you can see it looks like the second camp was correct. Proportional fill was used to allocate pages to each data file according to its free space percentage and not it's size. The result is that there were a lot more allocations to the larger file but at the end they all ended up around the same free space percentage

Add to the Test

Now I wanted to see what would happen if you added a new TempDB data file, so I added another 500 MB file to TempDB. My thought was that if I now had four files at 55% free and one file at almost 100% free would it fill up the new file until it was at 55% or continue to allocate pages in all the files
After the file was added I ran my test again and checked the file usage twice
Once about halfway

And when the script finished

As you can see, the allocations were made to all of the files, not just the new one. It is just that more allocations were made to the new 500 MB file than to the old 500MB files during this second test (361MB as opposed to about 192 MB). Proportional Fill strikes again! If you look at how the numbers are trending you can see that if I hadn't set a limit on my while loop all of the files would have been almost equally full when the first one completely filled up.

Sum Up

This was a neat little experiment for me. I enjoy being able to test things out rather than just taking as fact what I read some where. In fact, quite a bit of the documentation I found was misleading on this subject. If you would like a good (and more thorough) look at our friend Fill, check out this post by Jonathan Kehayias:

He uses extended events to show this so you can actually see the number of file writes per data file. His test is not specifically on TempDB but the allocation should work the same way.
Also check out this article by Paul Randall for more info about proportional fill:

I'll be back in two weeks. Hopefully I can squeeze one more blog post out of all my TempDB testing.

Hasta Luego,
RutherfordTX



1 comment:

  1. Play casino - No.1 for the Casino Guru
    No longer have the opportunity to go to the casinos or read the reviews of the https://septcasino.com/review/merit-casino/ slots you love. But they're not always the same. sol.edu.kg Sometimes you 출장마사지 have a https://jancasino.com/review/merit-casino/ new 나비효과 online

    ReplyDelete