如何在sql server中使用空间索引来测试过程?
我有一个程序返回最近的真空列表,我使用空间索引来测试这个程序 我的程序是:如何在sql server中使用空间索引来测试过程?,sql,sql-server,indexing,spatial,Sql,Sql Server,Indexing,Spatial,我有一个程序返回最近的真空列表,我使用空间索引来测试这个程序 我的程序是: create procedure [dbo].[p_search_vehicle] @IdCustomer int, @idGroupVehicle int = null, @ResultCount int= null, @Radiant int= null as begin if @IdCustomer is null begin print 'The argument ca
create procedure [dbo].[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
---@Radiant 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
--- @Result is null
else if @ResultCount 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 <= @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
但是当我测试我的程序来查看我的计划时,我没有在这个计划中看到我的空间索引,我不知道为什么有人能告诉我为什么我没有在执行计划中看到我的空间索引?
我只看到这个表的pk键
将评论中冗长的讨论转化为一个答案,您的查询中的几个地方的米到公里的转换可能就是原因所在。让我们按顺序看一下:
@start.STDistance(locationVehicle)/1000不为空
。如果@start不为null且locationVechicle不为null,这是否可以更好地编写?也就是说,如果两个地理空间点存在,它们之间将有一个距离(可能是0,但即使是0也不是空的`)
按@start.STDistance(locationVehicle)/1000订购。最好写为按@start.STDistance(locationVehicle)订购
。如前所述,SQL必须计算@start
与表中每个点之间的距离,除以1000,然后使用新计算的度量进行排序。简言之,该除法已变为不可分配
这就是说,我还将完全从数据库中删除米到公里的转换,并将其降级到应用层。这是一个较小的计算负担,这仍然是昂贵数据库服务器上的CPU周期,可以在较便宜(且更容易扩展)的应用层上完成。
其中(@start.STDistance)(locationVehicle)/1000不为空)
?为什么/1000
?null/1000=null
其中as{any non-null value}/1000={a non-null value}
。/1000
对这里的RDBMS没有帮助。/1000因为我想在我的回报中看到公里数,如果我删除/1000会更好吗?这不存在为什么它在WHERE
中,@Konrad。WHERE中不存在什么?@Lamu我想看到到车辆的距离,单位是公里,距离不能为空,但我必须这样做从车辆[WITH(INDEX())]中使用类似的方法?也就是说,您建议在所在的行中选择@start.STDistance(locationVehicle)/1000
删除/1000,并在所在的行中(@start.STDistance(locationVehicle)/1000不为空)
更改为@start不为空,locationVechicle不为空
并且按顺序按@start.STDistance(locationVehicle)
删除/1000?好的,我会检查此项,但当我使用提示时,我的执行计划中有一个空间索引
CREATE SPATIAL INDEX [SIndx_Vehicle_locationVehicle] ON [dbo].[Vehicle]
(
[locationVehicle]
)USING GEOGRAPHY_AUTO_GRID
WITH (
CELLS_PER_OBJECT = 12, PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO