Professional Documents
Culture Documents
dk
Whoami
Tech
Lead @ iPaper
Developer/DBA/Sysadmin/Project
manager/*
Comp.Sci
Blogging
@ Aarhus University
at improve.dk
@improvedk
Author
Disclaimer
Level
teach!
Based
I
on 2008 R2
have no idea...
Most
Background
Presentation
Formally
started OrcaMDF
}
}
OrcaMDF Querying
using (var m df = new M dfFile(m dfPath))
{
var scanner = new D ataScanner(m df);
var row s = scanner.ScanTable("Persons");
EntityPrinter.Print(row s);
}
Pages
The foundation of SQL Server storage
What Is a Page?
8192
bytes
Everything
stored
as pages
Undocumented DBCC
Commands & Flags
DBCC
IND
DBCC
PAGE
DBCC
TRACEON (3604)
Documented
Unofficially
documented
9
Page Header
Absolutely
no documentation
Absolutely
10
OrcaMDF:
PageHeader
11
Slot Array
Points
to beginning
of records in body
Defines
logical
order of records
12
Records
Data
records
Index
records
14
Status Bits A
15
Status Bits B
16
NULL Bitmap
Bitmap
NULL
CEIL(#Cols
Always
/ 8) bytes
garbage
17
18
Example Record
CREATE TABLE
RecordTest
(
A int,
B int,
C char(5),
D varchar(10),
E varchar(20)
)
INSERT INTO RecordTest VALUES (25, 38, 'ABCD', 'Mark',
19
'Denmark')
Even if null
Though not necessarily tail columns!
Variable
null
Adding
20
Data Types
How are data types stored within a record?
float, etc.
Variable
text, etc.
sql_variant
Please just stay away from it
22
LOBs
text, ntext, image, varchar(MAX),
23
In-row (n)varchar(x)
Storage
Complex Columns
DEMO
Identified
0b1001001110010101
= 37.781
0b0001001110010101
= 5.013
Use
cases
Row-overflow/LOB pointers
Sparse vectors
Back pointers
25
nvarchar, varbinary
DEMO
26
27
28
BLOB_FRAGMENT Record
Blob row at: Page (1:21) Slot 0 Length: 4514 Type: 3
(DATA)
Blob Id:469368832
Stored
pages
30
Timestamp == Blob ID
varchar(MAX), nvarchar(MAX),
varbinary(MAX)
Three
scenarios
DEMO
32
etc.
12
byte header
Array
of 12 byte references
Only
Also
in a single-column meta
record
35
DATA
Blob row at: Page (1:176) Slot 0 Length: 8054 Type: 3
(DATA)
Blob Id:1210253312
Type
Where
Size
(SMALL_ROOT)
36
DATA
How
DATA record?
8096 Page body size
8080 (8094) Theoretical max
8040 (8054)- Reality
37
INTERNAL
Blob row at: Page (1:55) Slot 0 Length: 324 Type: 2 (INTERNAL)
Blob Id: 1210253312 Level: 0 MaxLinks: 501 CurLinks: 19
Child 0 at Page (1:176) Slot 0 Size: 8040 Offset: 8040
Child 1 at Page (1:177) Slot 0 Size: 8040 Offset: 16080
Type
CurLinks
MaxLinks
Level
Size
= number of references
=?
= tree level
= computed
38
INTERNAL
40
8096 20 2
In theory (INTERNAL)...
16
504
In reality... 500
500 8040 4,020,000
2 500 8040 8,040,000
5 500 8040 20,100,000
6 500 8040 24,120,000
41
42
43
8,040,000,000
bytes / 7,48 GB
(MAX)
limit is 231-1
Many
permutations
44
MyTable, Option,
ON/OFF
Even
more permutations
text
45
Textpointer
Used
Complex
column
46
Textpointer
=evil
47
SMALL_ROOT
Type
=0
Used
Min
size = 84
Data
LARGE_ROOT_YUKON
Type
=5
Min
size = 84
Part
of LOB tree
49
50
51
NULL
0-64
0-64
0-64
65-8000
65-8000
65-8000 *
(+ 24 +
14)
100 + 14 + 658000
8kb
+
N/A
24 + X
100 + X
Extreme
impact
on small data
The
Performance
diferences
http://sqlblog.com/blogs/paul_white/archive/2011/02/23/Advanced-TSQL-Tuning-Why-Internals
-Knowledge-Matters.aspx
52
Name
SMALL_ROOT
INTERNAL
DATA
LARGE_ROOT_YUKON
9+
53
54
Archeology 101
Type
Name
SMALL_ROOT
LARGE_ROOT
INTERNAL
DATA
LARGE_ROOT_SHILOH
LARGE_ROOT_YUKON
SUPER_LARGE_ROOT
7
8
NULL
9+
INVALID
55
LOB Summary
<
>
Text/ntext/image
horribly inefficient
56
Clustered
index
Heap
Data stored whereever SQL Server wants to
Row identified by RID
58
B+-tree Scanning
59
Heaps
Relies
Leaf
on IAM pages
Except...
60
Extents
All
extent
Mixed
extents
Uniform
First
extents
62
GAM Pages
1 = Free, 0 = Allocated
GAM interval
63
SGAM Pages
Shared
free pages
Structure
3
identical to GAM
IAM Pages
Index
1
Allocation Map
allocation unit
0
No
fixed positioning!
Tracks
a GAM interval
Structure
66
SGAM Any
IAM
Status
0
0
1
0
1
1
1
1
0
0
1
0
1
1
0
1
0
1
1
0
1
PFS Pages
Page
Free Space
Bytemap
1
PFS
interval
Only
69
Allocation Units
70
DBCC
Lots
of interesting info
Physical version
Log rebuild count
Last OK CHECKDB
Last LOG backup
Name + ID
FirstSysIndexes
71
sys.index_columns
Indexes
Root page
Heaps
IAM chain root
DEMO
73
Allocation Metadata
Overview
74
Chicken
How
or the egg
views?
DEMO
75
Base Tables
The
Can
Here be dragons!
Confusing
Utilizes
column names
internal functions
76
* FROM sys.sysschobjs
sysschobjs
syscolpars
sysrowsets
sysallocunits
DEMO
77
DEMO
78
OPENROWSET(TABLE
RSCPROP)
SELECT
CASE c.maxinrowlen
WHEN 0 THEN p.length
ELSE c.maxinrowlen
END AS max_inrow_length,
p.xtype AS system_type_id,
p.length AS max_length,
p.prec AS PRECISION,
p.scale AS scale,
FROM
sys.sysrscols c sys.system_internals_partition_columns
OUTER APPLY
OPENROWSET(TABLE RSCPROP, c.ti) p
79
OPENROWSET(TABLE
RSCPROP)
80
OPENROWSET(TABLE
RSCPROP)
CREATE TABLE TITest
(
A binary(50),
B char(10),
C datetime2(5),
D decimal(12, 5),
E float,
F int,
G numeric(11, 4),
H nvarchar(50),
I nvarchar(max),
J time(3),
K tinyint,
L varbinary(max),
M varchar(75),
N text
)
81
OPENROWSET(TABLE
RSCPROP)
SELECT
t.name,
r.ti,
p.scale,
p.precision,
p.max_length,
p.system_type_id,
p.max_inrow_length
FROM
sys.system_internals_partition_columns p
INNER JOIN
sys.sysrscols r ON
r.rscolid = p.partition_column_id AND
r.rsid = p.partition_id
INNER JOIN
sys.types t ON
t.system_type_id = p.system_type_id AND
t.user_type_id = p.system_type_id
WHERE
partition_id = 72057594040614912
82
OPENROWSET(TABLE
RSCPROP)
83
OPENROWSET(TABLE
RSCPROP)
12973
= 0x000032AD
173
50
84
OPENROWSET(TABLE
RSCPROP)
1322
42
5
= 0x0000052A
25
= 20 + scale
85
OPENROWSET(TABLE
RSCPROP)
330858 = 0x00050C6A
106
12
86
87
Data Recovery
When everything else fails
available
Make
Run
This
89
pages
Corrupt
Bad
pages
metadata
Accidental
How
90
Torn Pages
One
First
No
header
No
slot array
Corrupt Pages
Checksum
Could
Treat
92
Bad Metadata
SQL
Server bugs
Corrupt/torn
pages
Scan
Scan
Deduce
schema
App
Docs
Record format
93
delete
Accidental
truncation
94
page allocated?
structures
95
class
tornbits / checksum
methods
Best-efort
parsing of pages
96
Questions
Nows the chance
Thank you!
Blog:
improve.dk
Twitter: @improvedk
Email: mark@improve.dk