尽管统计信息已更新,但SQL Server 2016错误的查询计划估计值

时间:2018-12-28 20:36:47

标签: sql sql-server tsql

我正在优化查询,并且注意到它变得非常慢,因为它估计行数为16.6,实际返回的行数为565824。我更新了统计信息,删除并重新创建了该统计信息,它仍然给出错误的估计。这是针对SQL Server 2016的,不胜感激。

SQL:

select cd_key 
from dbo.CAMPDIV
where cd_camp = 'a' 
  and CD_CAMPYR = '2018' 
option (recompile)

enter image description here

enter image description here

enter image description here

enter image description here

非聚集索引(cd_campyr)的直方图

All Density                     Average Length                  Columns                         
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
0.02040816                      4                               CD_CAMPYR                       
7.412665E-08                    8                               CD_CAMPYR, CD_ID                
7.184833E-08                    18                              CD_CAMPYR, CD_ID, CD_CAMP       

Histogram Steps                 
RANGE_HI_KEY                    RANGE_ROWS                      EQ_ROWS                         DISTINCT_RANGE_ROWS             AVG_RANGE_ROWS                  
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                                0                               792181                          0                               1                               
1979                            0                               230                             0                               1                               
1980                            0                               332                             0                               1                               
1981                            0                               604                             0                               1                               
1982                            0                               622                             0                               1                               
1983                            0                               330                             0                               1                               
1984                            0                               1762                            0                               1                               
1985                            0                               868                             0                               1                               
1986                            0                               551                             0                               1                               
1987                            0                               190                             0                               1                               
1988                            0                               352                             0                               1                               
1989                            0                               519                             0                               1                               
1990                            0                               38829                           0                               1                               
1991                            0                               439486                          0                               1                               
1992                            0                               366357                          0                               1                               
1993                            0                               375469                          0                               1                               
1994                            0                               369176                          0                               1                               
1995                            0                               367691                          0                               1                               
1996                            0                               376979                          0                               1                               
1997                            0                               388239                          0                               1                               
1998                            0                               391408                          0                               1                               
1999                            0                               402551                          0                               1                               
2000                            0                               413392                          0                               1                               
2001                            0                               422470                          0                               1                               
2002                            0                               461895                          0                               1                               
2003                            0                               458726                          0                               1                               
2004                            0                               459876                          0                               1                               
2005                            0                               473357                          0                               1                               
2006                            0                               464213                          0                               1                               
2007                            0                               472373                          0                               1                               
2008                            0                               457623                          0                               1                               
2009                            0                               462268                          0                               1                               
2010                            0                               465633                          0                               1                               
2011                            0                               470338                          0                               1                               
2012                            0                               472091                          0                               1                               
2013                            0                               481586                          0                               1                               
2014                            0                               484236                          0                               1                               
2015                            0                               492460                          0                               1                               
2016                            0                               514569                          0                               1                               
2017                            0                               551739                          0                               1                               
2018                            0                               571969                          0                               1                               
2019                            0                               552550                          0                               1                               
2020                            0                               54                              0                               1                               
2021                            0                               33                              0                               1                               
2022                            0                               21                              0                               1                               
2023                            0                               8                               0                               1                               
2025                            1                               1                               1                               1                               
2099                            0                               1                               0                               1                               

1 个答案:

答案 0 :(得分:4)

您所说的有些奇怪,但是无论如何...覆盖指数可能会帮助甚至产生急剧的增长。 请尝试创建索引:

CREATE INDEX IX_CampDiv_CD_Camp_CD_CampYR ON dbo.CAMPDIV (cd_camp, CD_CAMPYR  ) 
INCLUDE (cd_key)

至少会阻止嵌套循环改善计划。

请分享结果。