Roadmap › 2. Database, ETL & BI › Step 29
SQLi
SQL Server Internals — How the Database Engine Really Works
The SQL Server architecture (protocol layer, relational engine, storage engine, SQLOS), the life of a query from TDS packet to result, pages, extents and allocation maps, heaps vs clustered tables and B+ tree index structure, row layout, statistics and cardinality estimation, the query optimizer and plan cache, parameter sniffing, execution plan operators and join algorithms, buffer pool and memory grants, the transaction log, write-ahead logging, checkpoints and recovery, locking, latches, isolation levels, row versioning and tempdb, deadlocks, waits and DMVs, columnstore and In-Memory OLTP.
What it is & where it is used
SQL Server looks simple from outside — you send T-SQL and get rows — but inside it is an operating system of its own. Understanding it explains why indexes help, why a query is slow, why blocking and deadlocks happen and how data survives a crash.
text
Client (ADO.NET / EF / SSMS)
│ TDS (Tabular Data Stream) over TCP 1433
┌──▼──────────────────────────────────────────────────────────┐
│ PROTOCOL LAYER (SNI) — connections, TDS packets │
├──────────────────────────────────────────────────────────────┤
│ RELATIONAL ENGINE (query processor) │
│ Parser → Algebrizer (binding) → Query Optimizer → Executor │
│ Plan cache │
├──────────────────────────────────────────────────────────────┤
│ STORAGE ENGINE │
│ Access methods (scans, seeks, B-trees, heaps) │
│ Buffer manager (buffer pool) · Transaction manager │
│ Lock manager · Log manager │
├──────────────────────────────────────────────────────────────┤
│ SQLOS — schedulers, workers, memory, waits, I/O, deadlock detection │
└──────────────────────────────────────────────────────────────┘
Data files (.mdf/.ndf, 8 KB pages) Log file (.ldf, sequential) tempdbVersions & history
| Version | Year | Internals milestones |
|---|---|---|
| SQL Server 7.0 | 1998 | Rewritten storage engine: 8 KB pages, row-level locking, cost-based optimizer |
| 2000 | 2000 | Indexed views, user-defined functions |
| 2005 | 2005 | Row versioning (snapshot isolation, RCSI), DMVs, partitioning, SQLOS |
| 2008 / R2 | 2008–2010 | Data compression, filtered indexes, Extended Events, Resource Governor |
| 2012 | 2012 | Columnstore indexes, AlwaysOn Availability Groups |
| 2014 | 2014 | In-Memory OLTP (Hekaton), new cardinality estimator (CE 120) |
| 2016 | 2016 | Query Store, temporal tables, updatable columnstore improvements |
| 2017 | 2017 | Linux support, adaptive query processing |
| 2019 | 2019 | Intelligent Query Processing (scalar UDF inlining, table variable deferred compilation), Accelerated Database Recovery |
| 2022 | 2022 | Parameter Sensitive Plan optimisation, DOP/CE feedback, ledger tables, Query Store on by default for new DBs |
| 2025 | 2025 | Vector data type and vector search, native JSON type, regex functions, optimized locking |
Lifecycle
Life of a query — SELECT Name FROM dbo.Customers WHERE Id = 42:
- Connect — the client opens a TCP connection (pooled by ADO.NET), authenticates, gets a session (SPID).
- Send — the batch arrives as TDS packets; a worker thread on a scheduler (one per CPU core) picks it up.
- Parse — syntax check → parse tree.
- Algebrize (bind) — resolve names (tables, columns, types), check permissions → query tree.
- Plan cache lookup — hash the query text; if a cached plan exists, reuse it (skip optimisation).
- Optimize — cost-based optimizer uses statistics to estimate rows, explores alternatives (indexes, join orders and algorithms) and chooses the cheapest execution plan; plan stored in cache.
- Execute — the executor runs plan operators; the storage engine reads needed pages through the buffer pool (from memory if cached, otherwise physical reads from disk).
- Lock — shared locks (or row versions) protect consistency according to the isolation level.
- Return — rows are streamed back in TDS packets; locks released per isolation rules.
For a write: the change is applied to pages in memory, the log record is written to the transaction log first (write-ahead logging), and on COMMIT the log is hardened to disk; dirty data pages are written later by checkpoint/lazy writer.
Tools used to develop
- SSMS — actual/estimated execution plans, Live Query Statistics, Activity Monitor, Query Store reports.
- DMVs —
sys.dm_exec_requests,sys.dm_exec_sessions,sys.dm_exec_query_stats,sys.dm_os_wait_stats,sys.dm_tran_locks,sys.dm_db_index_physical_stats,sys.dm_db_index_usage_stats,sys.dm_exec_query_plan. - Extended Events (replaces SQL Profiler),
system_healthsession (deadlock graphs). - Query Store — plan history, regressions, forcing plans.
- DBCC —
DBCC PAGE,DBCC IND(undocumented; learning),DBCC SHOW_STATISTICS,DBCC CHECKDB,DBCC SQLPERF(LOGSPACE). - Community:
sp_WhoIsActive, First Responder Kit (sp_Blitz,sp_BlitzIndex,sp_BlitzCache), SentryOne Plan Explorer.