尝试查找地理编码范围内的所有点时,空间索引很慢。如何让它更快?

时间:2011-06-23 22:44:28

标签: tsql sql-server-2008 spatial-index

我在一张表上设置了一个空间索引,其中有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)

1 个答案:

答案 0 :(得分:5)

您可能需要使用索引提示(即WITH(INDEX([INDEX_NAME]))我认为2008 R2可能已经解决了这个问题。

Select top 100 
ci.Geocode.STDistance(@g), ci.CIOI  
from CustomerInformation WITH(INDEX(IX_CI_Geocode))
ci where ci.Geocode.Filter(@region) = 1 
order by ci.Geocode.STDistance(@g) asc