问题描述
|
我已经在具有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)
解决方法
您可能需要使用索引提示(即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