我有一张包含美国所有邮政编码的表格,我的查询是选择邮政编码边界坐标和每个邮政编码的分开计数。现在,如果我指定某个邮政编码,则此操作非常快。问题是当我添加 IN 运算符时,我可以获得一定半径内的所有邮政编码。我使用此处显示的大圆函数http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=81360来执行此操作。我现在认为的问题是,对于从大圆圈函数返回的每个邮政编码,整个操作都在运行。最终结果是谷歌地图,但这与改进 sql 查询无关。应如何修改此查询以提高性能?我已经在索引邮政编码。任何帮助,将不胜感激!
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