DuckDB
DuckDB
An in-process analytics database. SQLite for OLAP.
DuckDB
An in-process analytics database. SQLite for OLAP.
DuckDB is an in-process analytical database known as "SQLite for OLAP." It can be imported as a library in Python, R, Node.js, and other environments and used right away, without installing a separate server, and it can quickly analyze tens of GB of data in a local environment.
DuckDB's core strengths are columnar storage and vectorized query execution. With a columnar approach optimized for analytical queries, it reads only the columns it needs, and with vectorized execution that uses SIMD (Single Instruction Multiple Data) instructions, it uses the CPU efficiently. As a result, aggregation operations tens of times faster than Pandas are possible.
It can directly query a variety of file formats. CSV, Parquet, and JSON files can be queried directly with SQL without loading them into memory. Files in cloud storage such as S3 and GCS are also supported. For example, it is used like SELECT * FROM read_parquet('s3://bucket/data.parquet').
Integration with Pandas and Polars is also excellent. You can register a DataFrame as a DuckDB table, or return DuckDB query results as a DataFrame. When exploring data in Jupyter Notebook, you can move freely between SQL and Python, which makes for high productivity.
import duckdb
import pandas as pd
# 1. ์ธ๋ฉ๋ชจ๋ฆฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ (๊ธฐ๋ณธ)
con = duckdb.connect()
# 2. CSV ํ์ผ ์ง์ ์ฟผ๋ฆฌ (๋ก๋ ์์ด)
result = con.execute("""
SELECT
category,
COUNT(*) as count,
AVG(price) as avg_price,
SUM(quantity) as total_qty
FROM read_csv_auto('sales_data.csv')
GROUP BY category
ORDER BY total_qty DESC
LIMIT 10
""").fetchdf()
print(result)
# 3. Parquet ํ์ผ ์ฟผ๋ฆฌ (S3 ์ง์ ์ ๊ทผ)
con.execute("INSTALL httpfs; LOAD httpfs;")
con.execute("SET s3_region='ap-northeast-2';")
remote_result = con.execute("""
SELECT * FROM read_parquet('s3://my-bucket/data/*.parquet')
WHERE event_date >= '2024-01-01'
""").fetchdf()
# 4. Pandas DataFrame๊ณผ ํตํฉ
df = pd.DataFrame({
'user_id': ['u1', 'u2', 'u3', 'u1', 'u2'],
'product': ['A', 'B', 'A', 'C', 'A'],
'amount': [100, 200, 150, 300, 250]
})
# DataFrame์ DuckDB์์ ์ง์ ์ฟผ๋ฆฌ
user_summary = con.execute("""
SELECT
user_id,
COUNT(DISTINCT product) as product_count,
SUM(amount) as total_amount
FROM df
GROUP BY user_id
""").fetchdf()
# 5. ์๊ตฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ํ์ผ ์ฌ์ฉ
con_persistent = duckdb.connect('analytics.duckdb')
con_persistent.execute("""
CREATE TABLE IF NOT EXISTS events (
event_id INTEGER,
event_type VARCHAR,
user_id VARCHAR,
timestamp TIMESTAMP
)
""")
# 6. ๋์ฉ๋ ํ์ผ ํจ์จ์ ์ฒ๋ฆฌ (ํํฐ์
๋ณ ๋ณ๋ ฌ ์ฝ๊ธฐ)
con.execute("""
COPY (
SELECT * FROM read_parquet('data/year=*/month=*/*.parquet',
hive_partitioning=true)
WHERE year = 2024
) TO 'output.parquet' (FORMAT PARQUET)
""")
# 7. Window ํจ์ ํ์ฉ
con.execute("""
SELECT
user_id,
event_date,
daily_amount,
SUM(daily_amount) OVER (
PARTITION BY user_id
ORDER BY event_date
ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW
) as cumulative_amount
FROM daily_summary
""")
print("DuckDB ๋ถ์ ์๋ฃ!")
con.close()
Analyst: "I need to analyze a 20GB Parquet file, but when I read it with Pandas, I run out of memory."
Data Engineer: "Try DuckDB. If you query it directly with read_parquet, it reads only the columns it needs, so it uses less memory. I was able to query even a 60GB file on my laptop."
Analyst: "Since it works with SQL, aggregation should be easier than with Pandas too. I'll try it right away."
Interviewer: "Do you have experience analyzing large-scale data in a local environment?"
Candidate: "I used DuckDB. I queried tens of GB of Parquet files directly on my laptop without a server. With Pandas I got memory errors, but with DuckDB it was possible thanks to columnar reading and streaming processing. After complex exploratory analysis, I moved only the necessary parts up to a Spark cluster."
Take a mock interview on this topic โ WIA SOOM AI Interview ยท Start for free
Reviewer: "I think this Pandas groupby logic would be faster and easier to read if you converted it to DuckDB SQL."
Author: "You're right, this multi-aggregation part in particular was complicated in Pandas, and it would be cleaner in SQL. I'll make the change."