1

我在玩 bigquery 并遇到了一个问题,我的查询是:

SELECT * FROM (
SELECT a.title,  a.counter , MAX(b.num_characters) as max
FROM (
  SELECT title, count(*) as counter FROM publicdata:samples.wikipedia
  GROUP EACH BY title
  ORDER BY counter DESC
  LIMIT 10
) a JOIN
(SELECT title,num_characters FROM publicdata:samples.wikipedia
) b ON a.title = b.title
GROUP BY a.title, a.counter)
LIMIT 1;

虽然这是有效的,但我得到的响应太大而无法返回。第一个子查询运行良好,我想做的是获得更多的列。但我失败了。

4

1 回答 1

2

不要担心“限制 1”,在到达那个阶段之前响应会变得太大。

尝试跳过第二个子查询,因为它只从大型数据集中选择 2 列,而不过滤它。一个可行的替代方案是:

SELECT
  a.title, a.counter, MAX(b.num_characters) AS max
FROM
  publicdata:samples.wikipedia b JOIN(
  SELECT
    title, COUNT(*) AS counter
  FROM
    publicdata:samples.wikipedia
    GROUP EACH BY title
  ORDER BY
    counter DESC
  LIMIT 10) a
  ON a.title = b.title
GROUP BY
  a.title,
  a.counter

这在 15.4 秒内运行。

我们可以使用 TOP() 更快地做到这一点:

SELECT
  a.title title, counter, MAX(num_characters) max
FROM
  publicdata:samples.wikipedia b
JOIN
  (
  SELECT
    TOP(title, 10) AS title, COUNT(*) AS counter
  FROM
    publicdata:samples.wikipedia
    ) a
  ON a.title=b.title
GROUP BY
  title, counter

TOP() 的作用更简单、更快(SELECT COUNT(*)/GROUP/LIMIT)。

https://developers.google.com/bigquery/docs/query-reference#top-function

现在它只运行 6.5 秒,处理 15.9 GB。

于 2013-05-09T05:50:19.170 回答