You are on page 1of 6

SQL Server - TempDB performance and strategy

checklist
29 April,2011 by Jack Vamvas

TEMPDB performance is critical to SQL Server performance. There are different uses
for the SQL Server TEMPDB , and it's important to document the planned SQL
activity. I've seen many SQL Server Instances performance increase just by optimizing
the TEMPDB

When thinking and planning TEMPDB configuration consider TempDB purpose and
how to maximise TEMPDB.

Different Uses of TEMPDB


User objects e.g tables, indices, table variables. Scope (user session or routine)

Internal objects .e.g join work files Scope (statement)

Version Stores to support row versioning

SQL Server Objects Using TEMPDB


Cursors

Databse Mail

DBCC CHECKDB

Index Rebuild with SORT_IN_TEMPDB

Queries

Temp tables

UDF

Version Store

Tactics for Optimizing TEMPDB


1) Create an IO profile of TEMPDB . Understand workload requirements.

2) Placement of TEMPDB .Locate the TEMPDB on a separate disk to user databases.


3) Place the TEMPDB on the fastest IO subsystem possible in your
environment.Read SQL Server tempdb and solid state drives - SQL Server DBA

4) Sizing . Configure the TEMPDB size accurately particularly if performance


decrease cant be tolerated .

5) Recovery Model , keep it SIMPLE.

6) Set to Autogrow , see comment above re:Sizing. What rate of growth? TEMPDB
grows raplidly , setting small growth rate may cause problems. Normally I set the auto-
grow rate to 10% of the initial size. That does not mean the growth is set at 10% , it
means that that if the initial size is 500 MB , the growth is set at 50 MB

7) Multiple data files for TEMPDB 1 data file per CPU. In a tightly consolidated
environment , 1 tempDB file per CPU may be a luxury.

8) Define the storage IO capability to the CPU processing capacity

9) Split TEMPDB into multiple files. Place the TEMPDB files on different drives.
Depending on the disk alignment architecture each drive is assigned to a separate
LUN . If using SVC there are different considerations. Speak to your SAN
administrator about the storage architecture .

10) Configure the split TEMPDB files as equal sizes.

SQL Server TEMPDB error - Could not allocate space


for object dbo.SORT temporary run storage
02 September,2012 by Jack Vamvas

A Developer sent me this error message today , and asked for some suggestions to fix
the TEMPDB error:

view source
print?
1.Could not allocate space for object 'dbo.SORT temporary run storage:
140744401616896' in database 'tempdb' because the 'PRIMARY' filegroup is
full.
The error message refers to the TEMPDB space usage. The TEMPDB data and log file
are out of space. I checked the TEMPDB it was onautogrow , but the drives were full.
We considered a few options.

1) Check for other files on the drive storing TEMPDB . Even though the TEMPDB is
stored on its own drive, its possible someone has dumped files on this drive

2) Speak to the Storage guys and request to expand the drive. This would take some
time as it is a Production system which requires a change management process.

3) Check the SQL statement causing this situation. It was an INSERT statement ,
attempting to INSERT 110 million rows from one table to another. Consider breaking
the sql statement down into smaller chunks. SQL Server List
current tempdb statements - SQL Server DBA

4) Add an additional file to the TEMPDB on another drive. This is a last resort and I
prefer to spend time understanding why the TEMPDB file is growing. This is
a temporary solution .

view source
print?
1.--this is example code customise for your purpose
2.alter database tempdb add file
3.(name = 'tempdev2',
4.filename = 'G:\Program Files\Microsoft SQL
Server\MSSQL.1\MSSQL\DATA\tempdb2.mdf',
5.size = 1024)

Implementing SSD on the SAN has advantages such as hardware redundancy but
also limitations i.e the data must first navigate CPU to drives with the various
limitations.

An alternative is to plug the SSD directly into the PCIe bus. The benefit is
that IO will bypass the drives and RAID controllers . This type of SSD has its own
controller , with the bandwidth dedicated to the drive avoiding SAN contention and
freeing up bandwidth for SQL data and logs.

This should amount to a greater and faster number of IO operations per second .OCZ
and Fusion-IO are two examples of manufacturers
This is not practical for all types of SQL Server files for example , would you want
the Primary filegroup on a drive that is not part of the hardware redundancy
schema. ? There are risks involved in not choosing the right files to use on a SSD
drive.

But there are files , such as the TEMPDB where there is good performance
improvement potential. To assist in deciding Monitor SQL Server TempDB usage -
SQL Server DBA

Firstly , the TEMPDB is rebuilt every time the SQL Server restarts no permanent
data is maintained . The type of activity on TEMPDB is of a temporary nature such
as : temp tables, index rebuild, table variables, xml variables. TEMPDB is also a
spillover space for sorting data, group by, snapshot isolation and large joins

This type of activity works well with SSD on the PCIe bus . If the server crashed and
was not recoverable TEMPDB could be rebuilt with minimal risk .TEMPDB does not
have to be represented onto another server as there is no permanent data .

If redundancy is required for this type of drive then a software RAID solution would
need to be considered.

This solution would not work for clustering as the clustering requirement is that all
drives are remote to the server. You could potentially develop a workaround but
based on the SQL Server 2005 \ 2008 documentation - maintaining a local drive is not
supportable.

This does not apply to LAN based SSD which are considered to be remote

Monitor SQL Server TempDB usage


07 March,2013 by Jack Vamvas

Large queries spilling into tempdb such as SQL Server SORT IN TEMPDB and Sort
Warnings and large temporary tables can use Tempdb space very quickly and lead to
outages. Use the DMVs : sys.dm_db_file_space_usage,
sys.dm_db_session_space_usage, sys.dm_db_task_space_usage

How to Troubleshoot TempDB

First Step - Comprehensive Tempdb Sizing exercise. There are many different
methods , such as : a) create a workload b) running large maintenance jobs e.g index
rebuild
Second Step - monitoring Tempdb usage. As well as a SQL Server monitoring
alerts solution based on events , such as disk full, there are some useful DMVs to drill
into TempDB usage based on tasks and sessions.

sys.dm_db_file_space_usage - Lists space usage information for each file in the


Tempdb database. Reports on unallocated extent page count.

sys.dm_db_session_space_usage Broken down by each session. Lists the number


of pages allocated and deallocated

sys.dm_db_task_space_usage Broken down by each task. Lists page allocation


and deallocation activity

view source
print?
1.go
2.select * from sys.dm_db_file_space_usage
3.go
4.select * from sys.dm_db_session_space_usage
5.go
6.select * from sys.dm_db_task_space_usage

To see the type of objects filling the TempDB use this SQL Script - How to find type
of objects filling TempDB

SQL Server - Shrink tempdb without sql server restart


13 January,2011 by Jack Vamvas

Despite best efforts to size a tempdb ,sometimes the tempdb expands to a size as big
as the drive. Because the drive is full the tempdb can no longer expand. This triggers a
situation in a monitoring framework. A quick solution is required, to shrink down the
tempdb . Following are 2 different methods I use , that dont require a SQL Server
restart.

It is important to focus on the reason for continious TEMDB growth , as the problem will
just reappear

If I receive consistency errors , than I resort to a SQL Server restart , depending on the
situation
DBCC DHRINKFILE will allow you to decrease to a size smaller than the original size

Method 1: DBCC SHRINKFILE

view source
print?
01.Use tempdb
02.
03.GO
04.
05.--shrink the main data file
06.
07.DBCC shrinkfile (tempdev,)
08.
09.GO
10.
11.--shrink the main log file
12.
13.DBCC shrinkfile (templog, )
14.
15.
16.
17.---e.g DBCC shrinkfile (tempdev, 5000)

Method 2:DBCC SHRINKDATABASE

view source
print?
1.DBCC SHRINKDATABASE(tempdb,)
2.
3.--e.g DBCC SHRINKDATABASE(tempdb,20)