This post is re-published from my original post on SQL Solutions Group. I hope that you enjoy it.
In my last article, I started off talking about checking various settings that make a performance difference prior to jumping into query tuning. This article continues the investigation into performance affecting settings, by jumping straight into the storage subsystem. How would you like to make a single change to your drives and have the IO operations improve by up to 40%?
Just reformat the drives that contain your database data and log files.
If you are running on a version of Windows prior to Windows Server 2008, or if the drives were migrated to a new system without reformatting them, then you really could improve IO performance simply by reformatting the drives. Older versions of Windows will create disk partitions that default to having the boundaries based upon the ancient Cylinder/Head/Sector addressing scheme that was common in disk controllers of that era. Once these boundaries are set, the only way to change them is to reformat the drive, so migrating this drive to a Windows Server 2008 or newer server alone does not fix the issue.
How reformatting the drive affects IO performance
File Allocation Size
On the hard drive, the minimum amount of data that can be read or written is called a sector, which historically was a mere 512 bytes (newer drives may have sector sizes up to 4KB). A group of sectors is a cluster (no not that type of cluster). A drive with a 64KB cluster size (at 512 bytes per cluster) will have 128 sectors, which is also known as the file allocation size. The first 63 sectors of a drive are reserved, or hidden, and include the master boot record (MBR). Simply multiplying the 63 sectors by the sector size of 512 bytes shows that this hidden area occupies 32,256 bytes, or 31.5KB. The drive dutifully starts storing data immediately after this.
SQL Server IO operations
Now, consider how SQL Server performs disk IO operations – an extent at a time (an extent is 8 pages, each of which are 8KB, for a total of 64KB). To store this information on disk, with the default settings, this 64KB will start immediately after the 31.5KB and continue for 64KB… and it will span two clusters. When the drive goes to read this data, it will have to read the first cluster, and then the second cluster, in order to get all of the data needed for the extent being read from disk. When the next extent is read, it will re-read the second cluster, and read the third cluster. The wasted IO operation should be obvious.
How to have SQL Server work better with disk
What we need to do is to offset the beginning of the data being stored on disk to a location more conducive to how the program is operating. This offset is known as the “Partition Alignment Offset”. To be in tune with SQL Server, this value should be an increment of 64KB. However, you also need to consider the entire storage subsystem – the disks, controllers and memory. Starting with Windows Server 2008, this offset is at 1024KB – a nice increment of 64KB that also works very nicely with most RAID disks/controllers. Prior to Windows Server 2008, partition alignment offset was not explicitly performed, so this will need to be performed.
Determining a drive’s partition alignment offset
To determine what the partition alignment offset is for a windows basic disk, there is a very simple wmic command to run:
1 |
wmic partition get BlockSize, StartingOffset, Name, Index |
The Index value is the same as the disk number reported in the Disk Management tool, and also from the wmic command wmic Volume. However, we tend to think in terms of drive letters, not drive indexes, so the following PowerShell script will return Partition Alignment Offset for all drives on the system that it is run on and perform the mapping from index to drive letter:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# # ------------------------------------------------------------------------------- # Partition Information # ------------------------------------------------------------------------------- # MODIFICATION LOG # ------------------------------------------------------------------------------- # 2012-08-09 WGS Initial Creation. # 2012-08-11 WGS Modified to work with PowerShell v1. # 2013-02-05 WGS Modified to get Drive Letter. # 2014-01-27 WGS Modified to return volume blocksize. # ------------------------------------------------------------------------------- # <# .PARAMETER OpenInNotepad Value passed as $True or $False. Defaults to $False. #> param ( [boolean] $OpenInNotepad = $false ) cls FUNCTION Get-DriveLetter($PartPath) { #Get the logical disk mapping $LogicalDisks = Get-WMIObject Win32_LogicalDiskToPartition | ` Where-Object {$_.Antecedent -eq $PartPath} $LogicalDrive = Get-WMIObject Win32_LogicalDisk | ` Where-Object {$_.__PATH -eq $LogicalDisks.Dependent} $LogicalDrive.DeviceID } FUNCTION Get-VolumeBlockSize($PartPath) { $Drive = Get-DriveLetter($PartPath) IF ($Drive -gt "") { #Get the BlockSize of the volume $Volume = Get-WMIObject Win32_Volume | ` Where-Object {$_.DriveLetter -eq $Drive} $Volume.BlockSize } } FUNCTION Get-PartitionAlignment { Get-WMIObject Win32_DiskPartition | ` Sort-Object DiskIndex, Index | ` Select-Object -Property ` @{Expression = {$_.DiskIndex};Label="Disk"},` @{Expression = {$_.Index};Label="Partition"},` @{Expression = {Get-DriveLetter($_.__PATH)};Label="Drive"},` @{Expression = {$_.BootPartition};Label="BootPartition"},` @{Expression = {"{0:N3}" -f ($_.Size/1Gb)};Label="Size_GB"},` @{Expression = {"{0:N0}" -f ($_.BlockSize)};Label="Partition_BlockSize"},` @{Expression = {Get-VolumeBlockSize($_.__PATH)};Label="Volume_BlockSize"}, @{Expression = {"{0:N0}" -f ($_.StartingOffset/1Kb)};Label="Offset_KB"},` @{Expression = {"{0:N0}" -f ($_.StartingOffset/$_.BlockSize)}; Label="OffsetSectors"},` @{Expression = {IF (($_.StartingOffset % 64KB) -EQ 0) {" Yes"} ELSE {" No"}};Label="64KB"} } # Hash table to set the alignment of the properties in the format-table $b = ` @{Expression = {$_.Disk};Label="Disk"},` @{Expression = {$_.Partition};Label="Partition"},` @{Expression = {$_.Drive};Label="Drive"},` @{Expression = {$_.BootPartition};Label="BootPartition"},` @{Expression = {"{0:N3}" -f ($_.Size_GB)};Label="Size_GB";align="right"},` @{Expression = {"{0:N0}" -f ($_.Partition_BlockSize)};Label="PartitionBlockSize";align="right"},` @{Expression = {"{0:N0}" -f ($_.Volume_BlockSize)};Label="VolumeBlockSize";align="right"},` @{Expression = {"{0:N0}" -f ($_.Offset_KB)};Label="Offset_KB";align="right"},` @{Expression = {"{0:N0}" -f ($_.OffsetSectors)};Label="OffsetSectors";align="right"},` @{Expression = {$_.{64KB}};Label="64KB"} $a = Get-PartitionAlignment # Display formatted data on the screen $a | Sort-Object Drive, Disk, Partition | Format-Table $b -AutoSize IF ($OpenInNotepad -eq $true) { # Export to a pipe-delimited file $a | Sort-Object Drive, Disk, Partition | Export-CSV $ENV:temp\PartInfo.txt -Delimiter "|" -NoTypeInformation # Open the file in NotePad Notepad $ENV:temp\PartInfo.txt } |
If you want to open the results in Notepad, pass in the parameter $True.
You may note that I said that using these wmic commands is for a Windows basic disk. If you are using a dynamic disk, you need to use alternative methods. With Windows Server 2008, you would use the tool diskdiag.exe (in earlier versions of Windows, this was called dmdiag). For either, you need to use the –V flag: dmdiag.exe –V or diskdiag.exe –V.
Changing the Partition Alignment Offset
To change the Partition Alignment Offset on Windows Server 2008 or higher, simply format the drive. On earlier systems, you will need to use the diskpart.exe tool to change the partition alignment offset, and then format the drive. In either case, this is a destructive operation – you will lose all data on the drive.
For more information on disk partition alignment, refer to this TechNet article: //technet.microsoft.com/en-us/library/dd758814(v=sql.100).aspx.
These steps should yield a noticeable IO performance improvement. Test it for yourself and let me know what you experience.