按半径过滤时的SQL地理性能问题

时间:2015-05-19 15:55:44

标签: sql sql-server tsql

我有这个简单的SQL表,其中有10,000,000条记录:

create table locationtests
(
    Id int not null identity(1,1) primary key,
    Localization geography not null
);

使用此功能从一些最小和最大纬度和经度开始随机计算和插入记录:

DECLARE @counter BIGINT;
SELECT @counter = COUNT(*) from locationtests;
print(@counter)
WHILE(@counter != 1000000)
BEGIN
    DECLARE @latitude varchar(MAX); 
    SET @latitude = (RAND() * (47.66202 - 28.39222)) + 28.39222 /*latitude*/
    DECLARE @longitude varchar(MAX);
    SET @longitude = (RAND() * (-122.14082 - -80.60771)) + -80.60771 /*longitude*/
    DECLARE @stringToInsert varchar(MAX);
    SET @stringToInsert = 'POINT('+ @longitude + ' ' + @latitude +')';
    INSERT INTO [locationtests] VALUES (geography::STGeomFromText(@stringToInsert,4326), @stringToInsert);
    SET @counter += 1;
END
GO

请注意,此表在“位置”列上有空间索引。

但是,当我使用此查询进行半径选择(搜索)时:

DECLARE @Origin GEOGRAPHY, @Distance INTEGER = 400000, @Points Varchar(MAX) = 'POINT(-122.14082' +' '+ '47.66202)';
SET @Origin = geography::STGeomFromText(@Points,4326)
select Localization FROM locationtests where @Origin.STDistance(Localization) <= @Distance;

还有执行计划:

i

老实说,我不知道在这个查询中如何优化和优化... 你有什么想法吗?

0 个答案:

没有答案