SQL Server 2014 - 一些查询非常慢(基数估计器)

SQL Server 2014 - some queries very slow (cardinality estimator)

在我们的生产环境中,我们有几台服务器 SQL server 2012 SP2+Windows Server 2008R2。 3 个月前,我们将所有服务器迁移到 SQL Server 2014 SP1+Windows Server 2012 R1。我们创建了具有新配置的新服务器(更多 RAM、更多 CPU、更多磁盘 space)并从 SQL Server 2012 备份我们的数据库 --> 恢复到新的 SQL Server 2014 服务器。恢复后,我们将兼容级别从 110 更改为 120+Rebuild Index+Update 统计信息。

但是现在我们有几个查询有问题,当兼容级别为 120 时,运行 非常慢。如果我们将兼容级别更改为旧的 110,它 运行 非常快。

关于这个问题我搜索了很多,但没有找到任何东西。

SQL Server 2014介绍new cardinality estimator

One of the performance improvement in SQL Server 2014 is the redesign of cardinality estimation. The component which does cardinality estimation (CE) is called cardinality estimator. It is the essential component of SQL query processor for query plan generation. Cardinality estimates are predictions of final row count and row counts of intermediate results (such as joins, filtering and aggregation). These estimates have direct impact on plan choices such as join order, join type etc. Prior to SQL Server 2014, cardinality estimator was largely based on SQL Server 7.0 code base. SQL Server 2014 introduces new design and the new cardinality estimator is based on research on modern workloads and learning from past experience.

跟踪标志 94812312 可用于控制使用哪个版本的基数估计器。

检查导致问题的查询并比较 2008 年和 2014 年的执行计划属性 估计行数 实际行数 值。

Cardinality Estimates in Microsoft SQL Server 2014


SQL Server 2016+ 开始,您可以为每个数据库设置旧的基数估计器,而无需使用跟踪标志或将数据库兼容级别更改为 110。

ALTER DATABASE SCOPED CONFIGURATION

This statement enables the configuration of a number of database configuration settings at the individual database level, independent of these settings for any other database.

LEGACY_CARDINALITY_ESTIMATION = { ON | OFF | PRIMARY }

Enables you to set the query optimizer cardinality estimation model to the SQL Server 2012 and earlier version independent of the compatibility level of the database. This is equivalent to Trace Flag 9481. To set this at the instance level, see Trace Flags (Transact-SQL). To accomplish this at the query level, add the QUERYTRACEON query hint.

ON

Sets the query optimizer cardinality estimation model to the SQL Server 2012 and earlier version of the cardinality estimation model.

OFF

Sets the query optimizer cardinality estimation model based on the compatibility level of the database.

ALTER DATABASE SCOPED CONFIGURATION SET LEGACY_CARDINALITY_ESTIMATION = ON;