Inside tempdb

24
©2011 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice Miroslav Dimitrov Boris Hristov INSIDE TEMPDB (ALMOST LEVEL 500)

Transcript of Inside tempdb

Page 1: Inside tempdb

©2011 Hewlett-Packard Development Company, L.P. The information contained herein is subject to change without notice

Miroslav DimitrovBoris Hristov

INSIDE TEMPDB(ALMOST LEVEL 500)

Page 2: Inside tempdb

2

Our agenda = answer the WWW questions…

1. Tempdb creation

2. Logging

3. Allocations

Page 3: Inside tempdb

Tempdb creation

Page 4: Inside tempdb

4

Right after master, we create tempdbOpen model firstCreate tempdb based on model and propertiesNotify tempdb is readyIf we fail – server goes down!

Page 5: Inside tempdb

5

How?

Lock model and tempdb! Copy extents from model to tempdb (including objects)Fixup the tempdb data fileThen tlog fileCreate attach the other files

-f option1 primary data file the size of model (unlimited fixed auto grow)Build one primary log 516096 – 2vlfs + header = 516096 bytes (unlimited 10% autogrowth)DATA FolderOrginal sizes are retained

Page 6: Inside tempdb

Demo

Page 7: Inside tempdb

7

How?

Lock model and tempdb! Copy extents from model to tempdb (including objects)Fixup the tempdb data fileThen tlog fileCreate attach the other files

-f option1 primary data file the size of model (unlimited fixed auto grow)Build one primary log 516096 – 2vlfs + header = 516096 bytes (unlimited 10% autogrowth)DATA FolderOrginal sizes are retained

Page 8: Inside tempdb

8

Before tempdb is ready – no one will use it

Page 9: Inside tempdb

9

Tempdb log

Minimize logging – after images for inserts and updates (only to heaps – temp tables)

No need for crash recovery – who cares?

Then why a tlog?

ONLY FOR ROLLBACK

Log is faster than you think (lazy commits – one note)

No normal checkpoint of pages – skips tempdb (hard Is supported) truncates the loginternal objects – mostly allocation logging

Page 10: Inside tempdb

Tempdb logging

Page 11: Inside tempdb

Demo

Page 12: Inside tempdb

12

After image logging

Table with value of 1

Page 13: Inside tempdb

Tempdb allocations

Page 14: Inside tempdb

14

What is there in tempdb exactly?

User Objects

• Temp tables• Table variables• UDFs• Online index space• …

Internal Objects

• Sort tables• Worktables• Workfiles• Version Store

Page 15: Inside tempdb

15

Temporary table caching

We cache…• Stored procs• Triggers • Functions

Why?

Page 16: Inside tempdb

16

Internal objects

Internal

Objects

• Sort tables• Worktables• Workfiles• Version Store

Page 17: Inside tempdb

17

TempDB allocations

PFS / GAM / SGAM pages

The only place MS do non-logged allocations!

What about contention?

Page 18: Inside tempdb

18

How is a table created inside tempdb?

Build system catalog enries

Why?

Why?

Allocate IAM – fina a mixed extent and mark in PFS

Allocate 1 page Record allocation in

system tables

Page 19: Inside tempdb

19

Tempdb Algorithm – how to choose a file?

Round robin/proportio

nal fill approach

For each “allocation” we

switch to next file with “equal free

space”

Skip files that are not so free

We calculate the “proportion”- After recovery- When adding or removing

files- Every 8192 extent

allocations

Trace flag 1165 – dump numbers

Page 20: Inside tempdb

20

Allocation of user and tempdb

And why is SQL Server 2005 SP2 CU KB9836185 that important?

Page 21: Inside tempdb

The pressure

• PFS, GAM and SGAM• Frequent create/drop of tables• Internal objects allocation/deallocation

The symptoms

• PAGELATCH waits• NOT PAGELATCHIO waits

The solution

• Temp table caching• Trace flag 1118• Multiple data files

(always restart after adding!)

The collision

Page 22: Inside tempdb

TempDB Files - benchmark

Page 23: Inside tempdb

Demo

Page 24: Inside tempdb

THANK YOU