solving sql server problems for millions of dbas and developers since 2006



SQL Server DBA Tips SQL Server Developer Tips SQL Server Business Intelligence Tips SQL Server Career Tips SQL Server Tip Categories SQL Server Tutorials SQL Server Webcasts SQL Server Whitepapers SQL Server Tools SQL Server Questions and Answers MSSQLTips Authors About MSSQLTips SQL Server User Groups MSSLQTips Giveaways MSSQLTips Advertising Options

MSSQLTips Facebook Page MSSQLTips LinkedIn Page MSSQLTips RSS Feed MSSQLTips Twitter Page MSSQLTips Google+ Page

SQL Product Highlight

Devart - dbForge SQL Complete

dbForge SQL Complete is a code autocomplete tool for SQL Server Management Studio and Visual Studio. Free and advanced paid editions of this useful add-in offer powerful autocompletion and formatting of T-SQL code that replaces native Microsoft T-SQL Intellisense.

Learn more!








SQL Server tempdb one or multiple data files

By: | Read Comments (2) | Print

Matteo is a DBA and has been working in IT since 1993. He specializes in SQL Server and also has knowledge of MySQL.

Related Tips: More

Problem

Tempdb plays an important role on SQL Server performance. A tempdb database that resides on a slow set of disks or a tempdb database that has been sized incorrectly may have an overall impact on query performance. In this tip I will go over some best practices for performance related to Tempdb.

Solution

Best practice recommends placing tempdb on a fast I/O subsystem and to use disk striping to numerous direct attached disks. Best practice, also recommends creating many files to maximize disk bandwidth and to reduce contention in allocation structures. As a general guideline, best practice, suggests creating one data file per CPU. Each file should be set to the same size because this allows the proportional fill algorithm to distribute the allocation load uniformly with minimal contention.

Contention in tempdb is caused with PFS, GAM, and SGAM page allocation when a lot of very small temp tables are created. It is not the goal of this tip to provide a detailed explanation of tempdb contention. The  goal of this tip is to illustrate, with an example, that sometimes the blind application of best practices can be counterproductive to performance.

The SQL code that I will use in this example comes from my previous tip (SQL Server 2008 64bit Query Optimization Trick  http://www.mssqltips.com/sqlservertip/1955/sql-server-2008-64bit-query-optimization-trick/).


Test 1: Using SQL Server with 1 tempdb

 Using SQL Server with 1 tempdb

I ran the following query 3 times (keep in mind that this query spills the sort operation onto tempdb)

--- T-SQL script provided by: www.sqlworkshops.com  
DECLARE @c1 INT@c2 INT@c3 CHAR(2000)
SELECT @c1=c1@c2=c2@c3=c3 FROM tab7 
WHERE c1 100000
ORDER BY c2

--Results:
--CPU time = 1100 ms,  elapsed time = 11670 ms.
--CPU time = 2190 ms,  elapsed time = 12200 ms.
--CPU time = 1720 ms,  elapsed time = 12630 ms.

Average: (11670+12200+12630)/3= 12166


Test 2: Using SQL Server with 2 tempdb files with the same size files

 Using SQL Server with 2 tempdb files with the same size files

I ran the following query 3 times

--- T-SQL script provided by: www.sqlworkshops.com
DECLARE @c1 INT@c2 INT@c3 CHAR(2000)
SELECT @c1=c1@c2=c2@c3=c3 FROM tab7 
WHERE c1 100000
ORDER BY c2

--Results:
--CPU time = 1500 ms,  elapsed time = 13740 ms.
--CPU time = 1710 ms,  elapsed time = 14940 ms.
--CPU time = 1610 ms,  elapsed time = 14340 ms.

Average: (13740+14940+14340)/3= 14340


As we can see, on average, when the query runs on 2 tempdb files it is 2174 ms slower then when it runs on a single tempdb file. The reason why is that when sort operations spill over to tempdb they are pretty sequential. If we have two tempdb files, we are making 2 streams of sequential I/O which is not really sequential anymore unless our SAN is configured to handle 2 or more streams of sequential I/O concurrently.

The above picture depicts the two streams of I/O over the two tempdb files

The above picture depicts the two streams of I/O over the two tempdb files. Process Monitor shows that SQL alternates its writing in sequential order over tempdb.mdf and tempdev1.ndf.  

Conclusion

If our subsystem I/O is capable of handling concurrent streams of sequential I/O then the best practice of a tempdb file per core will help our query performance otherwise; it will be counterproductive to performance.

Next Steps



Related Tips: More | Become a paid author


Last Update: 4/8/2010

Share: Share 






Comments and Feedback:

Friday, April 16, 2010 - 9:28:43 AM - DavidB Read The Tip

Thanks for confirming what I found as well. I ran performance tests on a database server comparing the tempdb with one and and tempd with one file per CPU. The test using multiple tempdb files took longer. Now I know it was not something specific to our hardware or something I did wrong.

 Thank You 

 


Sunday, September 12, 2010 - 11:54:14 PM - Patrick Flynn Read The Tip
For a detailed discussion of this issue and considerations around TempDB have a look at Paul Randal's blog post:

http://www.sqlskills.com/BLOGS/PAUL/post/A-SQL-Server-DBA-myth-a-day-(1230)-tempdb-should-always-have-one-data-file-per-processor-core.aspx



Post a Comment or Question

Keep it clean and stay on the subject or we may delete your comment.
Your email address is not published. Required fields are marked with an asterisk (*)

*Name   *Email   Notify for updates
Comments
*Enter Code refresh code


 
Sponsor Information
"Amazing, Amazing, Amazing! SQL doctor is truly one of the most powerful tools I have seen."

The 10 tools in the SQL Developer Bundle cut the time spent in dull and tedious tasks. Learn more.

What grade do you think your SQL Servers get? Find out with Edgewood's SQL Server Health Check consulting services.

Get SQL Server Tips Straight from Kevin Kline.

Solving SQL Server problems for millions of DBAs and Devs since 2006. Join now.

The SQL Server Security THREAT - It’s Closer Than You Think


Copyright (c) 2006-2012 Edgewood Solutions, LLC All rights reserved
privacy | disclaimer | copyright | advertise | about
authors | contribute | feedback | giveaways | user groups
Some names and products listed are the registered trademarks of their respective owners.


Edgewood Solutions LLC | MSSharePointTips.com | MSSQLTips.com