SQL Practice Logo

SQLPractice Online

Understanding EXPLAIN Plans: Functions

Module: Query Optimization & Performance

**PostgreSQL EXPLAIN Syntax:**

```sql

-- Basic EXPLAIN (shows plan only, doesn't execute)

EXPLAIN

SELECT * FROM orders WHERE customer_id = 123;

-- EXPLAIN ANALYZE (executes query and shows actual times)

EXPLAIN ANALYZE

SELECT * FROM orders WHERE customer_id = 123;

-- Detailed output with buffers and timing

EXPLAIN (ANALYZE, BUFFERS, VERBOSE, TIMING)

SELECT o.*, c.name

FROM orders o

JOIN customers c ON o.customer_id = c.customer_id

WHERE o.order_date >= '2024-01-01';

-- Output format options

EXPLAIN (FORMAT JSON) SELECT ...; -- JSON format

EXPLAIN (FORMAT YAML) SELECT ...; -- YAML format

EXPLAIN (FORMAT XML) SELECT ...; -- XML format

```

**MySQL EXPLAIN Syntax:**

```sql

-- Basic EXPLAIN

EXPLAIN

SELECT * FROM orders WHERE customer_id = 123;

-- Extended information

EXPLAIN EXTENDED

SELECT * FROM orders WHERE customer_id = 123;

SHOW WARNINGS; -- Shows optimizer notes

-- JSON format (more detailed)

EXPLAIN FORMAT=JSON

SELECT o.*, c.name

FROM orders o

JOIN customers c ON o.customer_id = c.customer_id

WHERE o.order_date >= '2024-01-01';

-- Analyze actual execution (MySQL 8.0+)

EXPLAIN ANALYZE

SELECT * FROM orders WHERE customer_id = 123;

```

**SQL Server Execution Plan:**

```sql