0

我有一张包含美国所有邮政编码的表格,我的查询是选择邮政编码边界坐标和每个邮政编码的分开计数。现在,如果我指定某个邮政编码,则此操作非常快。问题是当我添加 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
4

2 回答 2

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)
于 2013-02-28T17:13:31.633 回答
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
于 2013-02-28T18:26:19.987 回答