DuckDB – An in-process SQL OLAP database management system
duckdb.org
duckdb.org
I have successfully used DuckDB like above for preparing an ML dataset from about 100GB of input.
DuckDB is undergoing rapid development these days. There have been format-breaking changes and bugs that could lose data. I would not yet trust DuckDB for long-term storage or archival purposes. Parquet is a better choice for that.
How big can you go, and how does speed compare to Spark? (I'm guessing significantly faster from my experience using Duckdb on smaller machines)
Mixed experience, would definitely not put it in a system that isn’t ok crashing frequently.
It segfaults on Alpine (argh, C++!), and force exits the whole NodeJS process when it gets unexpected HTTP responses from S3.
In an archive run of 2M pushes it’ll crash the process 4-5 times.
Overall still really, really like it, but learned to not trust it
If I never have to write ETL pipelines again, I won't be upset about it.
Because it’s a single machine (no distributed cluster) DuckDB can heavily parallelize and vectorize. I don’t know if I can give you perf numbers but complex analytic queries over the entire dataset regularly finish in 1-2 mins (not scientific since I’m not telling what kinds of queries I’m running).
I’ve used Spark SQL and DuckDB overall is just more ergonomic, less boilerplate and is much faster since it is so lightweight.
Granted DuckDB can only process data on one machine (whereas Spark can scale up indefinitely by adding machines) but most data sets I work with fit on a single beefy machine.
Distributed computing — most of the time, you ain’t gonna need it.
It’s like StackOverflow: it serves 2B requests a month but only runs on a few on-prem servers. Most people think this is impossible but you can actually do a lot with very few machines if you’re smart about it. Same with data. Big data is overrated.
There was no cluster to set up or administer using DuckDB.
https://github.com/pola-rs/polars#handles-larger-than-ram-da...
import duckdb as db
import pandas as pd
df = pd.read_excel(“z.xlsx”)
df2 = db.query(“select * from df join ‘s3://bucket/a.parquet’ b on df.col b.col”).df()
df3 = df2.col.apply(lambda x: x)
DuckDB can refer any Pandas data frame in the namespace as a SQL object. You can query across Parquet, CSV and Pandas data frames seamlessly.Need to join Excel with Parquet with CSV? No problem. You can do it all within DuckDB.
DuckDB produces Pandas dataframes, so you would just do df.T. No need to choose between one the other.
But to answer your original question, the SQL analogue to a transpose are PIVOT/UNPIVOT operations which are mathematically rotation operations on invariants (your dimensions). This makes them much more general than a transpose -- which are just rotation operations on the rows/cols. PIVOT/UNPIVOT work on non-square data and allow you to specify different types of aggregations. PIVOT/UNPIVOT keywords are not yet implemented in DuckDB but are on the roadmap if I'm not mistaken.
Transforming into a pandas df isn't zero copy.
UNPIVOT and PIVOT are quite verbose compared to df.T.
In pandas it’s:
prices_df.loc['2020'] += 1
If you had a temperature forecast and you wanted to add the expected temperature miss to them, how would you do that on sql?In pandas it’s:
temps_df + expected_miss_dfFor example, this is how some basic operations would look in pandas.
Bump prices in 2020 up $1:
prices_df.loc['2020'] += 1
Add expected temperature offsets to base temperature forecast: temp_df + offset_df
Now imagine thousands of such operations, and you can see the necessity of pandas in models like this.If you have genuinely big and unstructured data then of course you need a cluster and would reach for Spark.
If you have smallish data then maybe DuckDB has a role because working with SQL is nicer than Pandas. But a lot of time you actually need the complexity of Pandas to do the transformation you need.
DuckDB is neat but I still can’t quite convince myself of a killer use case.
Really? I prefer working with dataframe apis. You get a nice sql-like paradigm plus all the control structures of the runtime.
Also Pandas methods are imperative so cannot be optimized. SQL is declarative so it can be optimized to the hilt and DuckDB is faster than Pandas in almost all cases, even on Pandas data frames themselves! (partly due to vectorization).
If you have a few billion records and you want to filter, join, aggregate them using SQL then DBT against an OLAP server solves that issue so well that it doesn’t leave much white space for DuckDB.
I mentioned modern data stack because when you have SaaS, low code, consumption based billing, open source etc then it treads even more on the DuckDB value prop. DuckDB would have been great if Oracle was my only choice, but when I have Snowflake and Clickhouse in the toolbox it is a tougher market for them to carve out a niche.
Ninja edit before anyone misconstrues this. I am not saying that the typical desktop has these specs. I am saying that the class of hardware that is most commonly run on desktops includes SKUs that can meet this spec. Desktop-class means the same motherboard socket and processor architecture.
https://pedram.substack.com/p/streaming-data-pipelines-with-...
Last year I was working on something using SQLite, users could perform analytical queries that would scan the entire 3gb db and generate aggregates. It would take at least 45 seconds to do the queries.
I did a dump of the db and imported to DuckDB. The same queries now only take 1.5 seconds with exactly the same SQL.
Obviously there is a trade off, inserts are slower on DuckDB. But for a low write, analytical read app it's perfect.
I tried to use the SQLite connector but struggled to get it working. Need to circle back and have another go.
With their SQLite and Postgres connectors, as a Django dev I would love an app that lets you run specific queries on your DB via DuckDB almost transparently. Would be awesome for analytical dashboards.
Looking at how it's deployed, as an in process database, how do people actually use this in production? Trying to figure out where I might actually want to think about replacing current databases or analyses with DuckDB.
EG if you deployed new code
1. Do you have a stateful machine you're doing an old school "Kill the old process, start the new process" deploy, and there's some duckdb file on disk that is maintained?
2. Or do you back that duckdb file in some sort of shared disk (Eg EBS), and have a rolling deploy where multiple applications access the same DB at the same time?
3. Or is DuckDB is treated as ephemeral, and you're using it to process data on the fly, so persisted state isn't an issue?
BTW, your questions are exactly those that I've been ask over the last few months, but also with a lot of focus over the last few days. Still learning as much as I can so the following might not be true.
For what it's worth, there's a difference between using duckdb to query a set of files vs loading a bunch of files in to a table. But once the data has been loaded into a table it can be backed up as a duckdb db file.
Therefore it might be more performant to preprocess duckdb db files (perhaps a process that works in conjunction with whatever manages your external tables) and load these db files into duckdb as needed (on the fly analysis) instead of loading datafiles into duckdb, transforming and CTAS every time.
https://duckdb.org/docs/sql/statements/attach
Of course all of this might be introducing more latency esp if you're trying to do NRT analytics.
I assume you could partition your data into multiple db files similar to how you would probably do it with your data files (managing external tables).
https://www.arecadata.com/getting-started-with-iceberg-using...
You would still need to interact with some kind of catalogue to understand which .db files you need to fetch.
And honestly I don't really know or understand the performance implications of the attach command.
I'm excited to see if the duckdb team will be able to integrate with external tables directly one day. (not that data files would be .db files)
Imagine this:
1) you have an external managed external table (iceberg, delta, etc... managed by Glue, databricks, etc)
2) register this table in duckdb
CREATE OR REPLACE EXTERNAL TABLE my_table ... TYPE = 'ICEBERG' CATALOG = 's3://...' CREDENTIALS = '...' etc
3) simply interact with table in duckdb as you would any other tablePodcast/interview link for anyone interested: https://www.dataengineeringpodcast.com/duckdb-in-process-ola...
Interesting since in some ways, as he points out, it's in direct competition with DataFrames for use cases, but he gives it a very positive treatment and shows how they can work together using advantages of standard SQL along with processing power of DataFrames.
The approach was never as fast as data.table but did approach the speed of dplyr for more complex queries.
Life had other things in store for me and I haven’t touched this library for a while now.
At the time there was no Julia connector for duckdb, but now that there is, I’d like to try this approach in that language.
There are so many infrastructure products, especially database products for some reason, where the marketing team takes control of the messaging away from engineers, and push outlandish claims on how their new DB is faster than all the competition, can support any workload, can scale infinitely, etc.
We at MotherDuck at working very closely with the DuckDB folks to build a DuckDB-based cloud service. I'm talking to various folks in the industry about the details in 1:1. Feel free to reach out to tino at motherduck.com.
(co-founder and head of Produck at MotherDuck)
Arguably Postgres was never the right tool to use for this analysis but nonetheless I was surprised at how much faster DuckDB was.
(i'm "only" working with 30 million rows though)
You can't make project choices in a vacuum, and you can't assume others can either. People have limited time. The choice they were facing was probably not C++ vs Rust, but C++ vs nothing because they didn't have time to learn a new language before starting their project.
Also, their first release was in 2019, so they probably heard of it, but that's around the beginning of its recent spike in popularity. It's starting to be viewed as a good long term option, but back then a lot of people were still wondering if it was a fad.
I'm learning Rust, and I'm a big fan, but this is a bad take.
It can be done and it’s not that hard.
I think the choice of C++ for an in-process DB that is going to be very popular makes the entire industry less secure. If Chrome, one of the largest budget C++ code bases, still has memory bugs then there is no way DuckDB won’t.
https://github.com/marcboeker/go-duckdb/issues
And a third-party effort, only:
Otherwise check out postgres scanner. https://github.com/duckdblabs/postgres_scanner
They have a blog entry about it too: https://motherduck.com/blog/duckdb-ecosystem-newsletter-two/
It would be also interesting to see how we can process semi-structured data in a similar way.
I've written a lot of OLAP queries (wrote a materialization layer for MonetDb and Postgres years ago). I find Pandas so much easier to work with for semi complicated work.
From an ergonomics perspective, I find pandas much harder to use casually than SQL. When I was an IC and was using it a lot, I was proefficient in it. But now that I code maybe 5 hours / month at work, I can't really do anything non trivial besides basic stuff/pivots. OTOH, I never really forget SQL.
* When to not use DuckDB: Multiple concurrent processes reading from a single writable database*
So no concurrent reads? Or is it no concurrent reads while writing?
Here's clickhouse's SQL support: https://clickhouse.com/docs/en/sql-reference/
Compare this to DuckDB's (on the sidebar): https://duckdb.org/docs/sql/introduction
DuckDB's SQL coverage is much more complete and matches my experience with full blown databases like Postgres and Redshift.
As well, performance-wise DuckDB is currently still somewhat faster than clickhouse-local [1] but I would say this is a secondary consideration -- as long as either is "fast enough for your purposes" this shouldn't be an issue -- and clickhouse is plenty fast.
The primary consideration for me would be the SQL support. That said, if you don't use any complex SQL, clickhouse-local seems like it would be a worthy contender.
[1] https://benchmark.clickhouse.com/#eyJzeXN0ZW0iOnsiQXRoZW5hIC...
Yeah, I think there was some advanced psql stuff that I wanted to do with duckdb but wasn't able, so I can imagine that with clickhouse-local it would be even worse. Not that duckdb isn't enough for me.
Now I need to make my mind about duckdb vs nushell. Both are greats. Well I can use both, maybe.
Thank you very much!
The SQL dialect is so much lacking that it seems intentional. They are meant to be a transactional database, not an analytics one.
Sqlite also takes pride on stability (deployed on a billion android devices). Adding 100+ analytics capabilities e.g. functions is not gonna be easy in terms of maintaining stability.
I want to be wrong though because my paid app (superintendent.app) uses Sqlite. Not supporting analytics well is the number one complaint.
The key thing to consider here is trade-offs.
Analytical databases tend to be optimized for analytical queries at the expense of fast atomic read-write transactions.
SQLite is mainly used in situations where fast atomic read-write transactions are key - that's why it's used in so many mobile phone applications, for example.
It's not going to grow analytical-query-at-scale capabilities if that means negatively impacting the stuff it's really good at already.
OLAP cube is microsoft's take on OLAP using 90s technologies for tech stacks from 1990s (Windows Server + SQL Server + SSAS).
DuckDB is a modern take on OLAP
MDX == OLAP
SQL == relational
Motherduck Raises $47.5M for DuckDB - https://news.ycombinator.com/item?id=33610218 - Nov 2022 (2 comments)
Modern Data Stack in a Box with DuckDB - https://news.ycombinator.com/item?id=33191938 - Oct 2022 (5 comments)
Querying Postgres Tables Directly from DuckDB - https://news.ycombinator.com/item?id=33035803 - Sept 2022 (38 comments)
Notes on the SQLite DuckDB Paper - https://news.ycombinator.com/item?id=32684424 - Sept 2022 (28 comments)
Show HN: CSVFiddle – Query CSV files with DuckDB in the browser - https://news.ycombinator.com/item?id=31946039 - July 2022 (13 comments)
Show HN: Easily Convert WARC (Web Archive) into Parquet, Then Query with DuckDB - https://news.ycombinator.com/item?id=31867179 - June 2022 (15 comments)
Range joins in DuckDB - https://news.ycombinator.com/item?id=31530639 - May 2022 (24 comments)
Friendlier SQL with DuckDB - https://news.ycombinator.com/item?id=31355050 - May 2022 (133 comments)
Fast analysis with DuckDB and Pyarrow - https://news.ycombinator.com/item?id=31217782 - April 2022 (53 comments)
Directly running DuckDB queries on data stored in SQLite files - https://news.ycombinator.com/item?id=30801575 - March 2022 (23 comments)
Parallel Grouped Aggregation in DuckDB - https://news.ycombinator.com/item?id=30589250 - March 2022 (10 comments)
DuckDB quacks Arrow: A zero-copy data integration between Arrow and DuckDB - https://news.ycombinator.com/item?id=29433941 - Dec 2021 (13 comments)
DuckDB-Wasm: Efficient analytical SQL in the browser - https://news.ycombinator.com/item?id=29039235 - Oct 2021 (58 comments)
Comparing SQLite, DuckDB and Arrow with UN trade data - https://news.ycombinator.com/item?id=29010103 - Oct 2021 (79 comments)
DuckDB is the better SQLite with APIs for Java/Python/R and it's got potential - https://news.ycombinator.com/item?id=28692997 - Sept 2021 (2 comments)
Fastest table sort in the West – Redesigning DuckDB's sort - https://news.ycombinator.com/item?id=28328657 - Aug 2021 (27 comments)
Querying Parquet with Precision Using DuckDB - https://news.ycombinator.com/item?id=27634840 - June 2021 (32 comments)
DuckDB now has a Node.js API - https://news.ycombinator.com/item?id=25289574 - Dec 2020 (4 comments)
DuckDB – An embeddable SQL database like SQLite, but supports Postgres features - https://news.ycombinator.com/item?id=24531085 - Sept 2020 (160 comments)
DuckDB: SQLite for Analytics - https://news.ycombinator.com/item?id=23287278 - May 2020 (67 comments)