Execution Plan
Execution Plan
A description of how a query is executed. Viewed with the EXPLAIN command.
Execution Plan
A description of how a query is executed. Viewed with the EXPLAIN command.
An Execution Plan is a roadmap that shows how a database will process an SQL query. The Query Optimizer selects the method it judges to be the most efficient among several execution methods, and it can be viewed with the EXPLAIN command.
An Execution Plan includes information such as table access methods (Full Scan, Index Scan, etc.), Join methods (Nested Loop, Hash Join, Merge Join), sorting methods, and estimated cost and row counts. Analyzing it lets you find a query's bottlenecks and optimize it.
An Execution Plan can vary depending on data distribution, Index statistics, system resources, and so on. Therefore, it is important to test with data similar to the production environment, and it is advisable to check the actual execution statistics with the ANALYZE command.
-- MySQL EXPLAIN
EXPLAIN SELECT e.name, d.name as dept_name
FROM employees e
JOIN departments d ON e.department_id = d.id
WHERE e.salary > 5000000;
-- EXPLAIN ANALYZE (실제 실행 통계 포함)
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';
-- PostgreSQL 상세 실행 계획
EXPLAIN (ANALYZE, BUFFERS, FORMAT TEXT)
SELECT c.name, COUNT(o.id) as order_count
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name
HAVING COUNT(o.id) > 5;
-- 실행 계획 결과 예시 (MySQL)
/*
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
| 1 | SIMPLE | e | ALL | NULL | NULL | NULL | NULL | 1000 | Using where |
| 1 | SIMPLE | d | ref | PRIMARY | PRIMARY| 4 | db.e.dept| 1 | NULL |
+----+-------------+-------+------+---------------+------+---------+------+------+-------------+
type 컬럼 해석:
- ALL: Full Table Scan (최악)
- index: Full Index Scan
- range: Index Range Scan
- ref: Index 조회
- eq_ref: Unique Index 조회
- const: Primary Key 조회 (최적)
*/
-- 인덱스 힌트로 실행 계획 변경
SELECT /*+ INDEX(orders idx_order_date) */
order_id, customer_id
FROM orders
WHERE order_date > '2024-01-01';
Backend developer: This query suddenly got slow. It was working fine until yesterday.
DBA: Did you try running EXPLAIN ANALYZE? The Execution Plan may have changed.
Backend developer: It shows a Full Table Scan. But there's definitely an Index.
DBA: The statistics seem to be outdated. Run ANALYZE TABLE and check again.
Interviewer: What parts of the Execution Plan do you mainly check?
Candidate: First I look at the type column, and if it's ALL, that means a Full Scan, so I consider adding an Index. I check the estimated number of rows to be processed with the rows column, and if Extra shows Using filesort or Using temporary, I review optimizing the sort or the temporary table.
Interviewer: What about when the estimate and the actual differ?
Candidate: I check the actual execution statistics with EXPLAIN ANALYZE. If the statistics are outdated, I refresh them with ANALYZE, and use hints if necessary.
Take a mock interview on this topic → WIA SOOM AI Interview · Start for free
Senior: Did you check the Execution Plan of the main Queries before opening the PR?
Junior: The Execution Plan? I only looked at the Query results.
Senior: When data grows, performance problems can arise. Get into the habit of checking with EXPLAIN that there's no Full Scan and that the estimated rows are appropriate.