I have a query of the below form, running on a large table (~60GB). I figure the WITH OFFSET is some sort of ORDER BY under the hood? Any ideas how to heal this problem are welcome.
I get the following error: Job xxx failed with error RESOURCE_EXHAUSTED: Resources exceeded during query execution: The query could not be executed in the allotted memory. Peak usage: 112% of limit. Top memory consumer(s): ORDER BY operations: 98% other/unattributed: 2% ; JobID: xxx
WITH
test_data AS (
SELECT
CAST(CURRENT_TIMESTAMP() as DATETIME) datetime,
'xyz' AS grp,
'["7f9f98fh9g4ef393d3h5", "chg3g33f26949hg6067d", "g477e5973ec04g7c3232", "0de1ec83304d761he786", "3c1h1f153530g90g35c2", "946637g145h48322686f"]' AS ids
UNION ALL
SELECT
CAST(CURRENT_TIMESTAMP() as DATETIME) datetime,
'abc' AS grp,
'["7f9f98fh9g4ef393d3h5", "chg3g33fdsfsdfs49hg6067d", "g477e5973ec04g7c3232", "0de1ec83304d761he786", "3c1h1f153530g90g35c2", "946637g145h48322686f"]' AS ids ),
as_list AS (
SELECT
datetime,
grp,
id,
pos
FROM
test_data, UNNEST(SPLIT(REGEXP_REPLACE(JSON_EXTRACT(ids,'$'), r'[\[\]\"]', ''), ',')) AS id WITH OFFSET AS pos)
SELECT
*
FROM
as_list
ORDER BY
grp, datetime