在16km范围内查找邮政编码

时间:2014-07-18 13:32:14

标签: sql tsql sql-server-2012 geo geography

我有一张包含邮政编码,纬度和经度的表格([dbo].[LUPostCode])。 (27K记录)。 我有另一个表([dbo].[LUPostCodeMaster]),一个具有相同结构的主表,有(250万条记录)。

我需要找出来自[dbo].[LUPostCodeMaster]的所有邮政编码 16Km(或)远离[dbo].[LUPostCode]

中的邮政编码

我正在尝试

SELECT  C.skMasterPostCode PostCode,
        M.skMasterPostCode AdjPostCode
INTO    WorkTable
FROM    [dbo].[LUPostCode] C,
        (
        SELECT [skMasterPostCode],[Latitude],[Longitude] 
        FROM    [dbo].[LUPostCodeMaster]
        ) M --Expected rows --> 70,771,012,410
WHERE   CAST('POINT('+C.Latitude+ ' '+C.Longitude+')' AS GEOGRAPHY).STDistance(CAST('POINT('+M.Latitude+ ' '+M.Longitude+')' AS GEOGRAPHY)) <= 16000

花了这么多时间..我知道CROSS JOIN 27K with 2.5 Million是个坏主意。

任何人都可以提出更好的主意。

1 个答案:

答案 0 :(得分:2)

-- DROP TABLE #mytemptable 

SELECT 
 C.UID  AS uidC
,M.UID AS uidM
,CAST('POINT(' + C.Latitude + ' ' + C.Longitude + ')' AS GEOGRAPHY)
.STDistance(CAST('POINT(' + M.Latitude+ ' ' + M.Longitude+')' AS GEOGRAPHY))
 AS distance 
INTO #mytemptable 
FROM LUPostCode AS C
CROSS JOIN LUPostCodeMaster AS M 


DELETE FROM #mytemptable WHERE distance > 16000    


SELECT * FROM #mytemptable 

LEFT JOIN LUPostCode AS C
    ON C.uid = uidC
LEFT JOIN LUPostCodeMaster AS M 
    ON M.uid = uidM

在我的400万条入境样本表上工作15秒钟。

嗯,根据
http://aboutsqlserver.com/2013/07/22/clr-vs-t-sql-performance-considerations/
你仍然可以将它移动到CLR功能 这可能会把时间缩短一半。

CLR Speed

而且,你仍在对675亿个组合进行计算,所以它只需要一些时间。