0

我有两个表 response_archive 27 000 000 行,以及由 4 000 行组成的机场。

我试图选择的字段:

  • response_archive.min(价格)
  • response_archive.avg(价格)
  • response_archive.year_week <-(分组,索引)
  • response_archive.from(加入 airport.IATA)
  • response_archive.to(与 airport.IATA 一起)

  • response_archive.from 和 response_archive.to 不能相同。

  • response_archive.year_week 应该在一个范围内,如查询中所示。

我在分组时遇到问题,我无法按 response_archive.year_week 分组,同时获得最低/平均价格。

这是我当前的查询,它远非最佳,大约需要 90 秒才能执行。

SELECT
    `from`,
    `to`,
    year_week,
    min(price) min_price,
    round(AVG(price)) avg_price
  FROM response_archive
   INNER JOIN airports AS `f` ON (f.IATA = `from`)
   INNER JOIN airports AS `t` ON (t.IATA = `to`)
 WHERE
    year_week < '1310' AND
    year_week > '1210' AND
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80 AND
    f.SweCity = 'Stockholm' AND
    t.CountryCode = 'US'
GROUP BY year_week
ORDER BY year_week;

当前结果:

from   to     year_week min   avg
STO    NYC    1211      3552  6311
ARN    LAX    1212      3097  6496
STO    NYC    1213      3532  7379
ARN    NYC    1214      3584  6635
STO    LAX    1215      3523  5907
STO    FLL    1216      3559  5698
STO    NYC    1217      3642  5919

除了 min / avg 不正确。

比较第一个值是否正确:

SELECT
    min(price) min,
    '3532' min_expected,
    round(avg(price)) avg,
    '7379' avg_expected
FROM
    response_archive
WHERE
    `from` = 'STO' AND
    `to` = 'NYC' AND
    year_week = '1213' AND
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80;

正确结果:

min     min_exp avg     avg_exp
3532    3532    5955    7379

有人可以指出我的方向或解释为什么它没有给我想要的结果。

4

2 回答 2

0

这是您的简化查询,并添加了缺少的GROUP BY字段:

SELECT
    `from`,
    `to`,
    year_week,
    min(price) min_price,
    round(AVG(price)) avg_price
  FROM response_archive
   INNER JOIN airports AS `f` ON (f.IATA = `from`)
   INNER JOIN airports AS `t` ON (t.IATA = `to`)
 WHERE
    year_week < '1310' AND
    year_week > '1210' AND
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80 AND
    f.CountryCode = 'SE' AND
    t.CountryCode = 'US'
GROUP BY `from`, `to`, year_week
ORDER BY year_week;
于 2013-09-19T08:35:43.873 回答
0

您可以避免使用表 x 并将条件放在外部 where 中。尝试这个:

SELECT 
    `from`,
    `to`,
    year_week,
    min(price) min_price,
    round(AVG(price)) avg_price     
FROM
    response_archive AS r
        INNER JOIN airports AS `f` ON (f.IATA = `from`)
        INNER JOIN airports AS `t` ON (t.IATA = `to`),
WHERE
    r.year_week < '1310' AND
    r.year_week > '1210'        
    returntrip = 1 AND
    adults = 1 AND
    children = 0 AND
    `price` < 70000 AND
    `price` > 80 AND
    f.CountryCode = 'SE' AND
    t.CountryCode = 'US'
group by `from`,`to`, year_week
于 2013-09-19T08:36:36.483 回答