KevsRobots Learning Platform
77% Percent Complete
By Kevin McAleer, 3 Minutes
This lesson is a checklist of practical steps you can apply to most DuckDB analytics workloads.
Seeing new terms (threads, memory limit, statistics, EXPLAIN)? See the Beginner glossary.
PRAGMA threads = 8; -- use more CPU cores
PRAGMA memory_limit = '2GB'; -- cap memory usage
You try it
- Set threads to the number of cores you have; re-run a heavy aggregation and time it
CREATE OR REPLACE VIEW v_orders_clean AS
SELECT * FROM orders WHERE order_status = 'COMPLETE';
CREATE OR REPLACE TABLE orders_daily AS
SELECT order_date, SUM(amount) AS revenue
FROM v_orders_clean
GROUP BY order_date;
You try it
- Convert one reused view in your project into a materialized table and compare timings
DuckDB can collect column stats to guide the optimizer.
ANALYZE; -- whole database
-- or
ANALYZE my_table; -- a specific table
You try it
- Run
ANALYZEafter bulk loads; compare query plans before/after withEXPLAIN
EXPLAIN
SELECT c.category, SUM(o.amount) AS revenue
FROM orders o
JOIN customers c USING (customer_id)
WHERE o.order_date >= DATE '2024-01-01'
GROUP BY c.category;
How to read:
You try it
- Add an index-like hint via materialization: create a filtered table
orders_2024and re-checkEXPLAIN
Enable profiling per session, run a query, then inspect timings.
PRAGMA enable_profiling = json; -- or query_tree / query_profile
PRAGMA profiling_output = 'exports/profile.json';
SELECT c.category, SUM(o.amount) AS revenue
FROM orders o
JOIN customers c USING (customer_id)
WHERE o.order_date >= DATE '2024-01-01'
GROUP BY c.category;
Then open exports/profile.json in your editor. Look for:
Hash Join, Aggregate Hash)You try it
- Turn profiling off:
PRAGMA enable_profiling = none;- Change the filter to a tighter range and compare operator timings
You try it
- Rewrite a CSV dataset to Parquet and compare read time for a simple aggregation
See also: Troubleshooting DuckDB for quick fixes that impact performance (file formats, partitions, stats).
You can use the arrows ← → on your keyboard to navigate between lessons.
Comments