SQL Server搜索查询,在select条件中添加top后只读取有限的行



当我们取前25行时,我们有一个活动记录为1700的表

select top 25 * from table where isactive=1 

行数和预估行数相同,实际执行计划为25。

但是当我们只取挂起的行时,它正在搜索整个活动记录

select top 25 * from table where isactive=1 and pending=1

当我们添加挂起条件时,实际执行计划中读取的行数为1700行,估计的行数为25行。

我想要一个查询,即使在添加pending=1条件之后,读取的行数也应该是25,只有在实际执行计划中。

希望您已经有了如下的索引。

create index idx_table_isactive on table(isactive) include (pending) with(drop_existing=on)
create index idx_table_isactive on table(isactive,pending)  with(drop_existing=on)

不要在Select语句中使用*。如果您有*(表中的所有列),则索引将无法正常使用。如果任何一个索引都已就位,请尝试如下操作。

SELECT TOP 25 isactive,
pending 
FROM table 
WHERE isactive=1 
AND pending=1

最新更新