SQL Server Mastery

Statistics: Why 'Out of Date' stats kill performance

1 Views Updated 5/4/2026

SQL Server Statistics

The Query Optimizer is like a GPS. It calculates the fastest route to your data. But just as a GPS needs up-to-date maps, the Optimizer needs Statistics to know how many rows are in each table and how the data is distributed.

1. What are Statistics?

Statistics are binary objects that store histograms of data distribution in a column. If SQL Server knows that only 1% of users are from 'Iceland', it will use an Index Seek. If it thinks 90% of users are from 'Iceland', it will correctly choose an Index Scan because scanning is faster than 1 million look-ups.

2. Stale Statistics

If you insert 1 million rows and don't update statistics, SQL Server might still think the table is empty! It will choose a "Nested Loop" join that works for 10 rows but crashes the server for 1 million. This is called a Bad Cardinality Estimate.

-- Manually update stats for a table
UPDATE STATISTICS Users;

4. Interview Mastery

Q: "What is 'Parameter Sniffing' and how does it relate to statistics?"

Architect Answer: "Parameter Sniffing happens when SQL Server creates an execution plan based on the *first* parameter passed to a stored procedure. If the first user has 10 records, SQL creates a 'Fast' plan. If the next user has 10 million records, SQL tries to use that same 'Fast' plan, which then becomes incredibly slow. We fix this using `OPTION (RECOMPILE)` or by using local variables to prevent the optimizer from 'sniffing' the specific parameter value."

SQL Server Mastery
1. SQL Server Architecture & Basics
SQL Server Internals: How the Storage Engine works Relational Database Design & Normalization (1NF to 3NF) Data Types Mastery: Choosing the right type for performance
2. Advanced T-SQL Querying
Joins Deep Dive: Inner, Outer, Cross, and Self Joins Subqueries vs CTEs: Writing readable, high-performance code Window Functions: ROW_NUMBER, RANK, and LEAD/LAG Aggregations & Grouping Sets: Building complex reports Set Operators: UNION vs UNION ALL, INTERSECT, and EXCEPT
3. Indexing & Performance Tuning
Clustered vs Non-Clustered Indexes: The physical storage reality Covering Indexes & Included Columns: Reducing I/O costs Index Fragmentation: Why it happens and how to fix it Execution Plans: Reading the Query Optimizer's mind Statistics: Why 'Out of Date' stats kill performance SARGability: Writing queries that actually use indexes
4. Database Programmability
Stored Procedures: Security, Performance, and Best Practices User Defined Functions (UDF): Scalar vs Table-Valued Triggers: Auditing changes and the dangers of hidden logic Views & Indexed Views: Abstraction with performance Error Handling: TRY/CATCH and XACT_STATE()
5. Transactions & Concurrency
Transaction Isolation Levels: Read Uncommitted to Snapshot Locking & Blocking: Analyzing Deadlocks like a Pro Optimistic vs Pessimistic Concurrency
6. Administration & Security
SQL Server Security: Logins, Users, and Roles SQL Injection Prevention: Beyond simple parameterization Backup & Recovery Models: Full vs Simple vs Bulk-Logged Automating Maintenance: SQL Agent Jobs & Rebuilding Indexes
7. Modern SQL & Cloud
SQL Server & JSON: Storing and Querying semi-structured data Temporal Tables: Keeping track of data history automatically Introduction to Azure SQL: Database as a Service (PaaS) SQL Server Developer Interview: Junior to Senior Architect Level