我有一张包含邮政编码,纬度和经度的表格([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
是个坏主意。
任何人都可以提出更好的主意。
答案 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功能
这可能会把时间缩短一半。
而且,你仍在对675亿个组合进行计算,所以它只需要一些时间。