Christopher Philip Hebert

Back to Home

Blog

Back to Blog
Previous Next

2025-01-07

When using DuckDB to create a table whose rows require memory-heavy aggregations, such as multiple windows or distinct counts, you may run out of memory and fail or (for supported operations, though it should always be avoided for performance) disk space to spill to.

You can address this by breaking the table creation into a series of smaller insertions.

CREATE TABLE one AS
SELECT * FROM source WHERE id % 3 = 0;

INSERT INTO one
SELECT * FROM source WHERE id % 3 = 1;

INSERT INTO one
SELECT * FROM source WHERE id % 3 = 2;

Break it into as many chunks as necessary to make the chunks small enough to operate entirely in memory without running out or spilling to disk.