{"slug": "duckdb-2-0-vs-polars-2-2-which-is-better-for-big-data", "title": "DuckDB 2.0 vs Polars 2.2: Which Is Better for Big Data?", "summary": "In a benchmark of 34 million 2024 NYC yellow taxi trips, DuckDB 2.0 finished a group-by and a join with less memory than Polars 2.2, needing 1 GB versus 1.5 GB for the group-by and 2 GB versus 2.5 GB for the join. With tight memory, DuckDB ran the group-by faster (0.46 s versus 0.76 s) while Polars ran the join faster (2.00 s versus 3.20 s). The test compared the two engines under memory limits that force spilling to disk, a capability Polars 2.x added for both group-bys and joins.", "body_md": "Table of Contents\n\nIntroduction\n\nWhen 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](https://codecut.ai/pandas-vs-polars-vs-duckdb-comparison/).\n\n[Polars 2.x](https://codecut.ai/polars-2-1-whats-new/) 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?\n\nTo 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.\n\n💻 Get the Code: The scripts are on [GitHub](https://github.com/khuyentran1401/codecut-blog/tree/main/duckdb-vs-polars-larger-than-ram).\n\n  Stay Current with CodeCut\n\n  Easy-to-digest articles on Python, AI, and open-source tools. Delivered twice a week.\n\n    .codecut-subscribe-wrap {\n\n        display: flex;\n\n        justify-content: center;\n\n    }\n\n    .codecut-subscribe-btn {\n\n        background: #72BEFA !important;\n\n        color: #2F2D2E !important;\n\n        border: none;\n\n        border-radius: 8px;\n\n        padding: 12px 28px;\n\n        font-family: inherit;\n\n        font-size: 16px;\n\n        font-weight: 700;\n\n        cursor: pointer;\n\n        text-decoration: none !important;\n\n        display: inline-flex;\n\n        align-items: center;\n\n        justify-content: center;\n\n        transition: background 0.3s ease;\n\n    }\n\n    .codecut-subscribe-btn:hover,\n\n    .codecut-subscribe-btn:focus {\n\n        background: #5aa8e8 !important;\n\n        color: #2F2D2E !important;\n\n        text-decoration: none !important;\n\n    }\n\n    /* Mobile responsive */\n\n    @media (max-width: 480px) {\n\n        .codecut-subscribe-btn {\n\n            width: 100%;\n\n            text-align: center;\n\n        }\n\n    }\n\nWhat Is Spilling to Disk?\n\nA group-by or join keeps a working table in memory while it runs. The bigger the data, the bigger that table gets.\n\nWhen that working table outgrows the memory limit, there’s nowhere to put the next row, so the process runs out of memory and stops.\n\nSpilling 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.\n\nwithout spilling                      with spilling\n\nmemory  [████████████████] full       memory  [██████     ]  part 1, now\n\n        -> out of memory, stops       disk    [░░░░] [░░░░]  parts 2 and 3, later\n\nWhile spilling to disk avoids running out of memory, it has some drawbacks:\n\nTime: writing to disk and reading back is slower than staying in memory.\n\nDisk space: on a very large query, the temporary files can fill your free disk.\n\nDuckDB 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.\n\nResults at a Glance\n\nMemory: DuckDB 2.0 needed less memory than Polars 2.2 to finish both queries.\n\nSpeed: With tight memory, DuckDB ran the group-by faster than Polars, but Polars ran the join faster than DuckDB.\n\nResult\n\nDuckDB 2.0\n\nPolars 2.2\n\nBetter\n\nMemory needed to finish the join\n\n2 GB\n\n2.5 GB\n\nDuckDB\n\nMemory needed to finish the group-by\n\n1 GB\n\n1.5 GB\n\nDuckDB\n\nJoin time with tight memory\n\n3.20 s\n\n2.00 s\n\nPolars\n\nGroup-by time with tight memory\n\n0.46 s\n\n0.76 s\n\nDuckDB\n\nGroup-By Performance\n\nThe data is 34 million [NYC yellow taxi trips](https://www.nyc.gov/site/tlc/about/tlc-trip-record-data.page) 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.\n\nIn [DuckDB](https://codecut.ai/deep-dive-into-duckdb-data-scientists/), any SQL query can spill to disk when it runs out of memory.\n\nimport duckdb\n\ncon = duckdb.connect()\n\nroutes_by_hour = con.sql(\"\"\"\n\n    SELECT PULocationID, DOLocationID,\n\n           date_trunc('hour', tpep_pickup_datetime) AS pickup_hour,\n\n           count(*) AS trips, avg(fare_amount) AS avg_fare\n\n    FROM '/data/yellow_tripdata_2024-*.parquet'\n\n    GROUP BY ALL\n\n\"\"\").to_arrow_table()  # columnar result, like Polars\n\nIn Polars, the query has to be [lazy](https://codecut.ai/eager-to-lazy-dataframes-with-narwhals/) (scan_parquet, then collect) to spill to disk.\n\nimport polars as pl\n\nmonths = [f\"/data/yellow_tripdata_2024-{m:02d}.parquet\" for m in range(1, 11)]\n\nroutes_by_hour = (\n\n    pl.scan_parquet(months)  # scan the Parquet files lazily\n\n    .group_by(\n\n        \"PULocationID\",\n\n        \"DOLocationID\",\n\n        pl.col(\"tpep_pickup_datetime\").dt.truncate(\"1h\").alias(\"pickup_hour\"),\n\n    )\n\n    .agg(pl.len().alias(\"trips\"), pl.col(\"fare_amount\").mean().alias(\"avg_fare\"))\n\n    .collect()  # run the query, spilling if needed\n\n)\n\nI 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](https://github.com/khuyentran1401/codecut-blog/tree/main/duckdb-vs-polars-larger-than-ram#setup) has the full setup. Here is how each one did as the memory shrank.\n\nAt 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.\n\nAt 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.\n\nSpeed barely changes with the limit. Both tools finish in under a second.\n\nTakeaway: For a large group-by, DuckDB is the safer choice when memory is tight.\n\nJoin Performance\n\nThe join matches two tables, rides and payments, on trip_id. Each has 100 million rows.\n\nTo join in DuckDB, use a SQL query with a JOIN clause.\n\ntips_by_zone = con.sql(\"\"\"\n\n    SELECT r.PULocationID, avg(p.tip_amount) AS avg_tip\n\n    FROM '/data/rides.parquet' r\n\n    JOIN '/data/payments.parquet' p USING (trip_id)\n\n    GROUP BY ALL\n\n\"\"\").to_arrow_table()\n\nTo join in Polars, call join on two lazy frames from scan_parquet.\n\nrides = pl.scan_parquet(\"/data/rides.parquet\")\n\npayments = pl.scan_parquet(\"/data/payments.parquet\")\n\ntips_by_zone = (\n\n    rides.join(payments, on=\"trip_id\")\n\n    .group_by(\"PULocationID\")\n\n    .agg(pl.col(\"tip_amount\").mean().alias(\"avg_tip\"))\n\n    .collect()\n\n)\n\nAt 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.\n\nA 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.\n\nMemory limit\n\nDuckDB spill\n\nPolars spill\n\n4 GB\n\n0 MB\n\n~300 MB\n\n3 GB\n\n~950 MB\n\n~360 MB\n\n2.5 GB\n\n~1,350 MB\n\n~420 MB\n\n2 GB\n\n~1,730 MB\n\nkilled\n\nBut 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.\n\nTakeaway: Polars is the faster choice for a large join when memory is tight, but DuckDB is the safer one.\n\nConclusion\n\nDuckDB 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.\n\nBut I think that speedup isn’t worth giving up DuckDB’s safety. If you work with large data on a small machine, use DuckDB.\n\nIf you already have a Polars pipeline and some memory to spare, you don’t need to [rewrite it in DuckDB](https://codecut.ai/ibis-vs-narwhals-vs-fugue-dataframe-portability/). Polars 2.2 can spill to disk now, and on large joins it can even be faster.\n\nReferences\n\n[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.\n\n[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.\n\n[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.\n\nRelated Tutorials\n\n[Scaling Pandas Workflows with PySpark’s Pandas API](https://codecut.ai/scaling-pandas-workflows-with-pysparks-pandas-api/): when data outgrows a single machine\n\n[Polars vs. Pandas](https://codecut.ai/polars-vs-pandas-a-fast-multi-core-alternative-for-dataframes/): why Polars is fast on a single machine\n\nThe 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).", "url": "https://wpnews.pro/news/duckdb-2-0-vs-polars-2-2-which-is-better-for-big-data", "canonical_source": "https://codecut.ai/duckdb-vs-polars-larger-than-ram/", "published_at": "2026-12-01 15:24:06+00:00", "updated_at": "2026-12-01 15:45:36.832722+00:00", "lang": "en", "topics": ["developer-tools"], "entities": ["DuckDB 2.0", "Polars 2.2", "DuckDB", "Polars", "NYC yellow taxi trips", "CodeCut"], "also_reported_by": [], "takeaway": "Under tight memory on 34 million NYC taxi trips, DuckDB 2.0 finished the group-by in 0.46 s versus Polars 2.2's 0.76 s and needed 1 GB versus 1.5 GB, but Polars ran the join in 2.00 s versus DuckDB's 3.20 s.", "recommendation": {"advice": "When queries must spill to disk, pick DuckDB 2.0 for group-bys and Polars 2.2 for joins, and budget 1 GB versus 1.5 GB of memory respectively for the group-by.", "playbook": "inference-serving", "playbook_url": "https://wpnews.pro/playbooks/inference-serving"}, "alternates": {"html": "https://wpnews.pro/news/duckdb-2-0-vs-polars-2-2-which-is-better-for-big-data", "markdown": "https://wpnews.pro/news/duckdb-2-0-vs-polars-2-2-which-is-better-for-big-data.md", "text": "https://wpnews.pro/news/duckdb-2-0-vs-polars-2-2-which-is-better-for-big-data.txt", "jsonld": "https://wpnews.pro/news/duckdb-2-0-vs-polars-2-2-which-is-better-for-big-data.jsonld"}}