我有一个带有字符串列和谓词的表,该谓词检查具有一定长度的行。在SQL Server 2014中,无论我要检查的长度如何,我都会看到1行的估计。这产生了非常糟糕的计划,因为实际上有成千上万甚至上百万的行,并且SQL Server选择将此表放在嵌套循环的外侧。
对于SQL Server 2014的基数估计为1.0003,而SQL Server 2012的基数估计为31,622,是否有解释?有没有好的解决方法?
以下是问题的简短摘要:
-- Create a table with 1MM rows of dummy data
CREATE TABLE #customers (cust_nbr VARCHAR(10) NOT NULL)
GO
INSERT INTO #customers WITH (TABLOCK) (cust_nbr)
SELECT TOP 1000000
CONVERT(VARCHAR(10),
ROW_NUMBER() OVER (ORDER BY (SELECT NULL))) AS cust_nbr
FROM master..spt_values v1
CROSS JOIN master..spt_values v2
GO
-- Looking for string of a certain length.
-- While both CEs yield fairly poor estimates, the 2012 CE is much
-- more conservative (higher estimate) and therefore much more likely
-- to yield an okay plan rather than a drastically understimated loop join.
-- 2012: 31,622 rows estimated, 900K rows actual
-- 2014: 1 row estimated, 900K rows actual
SELECT COUNT(*)
FROM #customers
WHERE LEN(cust_nbr) = 6
OPTION (QUERYTRACEON 9481) -- Optionally, use 2012 CE
GO
我还阅读了有关SQL Server 2014基数估计器的白皮书,但没有发现任何可以澄清这种情况的内容。