我有一张包含美国所有邮政编码的表格,我的查询是选择邮政编码边界坐标和每个邮政编码的单独计数。现在如果我指定某个邮政编码,这个操作非常快。问题是当我添加IN运算符时,我可以获得一定范围内的所有邮政编码。我使用此处显示的Great Circle函数http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81360来执行此操作。我现在认为的问题是从大圆函数返回的每个邮政编码都在运行整个操作。最终结果是谷歌地图,但这与改进SQL查询无关。如何修改此查询以提高性能?我已经索引了zipcodes。任何帮助将不胜感激!
select s.ZipCode, count(a.[Apartment_Complex]) apartCount, b.Longitude, b.Latitude from ZipCodeServiceAvailability s
left join pdx_apart_view a on s.ZipCode = left([Zip Code], 5)
left join ZipCodeBoundaries b on s.ZipCode = b.ZipCode
Where Ordering % 10 = 0 and s.zipcode in
(Select zipcode from ZipCodeServiceAvailability
Where AzumaWebInput.dbo.F_GREAT_CIRCLE_DISTANCE
((Select Latitude from ZipCodeServiceAvailability where ZipCode = '78745'), --latitude
(Select Longitude from ZipCodeServiceAvailability where ZipCode = '78745'), --longitude
Latitude,
Longitude) < 200) -- get all zips within 200 miles of 78745
Group By s.ZipCode, IsServiced, b.Longitude, b.Latitude, b.Ordering
Order by s.ZipCode, b.Ordering
答案 0 :(得分:3)
是的,您正在计算表格中每条记录的大圆距离。
更快的查询是将所有邮政编码放在两个纬度和两个经度之间的矩形中。
确保ZipCodeServiceAvailability表上有一个索引,同时包含纬度和经度字段。
编辑:
替换
(Select zipcode from ZipCodeServiceAvailability
Where AzumaWebInput.dbo.F_GREAT_CIRCLE_DISTANCE
((Select Latitude from ZipCodeServiceAvailability where ZipCode = '78745'), --latitude
(Select Longitude from ZipCodeServiceAvailability where ZipCode = '78745'), --longitude
Latitude,
Longitude) < 200)
与
(Select zipcode from ZipCodeServiceAvailability
Where Latitude between @Latitude - 100 and @Latitude + 100
and Longitude between @Longitude - 100 and @Longitude + 100)
答案 1 :(得分:1)
我认为你是对的。但是你为什么要使用子查询呢?子查询是说“在另一个位置的200(英里?)的邮政编码列表中原始表中的邮政编码是什么?”。
这与询问:“此邮政编码是否在200(英里?)范围内?”
你会说那个:
select s.ZipCode, count(a.[Apartment_Complex]) apartCount, b.Longitude, b.Latitude
from ZipCodeServiceAvailability s left join
pdx_apart_view a
on s.ZipCode = left([Zip Code], 5) left join
ZipCodeBoundaries b
on s.ZipCode = b.ZipCode
Where Ordering % 10 = 0 and
AzumaWebInput.dbo.F_GREAT_CIRCLE_DISTANCE((Select Latitude from ZipCodeServiceAvailability where ZipCode = '78745'), --latitude
(Select Longitude from ZipCodeServiceAvailability where ZipCode = '78745'), --longitude
Latitude,
Longitude
) < 200)
Group By s.ZipCode, IsServiced, b.Longitude, b.Latitude, b.Ordering
Order by s.ZipCode, b.Ordering