An execution plan is a roadmap that shows how the SQL database engine retrieves data to fulfill a query. Analyzing execution plans can help you understand and optimize query performance.
An execution plan describes the sequence of operations the database engine performs to execute a SQL query. It includes information about how tables are accessed, how joins are performed, and how data is filtered and sorted.
MySQL
Use the EXPLAIN statement.
EXPLAIN SELECT * FROM employees WHERE departmentid = 1;PostgreSQL
Use the EXPLAIN statement.
EXPLAIN SELECT * FROM employees WHERE departmentid = 1;SQL Server
Use the Display Estimated Execution Plan option in SQL Server Management Studio (SSMS) or SET SHOWPLAN.
SET SHOWPLAN_TEXT ON
GO
SELECT
*
FROM
employees
WHERE departmentid = 1;DuckDB
Use the EXPLAIN statement.
EXPLAIN SELECT * FROM employees WHERE departmentid = 1;- Full Table Scan: Scans the entire table to find matching rows.
- Index Scan: Scans the index to find matching rows.
- Index Seek: Directly accesses rows using the index.
- Nested Loop Join: For each row in the outer table, the database searches for matching rows in the inner table.
- Hash Join: Builds a hash table on one of the tables and probes it with rows from the other table.
- Merge Join: Sorts both tables on the join key and merges the results.
- Sort: Orders rows based on specified columns.
- Filter: Applies conditions to restrict the rows returned.
Simple Query
Analyze the execution plan for a simple query.
EXPLAIN SELECT * FROM employees WHERE departmentid = 1;Output: Shows a full table scan or index scan based on indexing.
Join Query
Analyze the execution plan for a join query.
EXPLAIN SELECT e.firstname, d.departmentname
FROM employees e
JOIN departments d ON e.departmentid = d.departmentid;Output: Shows how the join is performed (e.g., nested loop join, hash join).
Look for operations that consume a lot of resources, such as full table scans or expensive join operations.
Identify columns that could benefit from indexing to improve query performance.
Rewrite queries to use more efficient operations, such as replacing subqueries with joins.
- DuckDB CLI: Provides textual execution plans using the EXPLAIN statement.
- MySQL Workbench: Visualizes execution plans.
- PostgreSQL pgAdmin: Provides graphical representation of execution plans.
- SQL Server Management Studio (SSMS): Displays execution plans in a graphical format.
- Get the
execution plansfor at least3queries, look at how the query was executed and if there is anything you can implament to change the execution plans
Previous: Query Optermization | Next: Temporary Tables and CTEs