【发布时间】:2021-07-24 15:22:21
【问题描述】:
我有一个程序来返回最近的 vachile 列表,我使用空间索引来测试这个程序 我的程序是:
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
我有表 Vehicle 并且在我的表中我有 locationVehicle 这条记录有地理类型并且在这条记录上我有这样的空间索引
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
但是当我测试我的程序以查看我的计划时,我在这个计划中没有看到我的空间索引,我不知道为什么有人可以告诉我为什么我没有看到执行计划中的空间索引? 我只看到这个表的 pk 键
【问题讨论】:
-
where (@start.STDistance(locationVehicle)/1000 is not null)?为什么/1000?NULL/1000 = NULL其中{any non-NULL value} / 1000 = {a non-NULL value}。/1000不会在这里帮助 RDBMS。 -
/1000 因为我想在返回时看到公里,如果我删除 /1000 会更好吗?
-
这不存在为什么它在
WHERE虽然@Konrad。 -
什么在哪里不存在? @Lamu 我想以公里为单位查看与车辆的距离,并且距离不能为空
-
但我必须从 Vehicle [WITH(INDEX(
))] 中使用类似的东西?
标签: sql sql-server indexing spatial