Query Execution Plan

SQL · Extra Concepts

Theory

When you send a query, the database does not run it literally. A component called the optimizer considers different ways to execute it (scan the table or use an index, which table to read first, which join algorithm) and picks the one it estimates is cheapest, using statistics about table sizes and value distributions. The chosen strategy is the execution plan.

You can see it by putting EXPLAIN before the query in PostgreSQL and MySQL, EXPLAIN QUERY PLAN in SQLite, or viewing the estimated/actual plan in SQL Server. A plan is a tree of steps such as SCAN employees, SEARCH employees USING INDEX idx_dept (dept=?) or a hash join. PostgreSQL's EXPLAIN ANALYZE actually runs the query and shows real row counts and times next to the estimates. Reading plans is how you find out why a query is slow: a full scan on a huge table, a missing index, or a big gap between estimated and actual rows (stale statistics) are the usual suspects.

Why we use it

Two queries that look similar can run very differently. The execution plan shows exactly how the database will do the work, so you can find the slow step instead of guessing.

When to use it

Check the plan whenever a query is slow, after adding an index (to confirm it is used) and before running heavy queries in production. Use EXPLAIN ANALYZE carefully on UPDATE or DELETE, since it really executes them.

How it works

  1. Submit the SQL text
  2. Parser checks syntax and resolves table/column names
  3. Optimizer compares candidate plans using statistics
  4. Useful index for the filter? Yes: Plan SEARCH USING INDEX. No: Plan a full table SCAN.
  5. Executor runs the chosen plan
  6. EXPLAIN shows that plan without (or with ANALYZE, with) running it

Syntax

EXPLAIN QUERY PLAN
SELECT ...;

Example

EXPLAIN QUERY PLAN
SELECT * FROM employees WHERE department = 'IT';

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic