Table of Contents
Introduction
When a group-by or join needs more memory than you have, your query might fail if the engine can’t spill to disk. DuckDB has been able to spill for years, which gave it an advantage over Polars when memory is tight, as covered in pandas vs Polars vs DuckDB.
Polars 2.x now does it too, for both group-bys and joins. That means both tools can run a group-by or a join on data bigger than the memory they have, but which one performs better at different memory sizes?
To find out, I ran the same group-by and the same join in DuckDB 2.0 and Polars 2.2 under different memory limits and compared the results. By the end, you’ll know which tool to reach for, depending on your memory and your query.
💻 Get the Code: The scripts are on GitHub. Stay Current with CodeCut
Easy-to-digest articles on Python, AI, and open-source tools. Delivered twice a week.
.codecut-subscribe-wrap {
display: flex;
justify-content: center;
}
.codecut-subscribe-btn {
background: #72BEFA !important;
color: #2F2D2E !important;
border: none;
border-radius: 8px;
padding: 12px 28px;
font-family: inherit;
font-size: 16px;
font-weight: 700;
cursor: pointer;
text-decoration: none !important;
display: inline-flex;
align-items: center;
justify-content: center;
transition: background 0.3s ease;
}
.codecut-subscribe-btn:hover,
.codecut-subscribe-btn:focus {
background: #5aa8e8 !important;
color: #2F2D2E !important;
text-decoration: none !important;
}
/* Mobile responsive */
@media (max-width: 480px) {
.codecut-subscribe-btn {
width: 100%;
text-align: center;
}
}
What Is Spilling to Disk?
A group-by or join keeps a working table in memory while it runs. The bigger the data, the bigger that table gets.
When that working table outgrows the memory limit, there’s nowhere to put the next row, so the process runs out of memory and stops.
Spilling to disk avoids that. The engine splits the work into parts, keeps the parts that fit in memory, and writes the rest to temporary files to process one at a time later.
without spilling with spilling
memory [████████████████] full memory [██████ ] part 1, now
-> out of memory, stops disk [░░░░] [░░░░] parts 2 and 3, later
While spilling to disk avoids running out of memory, it has some drawbacks:
Time: writing to disk and reading back is slower than staying in memory.
Disk space: on a very large query, the temporary files can fill your free disk.
DuckDB and Polars can both spill to disk, but that doesn’t mean they take the same time or use the same memory. The rest of this article tests which one performs better at different memory limits.
Results at a Glance
Memory: DuckDB 2.0 needed less memory than Polars 2.2 to finish both queries.
Speed: With tight memory, DuckDB ran the group-by faster than Polars, but Polars ran the join faster than DuckDB.
Result
DuckDB 2.0
Polars 2.2
Better
Memory needed to finish the join
2 GB
2.5 GB
DuckDB
Memory needed to finish the group-by
1 GB
1.5 GB
DuckDB
Join time with tight memory
3.20 s
2.00 s
Polars
Group-by time with tight memory
0.46 s
0.76 s
DuckDB
Group-By Performance
The data is 34 million NYC yellow taxi trips from 2024, in 10 monthly files. The group-by counts trips and averages the fare per pickup zone, dropoff zone and hour. That’s 12.4 million groups, so the result alone takes a lot of memory.
In [DuckDB](https://codecut.ai/deep-dive-into-duckdb-data-scientists/), any SQL query can spill to disk when it runs out of memory.
import duckdb
con = duckdb.connect()
routes_by_hour = con.sql("""
SELECT PULocationID, DOLocationID,
date_trunc('hour', tpep_pickup_datetime) AS pickup_hour,
count(*) AS trips, avg(fare_amount) AS avg_fare
FROM '/data/yellow_tripdata_2024-*.parquet'
GROUP BY ALL
""").to_arrow_table() # columnar result, like Polars
In Polars, the query has to be [lazy](https://codecut.ai/eager-to-lazy-dataframes-with-narwhals/) (scan_parquet, then collect) to spill to disk.
import polars as pl
months = [f"/data/yellow_tripdata_2024-{m:02d}.parquet" for m in range(1, 11)]
routes_by_hour = (
pl.scan_parquet(months) # scan the Parquet files lazily
.group_by(
"PULocationID",
"DOLocationID",
pl.col("tpep_pickup_datetime").dt.truncate("1h").alias("pickup_hour"),
)
.agg(pl.len().alias("trips"), pl.col("fare_amount").mean().alias("avg_fare"))
.collect() # run the query, spilling if needed
)
I ran each query at least 3 times in a Docker container at memory limits from 6 GB down to 1 GB, and the charts show the median. The speed gaps held in every run, not just the median. The example README has the full setup. Here is how each one did as the memory shrank.
At 6 and 4 GB, both tools have room to spare, so their peak memory barely changes. Polars uses about 1 GB more than DuckDB, or about 75% more.
At 1.5 GB, Polars starts spilling to disk to stay under the limit, while DuckDB still fits in memory. At 1 GB, DuckDB spills about 80 MB and finishes, while Polars is killed every time before it writes anything to disk.
Speed barely changes with the limit. Both tools finish in under a second.
Takeaway: For a large group-by, DuckDB is the safer choice when memory is tight.
Join Performance
The join matches two tables, rides and payments, on trip_id. Each has 100 million rows.
To join in DuckDB, use a SQL query with a JOIN clause.
tips_by_zone = con.sql("""
SELECT r.PULocationID, avg(p.tip_amount) AS avg_tip
FROM '/data/rides.parquet' r
JOIN '/data/payments.parquet' p USING (trip_id)
GROUP BY ALL
""").to_arrow_table()
To join in Polars, call join on two lazy frames from scan_parquet.
rides = pl.scan_parquet("/data/rides.parquet")
payments = pl.scan_parquet("/data/payments.parquet")
tips_by_zone = (
rides.join(payments, on="trip_id")
.group_by("PULocationID")
.agg(pl.col("tip_amount").mean().alias("avg_tip"))
.collect()
)
At 6 and 4 GB, the two tools are close in speed, at about 2 seconds each. Below that, DuckDB starts spilling to disk and slows down as memory shrinks, while Polars’ speed stays about the same.
A likely reason is how much each tool writes to disk. Polars spills a little at every limit, while DuckDB writes more and more as memory shrinks.
Memory limit
DuckDB spill
Polars spill
4 GB
0 MB
~300 MB
3 GB
~950 MB
~360 MB
2.5 GB
~1,350 MB
~420 MB
2 GB
~1,730 MB
killed
But DuckDB uses about 200 MB less memory when memory is tight. That’s why it finishes at 2 GB, while Polars is killed every time. At 1 GB, neither tool finishes the join.
Takeaway: Polars is the faster choice for a large join when memory is tight, but DuckDB is the safer one.
Conclusion
DuckDB 2.0 needs less memory than Polars 2.2 to finish both queries. Polars 2.2 can be faster on a large join when memory is tight.
But I think that speedup isn’t worth giving up DuckDB’s safety. If you work with large data on a small machine, use DuckDB.
If you already have a Polars pipeline and some memory to spare, you don’t need to rewrite it in DuckDB. Polars 2.2 can spill to disk now, and on large joins it can even be faster. References
[Polars 2.0 release post](https://pola.rs/posts/release-polars-2) (Polars, 2026): spilling to disk starts at about 80% of RAM, with a 64 GB default disk budget.
[Announcing DuckDB 2.0.0](https://duckdb.org/2026/10/21/announcing-duckdb-200) (DuckDB, 2026): the 2.0 release date and its long-term support window.
[Memory Management in DuckDB](https://duckdb.org/2024/07/09/memory-management) (DuckDB, 2024): how DuckDB’s buffer manager keeps a query within its memory limit.
Related Tutorials
[Scaling Pandas Workflows with PySpark’s Pandas API](https://codecut.ai/scaling-pandas-workflows-with-pysparks-pandas-api/): when data outgrows a single machine
[Polars vs. Pandas](https://codecut.ai/polars-vs-pandas-a-fast-multi-core-alternative-for-dataframes/): why Polars is fast on a single machine
The post [DuckDB 2.0 vs Polars 2.2: Which Is Better for Big Data?](https://codecut.ai/duckdb-vs-polars-larger-than-ram/) appeared first on [CodeCut](https://codecut.ai).