Overview

SQL Server stores data in 8 KB pages, grouped into 8-page (64 KB) extents, inside files that belong to filegroups. The transaction log is separate and sequential. tempdb is a shared scratch database recreated at every start.

Core concepts

  • Page types: data, index, text/LOB, allocation maps (GAM, SGAM, PFS), and so on. A row must fit in a page (8,060 bytes in-row); larger values go off-row.
  • Heap vs clustered table: a table without a clustered index is a heap; with one, the rows live in the index order (sqlserver-indexes).
  • Files and filegroups: PRIMARY plus optional user filegroups. Files in a filegroup are filled proportionally, which spreads I/O. Partitions can be mapped to different filegroups (sqlserver-partitioning).
  • Autogrowth: set a fixed growth size in MB, not a percentage, and pre-size files. Log growth is always zero-initialised; data files can skip zeroing with instant file initialisation (a Windows privilege; check the documentation for your platform).
  • Transaction log is divided into virtual log files (VLFs). A log record space is reusable only after it is backed up (FULL/BULK_LOGGED recovery model) or checkpointed (SIMPLE), and no active transaction needs it. sys.databases.log_reuse_wait_desc tells you why the log cannot truncate.
  • tempdb holds temp tables and table variables, sort/hash spill, worktables, the row-version store for snapshot isolation (sqlserver-isolation-rcsi) and index-rebuild sorting.

Worked example

Files of the test instance (SQL Server 2022):

SELECT DB_NAME(database_id) db, name, type_desc, size*8/1024 AS size_mb, growth, is_percent_growth
FROM sys.master_files WHERE database_id IN (DB_ID('kb'), 2) ORDER BY database_id, file_id;
dbfiletypesize MBgrowth
tempdbtempdev, tempdev2 … tempdev8ROWS8 each8192 pages (64 MB), fixed
tempdbtemplogLOG864 MB
kbkbROWS136 (after loading 500k rows)64 MB
kbkb_logLOG20064 MB

tempdb was created with eight equally sized data files. Multiple equal files reduce allocation-page contention under heavy temp object use; modern setup creates up to eight automatically (and from SQL Server 2016, trace flags 1117/1118 behaviour is built in).

Why the log will not shrink:

SELECT name, recovery_model_desc, log_reuse_wait_desc FROM sys.databases;
-- LOG_BACKUP means: take a log backup (FULL recovery); ACTIVE_TRANSACTION means a long open transaction

Pitfalls and production notes

  • Never schedule DBCC SHRINKFILE regularly; it fragments indexes and the file grows again. Shrink only after a one-off large deletion.
  • A database in FULL recovery with no log backups grows its log without bound (sqlserver-backup-recovery).
  • Keep data files in a filegroup the same size so proportional fill works.
  • Put tempdb on the fastest local storage; monitor sys.dm_db_file_space_usage to see what is consuming it (user objects, internal objects, version store).
  • Too many small VLFs (from tiny autogrowth) slow recovery and log backup; check sys.dm_db_log_info.

Practice questions

  1. The log file is 90 GB and log_reuse_wait_desc = LOG_BACKUP. What do you do?
  2. Why is percentage autogrowth a bad idea on large files?
  3. Which workloads stress tempdb most?

References

  • Microsoft Learn, Pages and extents architecture guide; Transaction log architecture; tempdb database

sqlserver-architecture · sqlserver-backup-recovery · sqlserver-partitioning · sqlserver-isolation-rcsi