我在一个表上设置了一个空间索引,其中包含 130 万条记录,这些记录都是经过地理编码的。这些值存储在地理数据类型列中。我遇到的问题是,当我查询这个具有空间索引的列时,速度仍然很慢。例如,查找一英里内的所有帐户大约需要 20 秒。
下面是一个运行缓慢的查询示例:
DECLARE @g Geography;
SET @g = (select ci.Geocode from CustomerInformation ci where ci.CIOI = 372658)
DECLARE @region geography = @g.STBuffer(1609.344)
Select top 100 ci.Geocode.STDistance(@g), ci.CIOI
from CustomerInformation ci
where ci.Geocode.Filter(@region) = 1
order by ci.Geocode.STDistance(@g) asc
这是我的创建索引语句:
CREATE SPATIAL INDEX [IX_CI_Geocode] ON [dbo].[CustomerInformation]
(
[Geocode]
)USING GEOGRAPHY_GRID
WITH (
GRIDS =(LEVEL_1 = MEDIUM,LEVEL_2 = LOW,LEVEL_3 = LOW,LEVEL_4 = LOW),
CELLS_PER_OBJECT = 128, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
GO
数据是单个州一部分的每个房屋。因此,在一英里半径内,我预计会有 1000 点或更多。我是否正确索引了这个?任何帮助都会很棒。
另一个慢查询示例:
DECLARE @g Geography;
SET @g = (select ci.Geocode from CustomerInformation ci where ci.CIOI = 372658)
select top(100) CIOI, (ciFinding.Geocode.STDistance(@g) / 1609.344) as Distance, ciFinding.Geocode.ToString() --ciFinding.Geocode.STDistance(@g) / 1609.344
from CustomerInformation ciFinding
where ciFinding.Geocode.STDistance(@g) is not null and ciFinding.Geocode.STDistance(@g) < 1609.344
order by ciFinding.Geocode.STDistance(@g)