簡體   English   中英

在大表上使用 ROW_NUMBER() 時出現 Google Bigquery Memory 錯誤 - 用短唯一標識符替換長 hash 的方法

[英]Google Bigquery Memory error when using ROW_NUMBER() on large table - ways to replace long hash by short unique identifier

對於 google BigQuery 中的查詢,我想用一個較短的數字唯一標識符替換一個長的 hash,以便之后保存一些 memory,所以我這樣做:

SELECT 
    my_hash
    , ROW_NUMBER() OVER (ORDER BY null) AS id_numeric
FROM hash_table_raw
GROUP BY my_hash

我什至不需要 id 中的訂單,但ROW_NUMBER()需要一個ORDER BY

當我在我的數據集(> 10 億行)上嘗試此操作時,出現 memory 錯誤:

400 Resources exceeded during query execution: The query could not be executed in the allotted memory. Peak usage: 126% of limit.
Top memory consumer(s):
  sort operations used for analytic OVER() clauses: 99%
  other/unattributed: 1%

還有另一種方法可以用更短的標識符替換 hash 嗎?

謝謝!

執行此操作時實際上不需要填充 over 子句。

例如以下將起作用:

select col, row_number() over() as row_num from (select 'A' as col)

所以這將是你的第一次嘗試。

現在,你有十億多行:如果上面失敗了:你可以做這樣的事情(考慮到順序對你來說一點都不重要):但在這里你必須分部分做:

SELECT 
    my_hash
    , ROW_NUMBER() OVER () AS id_numeric
FROM hash_table_raw
where MOD(my_hash, 5) = 0

在后續查詢中:您可以從上一次運行中獲取 max(id_numeric) 並將其作為偏移量添加到下一次:

SELECT 
    my_hash
    , previous_max_id_numberic_val + ROW_NUMBER() OVER () AS id_numeric
FROM hash_table_raw
where MOD(my_hash, 5) = 1

並繼續將這些 mod 查詢 (0-4) 的輸出附加到一個新表中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM