【发布时间】:2017-08-04 10:34:06
【问题描述】:
我最近开始对客户的存储过程进行一些性能调整,但我碰到了这段代码,但找不到让它更有效地工作的方法。
declare @StationListCount int;
select @StationListCount = count(*) from @StationList;
declare @FleetsCnt int;
select @FleetsCnt=COUNT(*) from @FleetIds;
declare @StationCnt int;
select @StationCnt=COUNT(*) from @StationIds;
declare @VehiclesCnt int;
select @VehiclesCnt=COUNT(*) from @VehicleIds;
declare @TrIds table(VehicleId bigint,TrId bigint,InRange bit);
insert into @TrIds(VehicleId,TrId,InRange)
select t.VehicleID,t.FuelTransactionId,1
from dbo.FuelTransaction t
join dbo.Fleet f on f.FleetID = t.FleetID and f.CompanyID=@ActorCompanyID
where t.TransactionTime>=@From and (@To is null or t.TransactionTime<@To)
and (@StationListCount=0 or exists (select id fRom @StationList where t.FuelStationID = ID))
and (@FleetsCnt=0 or exists (select ID from @FleetIds where ID = t.FleetID))
and (@StationCnt=0 or exists (select ID from @StationIds where ID = t.FuelStationID))
and (@VehiclesCnt=0 or exists (select ID from @VehicleIds where ID = t.VehicleID))
and t.VehicleID is not null
插入命令会减慢整个过程并占用 99% 的资源。
I am not sure but i think these nested loops are referring to the queries inside the where clause
非常感谢我能在这方面获得帮助。
谢谢!
【问题讨论】:
-
您是否尝试过加入表格而不是使用存在子句?
-
这些表中只有一列......加入不会解决它:\
标签: sql sql-server performance stored-procedures database-administration