DuckDB Analytics
When to Use
- Run analytical SQL over local or partitioned Parquet, CSV, or JSON files without provisioning a database server.
- Combine DuckDB SQL queries with Polars or PyArrow tables via zero-copy Arrow exchange.
- Debug slow scans, out-of-memory errors, or thread-concurrency issues in embedded DuckDB pipelines.
Prerequisites
Identify the DuckDB version (duckdb.__version__ or SELECT version()), host language binding, available RAM and scratch disk, and input file layout (single files, glob patterns, or Hive-partitioned directories). Inspect how connections, cursors, and registered DataFrames are shared across threads, and confirm read-only vs write access before attaching persistent .duckdb files or writing Parquet output.
Verify partition and filter pushdown
- Pass explicit options such as
hive_partitioning = trueandunion_by_name = true(only when schema drift across files is expected) toread_parquet. Bind query paths and filter values with positional ($1) or named ($region) parameters instead of interpolating untrusted strings into SQL. - Run
EXPLAIN (FORMAT JSON)before executing expensive aggregations over large directory globs (avoid matching exact boxed ASCII line wrapping, which varies across DuckDB releases). Inspect theREAD_PARQUEToperator'sextra_infoto confirmFile FiltersandScanning Filesshow partition pruning and that row-level predicates appear asFiltersinsideREAD_PARQUETrather than only after a full scan. - Project only the columns required by the query. Avoid
SELECT *on wide Parquet datasets, since DuckDB's columnar reader skips unreferenced column chunks.
Exchange DataFrames cleanly and isolate cursors
- In Python DuckDB 2.x, use the Relation API (
con.sql(query, params=[...]).pl()or.to_arrow_table()) for zero-copy Polars/Arrow output rather than calling.to_arrow_table()on a DB-APIcon.execute(...)cursor, which makes an extra copy. - Do not share a single
DuckDBPyConnectionconcurrently across threads. Create an isolated worker cursor viacon.cursor()per thread. - Remember that
cursor.register("name", df)binds a Python DataFrame only on that cursor. Register DataFrames on the worker cursor executing the query, or materialize shared data into a DuckDB table when multiple cursors need it.
Bound memory, spill disk, and file writes
- Set explicit
SET memory_limit = '...',SET temp_directory = '...', andSET threads = Nin container or CI environments so DuckDB spills predictably instead of triggering the OS OOM killer. - Open persistent database files with
read_only = Truewhen only querying so multiple reader processes can attach safely. - When exporting partitioned datasets with
COPY (...) TO '...' (FORMAT PARQUET, PARTITION_BY (...)), write into a staging directory or checkOVERWRITE_OR_IGNORE/ALLOW_OVERWRITEexplicitly rather than mutating live output paths in place.
Example
Query a Hive-partitioned Parquet order dataset, prove DuckDB prunes unneeded partition files, and join Polars DataFrames across isolated cursors without extra Arrow copies.
Run the worked Parquet and Polars test to verify EXPLAIN file pruning (Scanning Files: 2/4), zero-warning sql(..., params=...).pl() output, and cursor-local registration isolation.
Limitations
- DuckDB is designed for OLAP queries and batch analytics, not high-concurrency OLTP row updates across multiple writer processes.
- Remote object-store scans (
httpfs/s3:///gs://) require extension loading and explicit credentials; test network and permission behavior in the target environment. - DuckDB 1.x and 2.x differ in some Python cursor/relation convenience methods (such as
.pl()on relations vs DB-API connections); check the installed version before writing bindings.
Safety and Side Effects
Inspect file globs and output paths before running COPY ... TO. Use read_only = True when inspecting existing .duckdb files, avoid loading unreviewed third-party community extensions, and never interpolate untrusted user input into SQL strings or file paths. Keep credentials out of query strings and logs.
Related Skills and Documentation
- For expression-based DataFrame pipelines in Python and Rust, see Polars.
- For general query-plan analysis and indexing fundamentals, see SQL Optimization Patterns.
- For labeled multi-dimensional scientific arrays, see Xarray Coordinate Alignment.
- See DuckDB's official documentation on Parquet files & Hive partitioning, Python Relational API, and Configuration & memory limits.