Cardinality Estimation - SQL Server 2022

Cardinality estimation is a critical component of query optimization in SQL Server that estimates the number of rows that a query will return, and uses this information to select an optimal query execution plan. SQL Server 2022 introduces several improvements to its cardinality estimation engine, which is based on a machine learning model, to provide more accurate cardinality estimates. Here are some of the improvements in SQL Server 2022's cardinality estimation engine, along with examples:

Selectivity Estimation for Complex Predicates: SQL Server 2022 can now more accurately estimate the selectivity of complex predicates, such as those involving multiple AND and OR operators, and nested subqueries. It uses a combination of heuristics and machine learning to estimate the selectivity of such predicates more accurately.

For example, consider the following query:

SELECT *

FROM Sales.Orders

WHERE (CustomerID = 123 OR CustomerID = 456) AND (OrderDate BETWEEN '2021-01-01' AND '2021-12-31')

SQL Server 2022 can use heuristics and machine learning to estimate the selectivity of this complex predicate more accurately, which can lead to a more optimal query execution plan.

Interpolation for Missing Statistics: SQL Server 2022 can now use interpolation to estimate the number of distinct values for a column, even when statistics are missing or outdated. Interpolation uses the values in neighboring statistics to estimate the cardinality of the missing statistics.

For example, consider the following query:

SELECT *
FROM Sales.Orders
WHERE CustomerID BETWEEN 100 AND 200

If statistics for the CustomerID column are missing or outdated, SQL Server 2022 can use interpolation to estimate the number of distinct values for the column, which can lead to a more accurate cardinality estimate and a more optimal query execution plan.

Adaptive Cardinality Estimation: SQL Server 2022 can now adapt its cardinality estimation to the actual data distribution of a table, based on feedback from query execution. It uses machine learning to adjust its estimates based on actual query results, which can improve the accuracy of future cardinality estimates.

For example, consider a table with a highly skewed data distribution, where most of the values are concentrated in a small number of rows. If a query returns a small number of rows, SQL Server 2022 can adapt its cardinality estimation to account for this skewed data distribution, and provide more accurate estimates for future queries on the same table.

Overall, the improvements to SQL Server 2022's cardinality estimation engine are designed to provide more accurate cardinality estimates, which can lead to more optimal query execution plans and better query performance. These improvements leverage machine learning and heuristics to handle complex queries, missing or outdated statistics, and highly skewed data distributions.




Comments

Popular posts from this blog

COPILOT Feature in SQL Server 2025

Prefetching - SQL Server

Split Brain - SQL Server