我必须选择小于给定 id 且大于给定 id 的 2 个值。我已经尝试过这个查询,但有没有更好的方法来做到这一点
小提琴Sql
Begin
declare @rootValue int
declare @repID int
set @repID = 2
set @rootValue = (select Id from tblLookups where Id = @repID)
declare @rootMinusTwo int
declare @rootPlusTwo int
set @rootMinusTwo = (select count(*) from tblLookups where Id < @rootValue)
set @rootPlusTwo = (select count(*) from tblLookups where Id > @rootValue)
if @rootMinusTwo >= 2 and @rootPlusTwo >= 2
Begin
select *
from tblLookups
where Id between @rootValue - 2 and @rootValue …Run Code Online (Sandbox Code Playgroud) 我有一个包含数百万条记录的 SQL 数据库,当我查询数据时
select * from ActCosts where ScenarioID= 456
Run Code Online (Sandbox Code Playgroud)
这些表有 1,323,718 行,它给了我 50,000 多行,这令人惊讶地花费了 3 多分钟。所以我现在正在考虑如何改进这个性能。我发现一种方法是在“SomeID”列上创建索引。我已经创建了这个索引,但查询需要相同的时间

执行计划

索引脚本
CREATE NONCLUSTERED INDEX [IX_ActCost_ScenarioID] ON [dbo].[ActCost]
(
[ScenarioID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
ALTER TABLE [dbo].[ActCost] ADD CONSTRAINT [PK_ActCost] PRIMARY KEY CLUSTERED
(
[ActCostID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, ONLINE …Run Code Online (Sandbox Code Playgroud) performance index sql-server azure-sql-database performance-tuning