Skip to main content

SQL Server Storage & Transaction Log Architecture

Understanding how SQL Server organizes data on disk is critical for performance tuning, capacity planning, and disaster recovery.

SQL Server Storage Architecture

Storage Hierarchy

SQL Server Storage Layers
*
Database
Logical container
Contains filegroups
Has a transaction log
*
Filegroup
Logical grouping of data files
PRIMARY (default)
User-defined filegroups
Tables/indexes assigned to filegroups
*
Data File (.mdf/.ndf)
.mdf = primary data file
.ndf = secondary data files
Contains pages and extents
*
Transaction Log (.ldf)
Sequential write-ahead log
One or more log files
Cannot be placed in a filegroup
Critical for recovery

Pages & Extents

Everything in SQL Server is stored in 8 KB pages - the fundamental unit of storage.

Page Types
*
Data Pages
- Store table row data
- 8 KB each (8,096 bytes usable)
- Row offset array at end
- Max row size: 8,060 bytes
*
Index Pages
- Store B-tree index entries
- Non-leaf: pointers to child pages
- Leaf: index key + row locator
- Clustered leaf = data page
*
System Pages
- GAM: tracks extent allocation
- SGAM: tracks mixed extents
- PFS: tracks page fullness
- IAM: maps pages to an object

Extent Types

TypeSizeUsage
Uniform extent8 contiguous pages (64 KB)All 8 pages belong to one object
Mixed extent8 contiguous pages (64 KB)Pages can belong to different objects
Best Practice

Small tables start on mixed extents (sharing space with other objects). Once a table grows beyond 8 pages, SQL Server allocates uniform extents. This is managed automatically - no DBA action needed. Reference: Microsoft Learn: Pages and Extents Architecture

Transaction Log Architecture

The transaction log is the most critical file for data integrity. It's a sequential, append-only write stream.

Transaction Log Internals
*
Virtual Log Files (VLFs)
- Log is divided into VLFs
- Each VLF is a segment of the log
- Active VLFs cannot be reused
- Too many VLFs = slow recovery
- Ideal: 50-200 VLFs per log file
*
Log Truncation
- Marks inactive VLFs as reusable
- Happens after: checkpoint (SIMPLE)
or log backup (FULL/BULK_LOGGED)
- Without truncation, log grows unbounded
- log_reuse_wait_desc shows why log is not truncating

Recovery Models

Recovery ModelLog TruncationPoint-in-Time RestoreUse Case
SIMPLEAt checkpoint (automatic)NoDev/test, read-only data, acceptable data loss
FULLAt log backup onlyYes - to any pointProduction OLTP - full recoverability
BULK_LOGGEDAt log backupLimited (not during bulk ops)Bulk load windows - then switch back to FULL
⚠️ Watch Out

The #1 log management mistake: Setting recovery model to FULL but never taking log backups. The log will grow forever because truncation only happens on log backup. Either take regular log backups (every 15-30 min for OLTP) or switch to SIMPLE if you don't need point-in-time recovery.

🔀 Oracle → Azure

Oracle parallel: SQL Server's transaction log combines what Oracle splits into redo logs (for crash recovery) and archived redo logs (for point-in-time recovery). FULL recovery model ~= Oracle ARCHIVELOG mode. SIMPLE ~= NOARCHIVELOG mode.

TempDB Architecture

TempDB is a special system database - recreated fresh on every SQL Server restart.

What Uses TempDB
*
Query Operations
- Sort spills (when sort exceeds memory grant)
- Hash joins / hash aggregates
- Worktables for cursors
- Spools for parallel queries
- DBCC CHECK operations
*
User Objects & Version Store
- Temp tables (#temp, ##global)
- Table variables (@table)
- Snapshot isolation version store
- RCSI version store
- Online index rebuild version store

TempDB Best Practices

PracticeWhy
Multiple data filesReduce allocation contention (GAM/SGAM/PFS)
Equal size filesProportional fill ensures even distribution
Files = logical CPUs (up to 8)Then add 4 at a time if contention persists
Pre-size filesAvoid autogrowth during production workload
Place on fast storageNVMe/local SSD preferred - TempDB is ephemeral
Enable trace flag 1118 (pre-2016)Force uniform extent allocation (default behavior in SQL Server 2016+ via MIXED_PAGE_ALLOCATION OFF)
🎯 Exam Focus

SQL Server 2016+ automatically optimizes TempDB during setup: multiple files, trace flag 1118 behavior by default, and uniform extent allocation. For older versions, these are manual DBA configurations. Reference: Microsoft Learn: TempDB


Flashcards

What is the fundamental unit of storage in SQL Server?
Click to reveal answer
The 8 KB page (8,096 usable bytes per page). All data, index entries, and system structures are stored in pages.
1 / 7