如何使此查询返回Google BigQuery上的结果?我收到资源超出错误...数据集中有大约2B行。我正在尝试为每个user_id获取最多的艺术家ID。
select user_id, artist, count(*) as count
from [legacy20130831.merged_data] as d
group each by user_id, artist
order by user_id ASC, count DESC
答案 0 :(得分:6)
对公共数据的等效查询,会抛出相同的错误:
SELECT actor, repository_name, count(*) AS count
FROM [githubarchive:github.timeline] AS d
GROUP EACH BY actor, repository_name
ORDER BY actor, count desc
与同一查询进行比较,并对要返回的结果加上限制。这个有效(对我来说是14秒):
SELECT actor, repository_name, count(*) as count
FROM [githubarchive:github.timeline] as d
GROUP EACH BY actor, repository_name
ORDER BY actor, count desc
LIMIT 100
您可以使用一小部分user_ids,而不是使用LIMIT。就我而言,1/3有效:
SELECT actor, repository_name, count(*) as count
FROM [githubarchive:github.timeline] as d
WHERE ABS(HASH(actor) % 3) = 0
GROUP EACH BY actor, repository_name
但你真正想要的是“获得每个user_id最多的艺术家ID”。让我们走得更远,并得到它:
SELECT actor, repository_name, count FROM (
SELECT actor, repository_name, count, ROW_NUMBER() OVER (PARTITION BY actor ORDER BY count DESC) rank FROM (
SELECT actor, repository_name, count(*) as count
FROM [githubarchive:github.timeline] as d
WHERE ABS(HASH(actor) % 10) = 0
GROUP EACH BY actor, repository_name
))
WHERE rank=1
请注意,这次我使用了%10,因为它让我的结果更快。但是你可能想知道“我希望用一个查询得到我的结果,而不是10”。
你可以为此做两件事:
如果您愿意与我分享您的数据集,我可以提供特定于数据集的建议(很多取决于基数)。