Gather and Check statistics in Sql Server
How Sql gathers these Statistics
- Optimize does not generaae the right plan without these status
- Other data relevant for SQL Performance include
- the structure of tables and views
- Definition of Indexes
To Generate these statistics in Sql Server
In SQL Server, gathering and maintaining statistics is crucial for the Query Optimizer to create efficient execution plans, leading to optimal query performance. Here are the different ways to gather and manage statistics:
Automatic Statistics Management (Default and Recommended)
- There are built-in technologies in SQL Server that can generate and update statistics automatically. In most cases, they will be the most suitable choices for your task.
- AUTO_CREATE_STATISTICS
- When enabled (which is by default), the Query Optimiser automatically creates single-column statistics on columns used in query predicates (e.g., WHERE clauses, JOIN conditions, ORDER BY clauses) if no index or manually created statistics provide enough information.
- These automatically created statistics usually have names starting with _WA_Sys
- Still we can Enable / disable this defaut behaviour using TSQL
- AUTO_UPDATE_STATISTICS:
- SQL Server automatically updates statistics when table data changes considerably when enabled (which is by default)
- The threshold for "significant change" depends on the table size (e.g., for tables over 500 rows, it's generally 500 + 20% of the table size).
- This ensures the Query Optimizer has reasonably up-to-date information for planning queries.
- Still we can Enable / disable this defaut behaviour using TSQL
- AUTO_UPDATE_STATISTICS ASYNCHRONOUS:
- This option alters the behaviour of AUTO_UPDATE_STATISTICS when statistics become outdated while a query requires them; it is disabled by default.
- synchronous:
- A query with stale statistics will wait for statistics to be updated before compiling and running if AUTO_UPDATE_STATISTICS_ASYNCHRONOUS is OFF. The query utilises the latest stats, although it may delay.
- Asynchronous:
- If AUTO_UPDATE_STATISTICS_ASYNCHRONOUS is ON, a query with stale statistics will compile and execute using the old statistics, while a background thread will update them. Avoiding the delay can improve immediate query response time, but the present query may run with a poor plan. Updated statistics will help future questions.
- Still we can Enable / disable this defaut behaviour using TSQL
- imp: Most OLTP workloads that require consistent plan quality should have this parameter off (synchronous). Allow it for systems that allow occasional inferior plans to avoid statistics update query latency.
- Create/Rebuild Index
- Implicit Statistics: SQL Server automatically builds or updates key column statistics when you establish or rebuild a clustered or non-clustered index.
- Since indexes are generally formed on query columns, this is how statistics are preserved.
- Since SQL Server 2014, partitioned index statistics are generated using the default sampling algorithm, not a complete scan.
- Implicit Statistics: SQL Server automatically builds or updates key column statistics when you establish or rebuild a clustered or non-clustered index.