这个问题出现在现实世界中,但在下面的简单示例中已经存在。我正在使用 SQL Server 2017。
给定一个只有一个非 id 列的表,它只需要一个值:
create table #test (id bigint not null, status smallint not null)
/* Populate with 10000 times value 10 */
;WITH numbers(Number) AS
(SELECT 1 AS Number
UNION ALL
SELECT Number+1 FROM numbers where Number<10000
)
insert into #test (id,status)
select number,10 from numbers option(maxrecursion 10000)
/* Create fresh index */
create index index_status on #test ([status])
DBCC SHOW_STATISTICS ("tempdb..#test", 'index_status') WITH HISTOGRAM
Run Code Online (Sandbox Code Playgroud)
现在我们在此列上查询双范围(0 结果,因为唯一的值在两个范围之外):
select 1
from #test
where status < 10
and …Run Code Online (Sandbox Code Playgroud) sql-server ×1