我最近开始在客户端的存储过程中进行一些性能调整,并且碰到了这段代码,无法找到一种更有效的工作方式。使用太多选择的存储过程?
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 [email protected]
where t.TransactionTime>[email protected] 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
insert命令减慢整个过程并占用99%的资源。
I am not sure but i think these nested loops are referring to the queries inside the where clause
我非常感谢帮助我能得到这个。
谢谢!
你有没有尝试加入表而不是使用exists子句? – Sharad
这些表在它们中只有一列....加入不会解决它:\ –