我想在Distance_KM列中找到最大值。这是我的代码...
SELECT Source_Airport, Destination_Airport, Airline, Source_LAT, Source_LONG, Destination_LAT, Destination_LONG,
(111.045*DEGREES(ACOS(COS(RADIANS(Source_LAT)) * COS(RADIANS(Destination_LAT)) *
COS(RADIANS(Source_LONG) - RADIANS(Destination_LONG)) +
SIN(RADIANS(Source_LAT)) * SIN(RADIANS(Destination_LAT))))) as Distance_KM
FROM
(SELECT
routes.Source_Airport AS Source_Airport,
routes.Destination_Airport AS Destination_Airport,
routes.Airline AS Airline,
S.Latitude AS Source_LAT,
S.Longitude AS Source_LONG,
D.Latitude AS Destination_LAT,
D.Longitude AS Destination_LONG
FROM
ROUTES
INNER JOIN
airports AS S ON S.Airport_ID = routes.Source_Airport_ID
INNER JOIN
airports AS D ON D.Airport_ID = routes.Destination_Airport_ID) as Airport_Dist
哪个会生成一个像这样的表...
Distance_KM
YYZ YAM AC 43.67720032 -79.63059998 46.48500061 -84.50939941 493.34801024280597
YUL YBC AC 45.47060013 -73.74079895 49.13249969 -68.20439911 582.0897139062885
YYY YBC AC 48.60860062 -68.20809937 49.13249969 -68.20439911 58.1770000072475
YUL YBG AC 45.47060013 -73.74079895 48.33060074 -70.99639893 379.706058161593
YCS YBK 5T 63.34690094 -90.73110199 64.29889679 -96.07779694 282.3404661252172
YRT YBK 5T 62.81140137 -92.11579895 64.29889679 -96.07779694 256.1877034117866
YRT YBK 7F 62.81140137 -92.11579895 64.29889679 -96.07779694 256.1877034117866
YUT YBK 7F 66.52140045 -86.22470093 64.29889679 -96.07779694 517.0880293513636
YQQ YBL 8P 49.71080017 -124.887001 49.95080185 -125.2710037 38.299320607992
要在Distance_KM列中找到最大值,我想我只需要像这样用MAX()函数包装公式(产生列)...
MAX( )(111.045 * DEGREES(ACOS(COS(RADIANS(Source_LAT))* COS(RADIANS(Destination_LAT))* COS(RADIANS(Source_LONG)-RADIANS(Destination_LONG))+ SIN(RADIANS(Source_LAT))* SIN(RADIANS(Destination_LAT))))) ) 作为Distance_KM
我希望脚本返回最远距离的条目,但它会使服务器崩溃。
为什么这不起作用?
答案 0 :(得分:0)
很有可能它并没有真正使服务器崩溃-它“只是”让他忙了几个小时,几天或几个月。
这种计算距离的方法不能很好地扩展:它非常耗费计算资源。
您有几种选择:
将CPU与存储进行交易,并创建一个距离表,该距离表通过机场表上的ON AFTER UPDATE
,ON AFTER INSERT
和ON AFTER DELETE
触发器进行维护。这样,您就可以轻松获得距离,几千个机场只会给您几百万个可能的距离。由于机场表不经常更新,因此触发器很可能没有问题。
使用优化的算法:首先根据盒定理计算正交距离(简称差),以创建高度简化的候选表,然后在该表上运行MAX
。