Benchmark PostgreSQL Queries

SiddarthaSiddartha
3 min read

Let's break this down into two parts: using EXPLAIN ANALYZE for benchmarking PostgreSQL queries and understanding the /timing command.

  1. Using EXPLAIN ANALYZE for Benchmarking PostgreSQL Queries:

EXPLAIN ANALYZE is a powerful tool in PostgreSQL for understanding and optimizing query performance. Here's how to use it and what it provides:

a) Basic usage:
To use EXPLAIN ANALYZE, you simply prefix your query with the command:

EXPLAIN ANALYZE SELECT * FROM your_table WHERE condition;

b) What it does:

  • EXPLAIN shows the query plan that PostgreSQL will use to execute your query.

  • ANALYZE actually runs the query and provides timing information for each step of the plan.

c) Key information provided:

  • The query plan: Shows the sequence of operations PostgreSQL will perform.

  • Estimated vs. actual row counts: Helps identify where estimates might be off.

  • Timing information: Shows how long each step took to execute.

  • Cost estimates: Gives an idea of the relative expense of each operation.

d) Interpreting the output:
The output is a tree structure showing the query plan. Each node represents an operation, and includes:

  • Node type (e.g., Seq Scan, Index Scan)

  • Actual time: Time taken for this step (e.g., 0.123..0.456 ms)

  • Rows: Actual number of rows processed

  • Loops: Number of times this node was executed

e) Example output:

EXPLAIN ANALYZE SELECT * FROM employees WHERE department = 'Sales';

                                                           QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------
 Seq Scan on employees  (cost=0.00..23.75 rows=1000 width=32) (actual time=0.012..0.134 rows=100 loops=1)
   Filter: (department = 'Sales'::text)
   Rows Removed by Filter: 900
 Planning Time: 0.051 ms
 Execution Time: 0.147 ms
(5 rows)

In this example, we can see:

  • A sequential scan was used

  • It took 0.147 ms to execute

  • It processed 1000 rows, but only 100 matched the filter

f) Tips for using EXPLAIN ANALYZE:

  • Run it on a representative dataset

  • Use it to compare different query versions

  • Look for unexpected full table scans or high-cost operations

  • Check if estimated row counts are far off from actual counts

  1. Understanding and Using the /timing Command:

The /timing command in psql (PostgreSQL's command-line interface) is used to toggle the display of query execution time.

a) How to use it:

  • To turn on timing: \timing

  • To turn off timing: \timing again

b) What it does:
When enabled, /timing will display the total execution time of each query you run in psql, right after the query output.

c) Why it's useful:

  • Provides a quick way to see how long queries are taking

  • Helps identify slow queries without needing to use EXPLAIN ANALYZE

d) Example usage:

testdb=# \timing
Timing is on.
testdb=# SELECT * FROM employees WHERE department = 'Sales';
 id | name  | department | salary 
----+-------+------------+--------
  1 | John  | Sales      |  50000
  2 | Jane  | Sales      |  55000
  3 | Mike  | Sales      |  60000
(3 rows)

Time: 1.234 ms

e) /timing vs. EXPLAIN ANALYZE:

  • /timing gives you a quick overall execution time

  • EXPLAIN ANALYZE provides detailed information about the query plan and timing for each step

f) Addressing your concern about network latency:

  • /timing includes network latency in its measurement

  • EXPLAIN ANALYZE's timing information is for the server-side execution only

  • If you want to separate network latency, you might need to use client-side tools or run the database on the same machine as your client

In summary, use EXPLAIN ANALYZE for detailed query optimization and /timing for quick checks on query performance. Both are valuable tools in your PostgreSQL performance tuning toolkit.

0
Subscribe to my newsletter

Read articles from Siddartha directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Siddartha
Siddartha