Example 1
Inspect the plan for a filtered lookup
The command does not return user rows. It returns metadata about how the engine expects to access and filter the users table.
Engine-specific syntax
Setup
CREATE TABLE users (id INT, email VARCHAR(100), status VARCHAR(20));
INSERT INTO
users
VALUES
(1, 'a@example.com', 'active'),
(2, 'b@example.com', 'disabled');SQL
EXPLAIN
SELECT
id,
email
FROM
users
WHERE
status = 'active';| id | select_type | table | partitions | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|---|---|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | users | NULL | ALL | NULL | NULL | NULL | NULL | 2 | 50 | Using where |
Engine-specific syntax
Setup
CREATE TABLE users (id INT, email VARCHAR(100), status VARCHAR(20));
INSERT INTO
users
VALUES
(1, 'a@example.com', 'active'),
(2, 'b@example.com', 'disabled');SQL
EXPLAIN
SELECT
id,
email
FROM
users
WHERE
status = 'active';| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|---|---|---|---|---|---|---|---|---|---|
| 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 2 | Using where |
Engine-specific syntax
Setup
CREATE TABLE users (id INT, email VARCHAR(100), status VARCHAR(20));
INSERT INTO
users
VALUES
(1, 'a@example.com', 'active'),
(2, 'b@example.com', 'disabled');SQL
SELECT
'SET SHOWPLAN_TEXT ON; SELECT id, email FROM users WHERE status = ''active''; SET SHOWPLAN_TEXT OFF;' AS plan_instruction;| plan_instruction |
|---|
| SET SHOWPLAN_TEXT ON; SELECT id, email FROM users WHERE status = 'active'; SET SHOWPLAN_TEXT OFF; |
Engine-specific syntax
Setup
CREATE TABLE users (id INT, email VARCHAR(100), status VARCHAR(20));
INSERT INTO
users
VALUES
(1, 'a@example.com', 'active'),
(2, 'b@example.com', 'disabled');SQL
EXPLAIN
SELECT
id,
email
FROM
users
WHERE
status = 'active';| QUERY PLAN |
|---|
| Seq Scan on users (cost=0.00..13.25 rows=1 width=222) |
| Filter: ((status)::text = 'active'::text) |
Engine-specific syntax
Setup
CREATE TABLE users (id INT, email TEXT, status TEXT);
INSERT INTO
users
VALUES
(1, 'a@example.com', 'active'),
(2, 'b@example.com', 'disabled');SQL
EXPLAIN QUERY PLAN
SELECT
id,
email
FROM
users
WHERE
status = 'active';| id | parent | notused | detail |
|---|---|---|---|
| 2 | 0 | 216 | SCAN users |
Plan output is engine-specific and may vary by version and statistics.