【发布时间】:2021-07-06 02:27:41
【问题描述】:
我想测试我的程序在使用和不使用索引的情况下的性能,并比较经过的时间和 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, idGroupVehicle, brand, model, maxRange, weight, maxSpeed, nameLocation, @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,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,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
---@idGroupVehicle is null and @ResultCount is null
else if @idGroupVehicle is null and @ResultCount 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 <= @Radiant)
order by @start.STDistance(locationVehicle)/1000 asc
--- @idGroupVehicle is null
else if @idGroupVehicle is null
select TOP(@ResultCount) idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,
@idGroupVehicle int = null,
@ResultCount int= null,
@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,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
end
---@Result null radiant null
if @ResultCount is null and @Radiant is null
begin
select top 10 idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,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
end
---@idGroupVehicle is null and @ResultCount is null
if @idGroupVehicle is null and @ResultCount is null
begin
select top 10 idVehicle,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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,idGroupVehicle,brand,model,maxRange,weight,maxSpeed, nameLocation , @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
是否有可能以其他方式测试过程以显示受影响的执行时间和行,而不对过程进行任何更改?
【问题讨论】:
-
STATISTICS IO通常是衡量性能的最佳指标,尤其是logical reads。可以从execution plan的属性中收集更多信息 - 您是否查看过每个计划? -
我希望从这个过程中看到很多表扫描,因为
STDistance()使用sqrt()操作可能会变得非常昂贵。查询规划器可能不够聪明,无法在预期看到geography1.STDistance(geography2) <= number时使用空间索引,因为/1000可能会将其丢弃。 -
这是 搜索 - 不是“搜索” ....
-
是的,我审查了计划,但是这个程序的计划与索引相同,没有索引可能我没有足够的数据?
标签: sql-server performance stored-procedures indexing geospatial