O'Reilly logo

PostgreSQL 9 Administration Cookbook by Hannu Krosing, Simon Riggs

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Finding what makes SQL slow

A SQL statement can be slow for a lot of reasons. Following, we give a shortlist of these and at least one way for recognizing each of these.

Too much data is processed

Run the query with explain analyse to see how much data is processed for completing the query as follows:

mydb=# explain analyse select count(*) from t;
QUERY PLAN
---------------------------------------------------------------------- Aggregate (cost=4427.27..4427.28 rows=1 width=0) \
(actual time=32.953..32.954 rows=1 loops=1)
-> Seq Scan on t (cost=0.00..4425.01 rows=901 width=0) \
(actual time=30.350..31.646 rows=901 loops=1)
Total runtime: 33.028 ms
(3 rows)

and also see how many rows are processed, and how many blocks of data accessed by comparing ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required