nothing is not more permanent than the temporary

47
Wonder TempDB Dubi Lebel Dad, chef, gardener and DBA [email protected]

Upload: sqlservercoil

Post on 27-Jan-2015

110 views

Category:

Technology


2 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Nothing Is Not More Permanent Than The Temporary

Wonder TempDB

Dubi Lebel

Dad, chef, gardener and

DBA

[email protected]

Page 2: Nothing Is Not More Permanent Than The Temporary

2

Who am I?

Dubi- Or Not To Be

To do is to be - Descartes

To be is to do - Voltaire

Do be do be do - Frank Sinatra

Yaba Daba Do - Fred Flintstone

11/2/2009 Dubi Or Not To Be

Page 3: Nothing Is Not More Permanent Than The Temporary

3

Temporary Agenda

It all about TemDB

For what?

Where?

How to?

best practice!

11/2/2009 Dubi Or Not To Be

Page 4: Nothing Is Not More Permanent Than The Temporary

4

TempDB

Only one TempDB per instance.

Operations within TempDB are minimally logged.

data in TempDB does not persist after SQL Server shuts

down.

Only one file group in TempDB is allowed for data and

one file group for logs.

By default auto grow is enabled.

When the server restarts, the TempDB file size is reset

to the configured value (the default is 8 MB).

11/2/2009 Dubi Or Not To Be

Page 5: Nothing Is Not More Permanent Than The Temporary

5

What is TempDB

BOL:

The TempDB system database is a global resource that is

available to all users connected to the instance of SQL

Server and is used to hold the following:

11/2/2009 Dubi Or Not To Be

Page 6: Nothing Is Not More Permanent Than The Temporary

6

BOL: TempDB hold the following :

Temporary user objects that are explicitly

created, such as: global or local

temporary tables,

temporary stored procedures,

table variables,

Cursors (Key-set-driven and static cursors ).

11/2/2009 Dubi Or Not To Be

Page 7: Nothing Is Not More Permanent Than The Temporary

7

BOL: TempDB hold the following :

Internal objects that are created by the SQL Server

Database Engine, for example:

work tables to store intermediate results for:

spools

sorting.

Hash match.

Index (create with SORT_IN_TempDB )

11/2/2009 Dubi Or Not To Be

Page 8: Nothing Is Not More Permanent Than The Temporary

8

BOL: TempDB hold the following :

Row versions that are generated by data

modification transactions in a database that uses read-

committed

using row versioning isolation

snapshot isolation transactions.

11/2/2009 Dubi Or Not To Be

Page 9: Nothing Is Not More Permanent Than The Temporary

9

BOL: TempDB hold the following :

Row versions that are generated by data

modification transactions for features, such as:

online index operations,

Multiple Active Result Sets (MARS),

AFTER triggers.

11/2/2009 Dubi Or Not To Be

Page 10: Nothing Is Not More Permanent Than The Temporary

10

Is that all?

11/2/2009 Dubi Or Not To Be

Page 11: Nothing Is Not More Permanent Than The Temporary

11

TempDB also hold the following:

DBCC CHECK

Service Broker

Database Mail (Service Broker)

Event notifications (Service Broker)

Query notifications (Service Broker)

XML

Common table expression queries (with cte(col,col) as select..)

Large object (LOB) data type variables and parameters

11/2/2009 Dubi Or Not To Be

Page 12: Nothing Is Not More Permanent Than The Temporary

12

event log:

Source: MSSQLSERVER

Event ID: 17052

Description: The log file for database 'TempDB' is full.

Back up the transaction log for the database to free up

some log space

11/2/2009 Dubi Or Not To Be

Page 13: Nothing Is Not More Permanent Than The Temporary

13

error message

Server: Msg 8624, Level 16, State 1

Internal SQL Server error

or

Server: Msg 1101, Level 17, State 10, Line 1

Could not allocate new page for database 'TempDB'.

There are no more pages available in filegroup

DEFAULT. Space can be created by dropping objects,

adding additional files, or allowing file growth.

11/2/2009 Dubi Or Not To Be

Page 14: Nothing Is Not More Permanent Than The Temporary

14

How to plan TempDB Capacity ?

Check that auto-grow is on or off for TempDB

Execute individual queries , workload trace files , index

maintenance and monitor TempDB space.

Sum the space-use values from the previous steps to

predict your total workload usage.

adjust this value. And adjust again.

11/2/2009 Dubi Or Not To Be

Page 15: Nothing Is Not More Permanent Than The Temporary

15

How to Monitor TempDB Use?

sys.dm_db_file_space_usage - disk space that is used

sys.dm_db_session_space_usage and

sys.dm_db_task_space_usage - page allocation or

deallocation activity

11/2/2009 Dubi Or Not To Be

Page 16: Nothing Is Not More Permanent Than The Temporary

16

Monitoring TempDB Disk Space 1

SELECT

SUM(unallocated_extent_page_count) AS [free pages],

(SUM(unallocated_extent_page_count)*1.0/128) AS [free space in MB]

FROM sys.dm_db_file_space_usage;

returns the total number of free pages and total free space in megabytes (MB) available in all files in TempDB

11/2/2009 Dubi Or Not To Be

Page 17: Nothing Is Not More Permanent Than The Temporary

1711/2/2009 Dubi Or Not To Be

Page 18: Nothing Is Not More Permanent Than The Temporary

18

Monitoring TempDB Disk Space 2

SELECT SUM(version_store_reserved_page_count)

AS [version store pages used],

(SUM(version_store_reserved_page_count)*1.0/128)

AS [version store space in MB]

FROM sys.dm_db_file_space_usage;

returns the total number of pages used by the version

store and the total space in MB used by the version store

in TempDB

11/2/2009 Dubi Or Not To Be

Page 19: Nothing Is Not More Permanent Than The Temporary

1911/2/2009 Dubi Or Not To Be

Page 20: Nothing Is Not More Permanent Than The Temporary

20

Monitoring TempDB Disk Space 3

SELECT transaction_id

FROM

sys.dm_tran_active_snapshot_database_transactions

ORDER BY elapsed_time_seconds DESC;

If the version store is using a lot of space in TempDB, you

must determine what is the longest running transaction.

11/2/2009 Dubi Or Not To Be

Page 21: Nothing Is Not More Permanent Than The Temporary

2111/2/2009 Dubi Or Not To Be

Page 22: Nothing Is Not More Permanent Than The Temporary

22

Monitoring TempDB Disk Space 4

SELECT SUM(internal_object_reserved_page_count)

AS [internal object pages used],

(SUM(internal_object_reserved_page_count)*1.0/128)

AS [internal object space in MB]

FROM sys.dm_db_file_space_usage;

returns the total number of pages used by internal objects

and the total space in MB used by internal objects in

TempDB

11/2/2009 Dubi Or Not To Be

Page 23: Nothing Is Not More Permanent Than The Temporary

2311/2/2009 Dubi Or Not To Be

Page 24: Nothing Is Not More Permanent Than The Temporary

24

Monitoring TempDB Disk Space 5

SELECT SUM(user_object_reserved_page_count)

AS [user object pages used],

(SUM(user_object_reserved_page_count)*1.0/128)

AS [user object space in MB]

FROM sys.dm_db_file_space_usage;

returns the total number of pages used by user objects

and the total space used by user objects in TempDB.

11/2/2009 Dubi Or Not To Be

Page 25: Nothing Is Not More Permanent Than The Temporary

2511/2/2009 Dubi Or Not To Be

Page 26: Nothing Is Not More Permanent Than The Temporary

26

Monitoring TempDB Disk Space 6

SELECT SUM(size)*1.0/128 AS [size in MB]

FROM TempDB.sys.database_files

returns the total amount of disk space used by all files

in TempDB.

11/2/2009 Dubi Or Not To Be

Page 27: Nothing Is Not More Permanent Than The Temporary

2711/2/2009 Dubi Or Not To Be

Page 28: Nothing Is Not More Permanent Than The Temporary

28

SELECT

SUM (user_object_reserved_page_count)*8 as usr_obj_kb,

SUM (internal_object_reserved_page_count)*8 as internal_obj_kb,

SUM (unallocated_extent_page_count)*8 as freespace_kb,

SUM (mixed_extent_page_count)*8 as mixedextent_kb

FROM sys.dm_db_file_space_usag

11/2/2009 Dubi Or Not To Be

Page 29: Nothing Is Not More Permanent Than The Temporary

2911/2/2009 Dubi Or Not To Be

Page 30: Nothing Is Not More Permanent Than The Temporary

30

which Transact-SQL statements are

the top consumers of tempdb space

SELECT t1.session_id, t1.request_id, t1.task_alloc, t1.task_dealloc, t2.sql_handle, t2.statement_start_offset, t2.statement_end_offset, t2.plan_handle FROM (Select session_id, request_id, SUM(internal_objects_alloc_page_count) AS task_alloc, SUM (internal_objects_dealloc_page_count) AS task_dealloc

FROM sys.dm_db_task_space_usageGROUP BY session_id, request_id) AS

t1, sys.dm_exec_requests AS t2 WHERE t1.session_id = t2.session_id AND (t1.request_id = t2.request_id)

ORDER BY t1.task_alloc DESC

11/2/2009 Dubi Or Not To Be

Page 31: Nothing Is Not More Permanent Than The Temporary

31

Perfmon counters

Access Methods::Worktables Created/sec

temporary objects and are used to store results for query

spool, LOB variables, and cursors.

Typically this number is < 200

Access Methods::Workfiles Created/sec

temporary results for hash joins and hash aggregates

Access Methods: Worktables From Cache Ratio

11/2/2009 Dubi Or Not To Be

Page 32: Nothing Is Not More Permanent Than The Temporary

32

Perfmon counters

Temp Tables Creation Rate (SQL Server 2005)

The number of temporary table or variables created/sec.

Temp Tables For Destruction (SQL Server 2005)

The number of temporary tables or variables waiting to be

destroyed by cleanup system thread.

11/2/2009 Dubi Or Not To Be

Page 33: Nothing Is Not More Permanent Than The Temporary

33

How to add space to TempDB?

ALTER DATABASE, the data file will return to the new

size every time you restart the instance of SQL Server.

Set the recovery model of TempDB to SIMPLE.

Turn on auto-grow, but be careful, % can be dangers!

based it on the speed of the I/O subsystem

BUT it is a good idea to turn off the auto-grow feature

for the TempDB data files

11/2/2009 Dubi Or Not To Be

Page 34: Nothing Is Not More Permanent Than The Temporary

34

Hardware

Generally, RAID 1+0 provides better write performance

than any other RAID level providing data protection,

including RAID 5.

Striping recommended

Separate from user databases

SQL Server 2008

CHECKSUM for new installs (Earlier versions – NONE, Not

changeable)

On upgrade NONE still default

Recommend setting CHECKSUM after upgrade

11/2/2009 Dubi Or Not To Be

Page 35: Nothing Is Not More Permanent Than The Temporary

35

File Placement

On a high-performance IO subsystem

Preferably on a separate spindle(s)

IO subsystem requirements not as strict

Durability not required (KB917047)

RAM Disk or IO subsystem with cache

multiple files on the same physical spindle or multiple

devices?

Page 36: Nothing Is Not More Permanent Than The Temporary

36

Number of Files

create as many files as needed to maximize disk

bandwidth.

One of the best practice is to create one data file for

each (virtual) CPU on the server

KB 328551 -

Trace flag 1118 switches allocations in TempDB from

single-page at a time for the first 8 pages

create the files with equal sizing

11/2/2009 Dubi Or Not To Be

Page 37: Nothing Is Not More Permanent Than The Temporary

37

equal sizing

The equal sizing of data files is critical because the

proportional fill algorithm is based on the size of the

files. If data files are created with unequal sizes, the

proportional fill algorithm tries to use the largest file

more for GAM allocations instead of spreading the

allocations between all the files, thereby defeating the

purpose of creating multiple data files.

11/2/2009 Dubi Or Not To Be

Page 38: Nothing Is Not More Permanent Than The Temporary

38

Moving TempDB from system disk

Alter database TempDB modify file

(name = tempdev,

filename = „DEV:\MyPath\TempDB.mdf')

Alter database TempDB modify file

(name = templog,

filename = „DEV:\MyPath\templog.ldf')

Restart server

11/2/2009 Dubi Or Not To Be

Page 39: Nothing Is Not More Permanent Than The Temporary

39

What about the log file?

Most operations in tempdb are not logged.

Operations on user objects are logged.

Sort operations in tempdb also require logging.

Estimating the log space:

similar to estimating the log file size for other databases.

Multi log files?

11/2/2009 Dubi Or Not To Be

Page 40: Nothing Is Not More Permanent Than The Temporary

40

Moving TempDB from system disk

Be careful when you move the TempDB database

because if the TempDB database cannot be created,

SQL Server will not start. If the TempDB database

cannot be created, start SQL Server by using the (-f)

startup parameter and move the TempDB database to a

valid location.

11/2/2009 Dubi Or Not To Be

Page 41: Nothing Is Not More Permanent Than The Temporary

41

What about Shrinking files?

Auto shrink is not allowed for tempdb.

How to shrink the TempDB database in SQL Server

http://support.microsoft.com/kb/307487

It is not recommended!

11/2/2009 Dubi Or Not To Be

Page 42: Nothing Is Not More Permanent Than The Temporary

42

What about auto update stats?

disabling the auto update stats

Usually objects created in the TempDB are fairly small

and, as such, the statistics will not reach the threshold

that causes the statistics to update automatically.

However, you'll need to manually update statistics if you

occasionally use large temporary objects.

What about “auto create statistics”

11/2/2009 Dubi Or Not To Be

Page 43: Nothing Is Not More Permanent Than The Temporary

43

Indexing temporary table

Indexing temporary large temp tables greatly increases

TempDB performance

use the normal CREATE INDEX command after the table

has been created.

table variables cannot have indexes created on them.

But can have a primary key defined upon creation

using the DECLARE @variable TABLE command.

11/2/2009 Dubi Or Not To Be

Page 44: Nothing Is Not More Permanent Than The Temporary

44

Restrictions Adding filegroups.

Backing up or restoring the database.

Changing collation. The default collation is the server collation.

Changing the database owner. TempDB is owned by dbo.

Creating a database snapshot.

Dropping the database.

Dropping the guest user from the database.

Enabling change data capture.

11/2/2009 Dubi Or Not To Be

Page 45: Nothing Is Not More Permanent Than The Temporary

45

Restrictions

Participating in database mirroring.

Removing the primary filegroup, primary data file, or log file.

Renaming the database or primary filegroup.

Running DBCC CHECKALLOC.

Running DBCC CHECKCATALOG.

Setting the database to OFFLINE.

Setting the database or primary filegroup to READ_ONLY.

11/2/2009 Dubi Or Not To Be

Page 46: Nothing Is Not More Permanent Than The Temporary

46

Working with tempdb in SQL Server 2005

http://technet.microsoft.com/en-us/library/cc966545.aspx

Capacity Planning for TempDB

http://msdn.microsoft.com/en-us/library/ms345368.aspx

TempDB Database

http://msdn.microsoft.com/en-us/library/ms190768.aspx

Concurrency enhancements for the TempDB database

http://support.microsoft.com/kb/328551

How to shrink the TempDB database in SQL Server

http://support.microsoft.com/kb/307487

11/2/2009 Dubi Or Not To Be

Page 47: Nothing Is Not More Permanent Than The Temporary

47

END or AND ?

D.O.N.T.B

11/2/2009 Dubi Or Not To Be