1.01k likes | 1.35k Views
Mark S. Rasmussen – improve.dk. Knowing the Internals, Who Needs SQL Server Anyway?. Whoami. Tech Lead @ iPaper Developer/DBA/Sysadmin/Project manager/* Comp.Sci @ Aarhus University Blogging at improve.dk @improvedk Author of the OrcaMDF project. Disclaimer.
E N D
Mark S. Rasmussen – improve.dk Knowing the Internals, Who Needs SQL Server Anyway?
Whoami • Tech Lead @ iPaper • Developer/DBA/Sysadmin/Project manager/* • Comp.Sci @ Aarhus University • Blogging at improve.dk • @improvedk • Author of the OrcaMDF project
Disclaimer • Level 650 – meant to inspire, not teach! • Based on 2008 R2 • I have no idea... • Most of what I say is incorrect
Background • Presentation at Miracle Open World • Formally started OrcaMDF
Old School Querying • privatestaticvoid oldschool() • { • using (varconn=newSqlConnection("Data Source=.;Initial Catalog=QFD;…") • { • conn.Open(); • varcmd=newSqlCommand("SELECT * FROM Persons", conn); • var reader = cmd.ExecuteReader(); • while(reader.Read()) • Console.WriteLine(reader["ID"] +": "+ reader["Name"] +" ("+ reader["A • } • }
OrcaMDF Querying • using (varmdf=newMdfFile(mdfPath)) • { • var scanner =new DataScanner(mdf); • var rows = scanner.ScanTable("Persons"); • EntityPrinter.Print(rows); • } • using (varmdf=newMdfFile(mdfPath)) • { • var scanner =new DataScanner(mdf); • var rows = scanner.ScanTable("Persons") • .Where(x => x.Field<short>(”Age”) < 40); • EntityPrinter.Print(rows); • }
Pages The foundation of SQL Server storage
What Is a Page? • 8192 bytes • Everything storedas pages
Undocumented DBCC Commands & Flags • DBCC IND • DBCC PAGE • DBCC TRACEON (3604) • Documented in 6.5 & 7.0 • Unofficially documented
Page Header • Absolutely no documentation • Absolutely necessary for parsing
Reverse Engineering the Header • DEMO • OrcaMDF: PageHeader
Slot Array • Points to beginningof records in body • Defines logicalorder of records
Records • Data records • Stores table data • Index records • Stores nonclustered index data, as well as non-leaf level clustered index data • Stored in the FixedVar format
NULL Bitmap • Bitmap tracking whether columns are NULL • CEIL(#Cols / 8) bytes • Always present on data pages, except when it’s not • Only trust defined bits – rest may be garbage
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', 'Denmark')
When Is Data Present? • Fixed length data always present • Even if null • Though not necessarily tail columns! • Variable length data only present when not null • Adding nullable columns is a metadata op • Denali default value columns is metadata too!
Data Types How are data types stored within a record?
Classifying Data Types • Fixed length data types • bit, char, int, decimal, date, datetime, float, etc. • Variable length data types • (n)varchar, varbinary, varchar(MAX), text, etc. • sql_variant • Please just stay away from it
Variable Length Data Types • SLOBs • varchar(x), nvarchar(x), varbinary(x) • LOBs • text, ntext, image, varchar(MAX), nvarchar(MAX), varbinary(MAX), xml • vardecimal
In-row (n)varchar(x) Storage • CREATE TABLE VarcharTest • ( • A varchar(4) • ) • INSERT INTO VarcharTest VALUES ('Mark')
Complex Columns • DEMO • Identified using the sign bit • 0b1001001110010101 = 37.781 • 0b0001001110010101 = 5.013 • Use cases • Row-overflow/LOB pointers • Sparse vectors • Back pointers
Off-row SLOB Storage • Varchar, nvarchar, varbinary • DEMO
Off-row SLOB Storage • Column data moved to new page, pointer left behind
Off-row SLOB Storage B = [BLOB Inline Root] Slot 0 Column 2 Offset 0x11a1 Length (physical) 24 Level = 0 Unused = 0 UpdateSeq = 1 TimeStamp = 1298595840 Link 0 Size = 4500 RowId = (1:21:0) • timestamp =BitConverter.ToInt64(data, 8) <<16;
BLOB_FRAGMENT Record Blob row at: Page (1:21) Slot 0 Length: 4514 Type: 3 (DATA) Blob Id:469368832 • Stored on shared (obj-level) TextMix pages
Off-row SLOB Storage • Allways stored in-row if < 24 bytes • 24 byte [BLOB Inline Root] pointer • Data stored in BLOB_FRAGMENT on TextMix page • Timestamp == Blob ID • Performance prediction is tough
(MAX) LOB Storage • varchar(MAX), nvarchar(MAX), varbinary(MAX) • The LOB that wanted to be a SLOB • Three scenarios • [BLOB Inline Data] • [BLOB Inline Root] • [Textpointer] • DEMO
”BLOB Inline Data” • Used when data fits in record • Not an official LOB structure Slot 0 Column 1 Offset 0x0 Length 0 Length (physical) 0 A = [NULL] B = [BLOB Inline Data] Slot 0 Column 2 Offset 0x1393 Length 4 Length (physical) 4 B = 0x41424344
BLOB Inline Root • Can reference up to 5 pages – data, roots, trees, etc. • 12 byte header • Array of 12 byte references • Only used by SLOBs & (MAX) LOBs • Also not a LOB structure (by my definition)
LOB Structure Records • Wrapped in a single-column ”meta” record
DATA Blob row at: Page (1:176) Slot 0 Length: 8054 Type: 3 (DATA) Blob Id:1210253312 • Type 3 • Where data is actually stored • Size always > 64 bytes (SMALL_ROOT)
DATA • How much data can we store in a DATA record? • 8096 – Page body size • 8080 (8094)– Theoretical max • 8040 (8054)- Reality
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 2 • CurLinks = number of references • MaxLinks = ? • Level = tree level • Size = computed
INTERNAL Total record size = 20 + X * 16
The Tree Grows • In theory (INTERNAL)... • In reality... 500
Two Levels Is All It Takes • 8,040,000,000 bytes / 7,48 GB • (MAX) limit is 231-1 • Many permutations
Large Value Types Out of Row • sp_tableoption ‘MyTable’, ‘Option’, ‘ON/OFF’ • Even more permutations • “text in row” 24-7000, default 256
Textpointer • Used for classic LOB types & MAX LOB types with ’large value types out of row’ ON • text, ntext, image • Complex column
Classic Lob Structures • You thought (MAX) was complex? • Textpointer =
SMALL_ROOT • Type = 0 • Used when data <= 64 bytes • Min size = 84 • Data > length = garbage
LARGE_ROOT_YUKON • Type = 5 • Min size = 84 • Part of LOB tree