问题描述
我想测试我的程序在使用和不使用索引的情况下的性能,并比较运行时间和 cpu 时间。我的程序看起来像:
create procedure p_search_vehicle
@IdCustomer int,@idGroupVehicle int = null,@ResultCount int= null,@Radiant int= null
as
begin
if @IdCustomer is null
begin
print 'The argument cannot be null'
return
end
declare @start geography
set @start = (select location from Customer where idCustomer = @idCustomer)
--- @Result null group null radiant null
if @ResultCount is null and @idGroupVehicle is null and @Radiant is null
select top 10
idVehicle,idGroupVehicle,brand,model,maxrange,weight,maxSpeed,nameLocation,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from
Vehicle
where
(@start.Stdistance(locationVehicle)/1000 is not null)
order by
@start.Stdistance(locationVehicle)/1000 asc
---@Result null radiant null
else if @ResultCount is null and @Radiant is null
select top 10
idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from
Vehicle
where
idGroupVehicle = @idGroupVehicle
and (@start.Stdistance(locationVehicle)/1000 is not null)
order by
@start.Stdistance(locationVehicle)/1000 asc
---@Result null
else if @Radiant is null
select top(@ResultCount) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where idGroupVehicle= @idGroupVehicle and (@start.Stdistance(locationVehicle)/1000 is not null)
order by @start.Stdistance(locationVehicle)/1000 asc
---@@idGroupVehicle null @Radiant is null
else if @idGroupVehicle is null and @Radiant is null
select TOP(@ResultCount) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 is not null)
order by @start.Stdistance(locationVehicle)/1000 asc
---@idGroupVehicle is null and @ResultCount is null
else if @idGroupVehicle is null and @ResultCount is null
select top 10 idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 <= @Radiant)
order by @start.Stdistance(locationVehicle)/1000 asc
--- @idGroupVehicle is null
else if @idGroupVehicle is null
select TOP(@ResultCount) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 <= @Radiant)
order by @start.Stdistance(locationVehicle)/1000 asc
--- all options
else
select TOP(@ResultCount) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where idGroupVehicle= @idGroupVehicle and (@start.Stdistance(locationVehicle)/1000 <= @Radiant)
order by @start.Stdistance(locationVehicle)/1000 asc
end
go
我已经使用 SET STATISTICS IO,TIME ON 测试了这个过程 DBCC FREEPROCCACHE; DBCC 删除缓冲区; 检查点 去,但我必须更改一些东西才能使测试工作,永久设置所有参数并丢弃创建过程并声明每个参数
SET STATISTICS IO,TIME ON
DBCC FREEPROCCACHE;
DBCC DROPCLEANBUFFERS;
CHECKPOINT
GO
declare @IdCustomer int = 1,@Radiant int= null
begin
if @IdCustomer is null
begin
print 'The argument cannot be null'
return
end
declare @start geography
SET @start = (select location from Customer where idCustomer=@idCustomer )
---@Result null group null radiant null
if @ResultCount is null and @idGroupVehicle is null and @Radiant is null
begin
select top 10 idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 is not null)
order by @start.Stdistance(locationVehicle)/1000 asc
end
---@Result null radiant null
if @ResultCount is null and @Radiant is null
begin
select top 10 idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where idGroupVehicle= 1 and (@start.Stdistance(locationVehicle)/1000 is not null)
order by @start.Stdistance(locationVehicle)/1000 asc
end
---@Result null
if @Radiant is null
begin
select TOP(5) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where idGroupVehicle= 1 and (@start.Stdistance(locationVehicle)/1000 is not null)
order by @start.Stdistance(locationVehicle)/1000 asc
end
---@@idGroupVehicle null @Radiant is null
if @idGroupVehicle is null and @Radiant is null
begin
select TOP(5) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 is not null)
order by @start.Stdistance(locationVehicle)/1000 asc
end
---@idGroupVehicle is null and @ResultCount is null
if @idGroupVehicle is null and @ResultCount is null
begin
select top 10 idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 <= 1)
order by @start.Stdistance(locationVehicle)/1000 asc
end
--- @idGroupVehicle is null
if @idGroupVehicle is null
begin
select TOP(5) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where (@start.Stdistance(locationVehicle)/1000 <= 1)
order by @start.Stdistance(locationVehicle)/1000 asc
end
--- all options
else
begin
select TOP(5) idVehicle,@start.Stdistance(locationVehicle)/1000 as distanceInKm
from Vehicle
where idGroupVehicle= 1 and (@start.Stdistance(locationVehicle)/1000 <= 1)
order by @start.Stdistance(locationVehicle)/1000 asc
end
end
是否可以通过其他方式测试程序以显示受影响的执行时间和行,而无需对程序进行任何更改?
解决方法
您不必将过程转换成测试脚本。而只是调用它。并且不要使用冷过程或页面缓存进行测试。这是一种极不寻常的状态。
SET STATISTICS IO,TIME ON
GO
declare @IdCustomer int = 1,@idGroupVehicle int = null,@ResultCount int= null,@Radiant int= null
exec p_search_vehicle @IdCustomer,@idGroupVehicle,@ResultCount,@Radiant
GO
SET STATISTICS IO,TIME OFF