Visualizing query execution plans
Understand how DuckDB executes your queries
SQL Workbench provides a powerful feature to visualize how DuckDB executes your SQL queries using interactive Mermaid flowcharts. This helps you understand the query execution flow, identify performance bottlenecks, and optimize your queries for better performance.
How to use
To visualize the execution plan for a query:
- Write your SQL query in the editor area
- Position your cursor anywhere within the SQL statement you want to analyze
-
Press the keyboard shortcut:
Cmd + Shift + Eon macOSCtrl + Shift + Eon Windows/Linux
Note: The explain plan feature is keyboard-only. You need to place your cursor within a SQL statement and use the keyboard shortcut to activate it.
Understanding the visualization
The execution plan is displayed as an interactive flowchart using Mermaid diagrams. The graph shows:
- Execution nodes: Each box represents an operation in the query execution pipeline (e.g., table scans, joins, filters, projections)
- Data flow: Arrows show how data flows from one operation to another
- Operation details: Each node displays the type of operation and relevant parameters
Example
Here's an example query that you can visualize:
-- Create sample data
CREATE TABLE employees AS
SELECT * FROM (VALUES
(1, 'Alice', 'Engineering', 95000),
(2, 'Bob', 'Sales', 75000),
(3, 'Charlie', 'Engineering', 85000),
(4, 'Diana', 'Sales', 80000)
) AS t(id, name, department, salary);
-- Query with JOIN and aggregation
SELECT
department,
COUNT(*) as employee_count,
AVG(salary) as avg_salary
FROM employees
WHERE salary > 70000
GROUP BY department
ORDER BY avg_salary DESC;
Position your cursor on the
SELECT statement and press Cmd + Shift + E (or Ctrl + Shift + E) to see its execution plan.
Use cases
- Query optimization: Identify which parts of your query are most expensive
- Understanding joins: See how DuckDB processes different types of joins
- Learning SQL: Visual feedback helps understand how SQL queries are executed
- Debugging: Understand why a query might be slow or not returning expected results
Tip: Execution plans are not saved to your query history, allowing you to analyze queries without cluttering your history.