Queries and performance

Read EXPLAIN ANALYZE

Compare estimated and actual work while remembering that the analyzed query really executes.

8 minute lesson

~~~

EXPLAIN shows the chosen plan. EXPLAIN ANALYZE runs the statement and adds actual rows and timing.

Use BUFFERS on a read to see cache and storage work:

EXPLAIN (ANALYZE, BUFFERS)
SELECT id, title
FROM app.notes
WHERE title = 'Plan the week';

Compare estimated rows with actual rows. Then find the largest scan, repeated loop, sort, or buffer count.

EXPLAIN ANALYZE executes writes too. Wrap a test write in a transaction and roll it back:

BEGIN;
EXPLAIN ANALYZE
UPDATE app.notes SET title = 'Plan Monday' WHERE id = 1;
ROLLBACK;

Do this on test data. Functions and sequence changes can have effects that a rollback does not undo.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →