I would like to benchmark some SQL-queries agains my PostgreSQL database. Is there any way I can time SQL-queries using psql?
Asked
Active
Viewed 2.3e+01k times
355
Jonas
- 33,945
- 27
- 62
- 64
3 Answers
80
Timing can be turned on with \timing at the psql prompt (as Caleb already said).
If you are on 8.4 or above, you can add an optional on/off argument to \timing, which can be helpful if you want to be able to set timing on in .psqlrc - you can then set \timing on explicitly in a script where plain \timing would otherwise toggle it off
Jack Douglas
- 40,517
- 16
- 106
- 178
35
The time that \timing returns also includes the network latency, if you're connecting to a remote server.
When you don't want that and don't need the query output too, better use EXPLAIN ANALYZE, which outputs the query plan with the planner estimates plus the actual execution times.
for example, EXPLAIN ANALYZE SELECT foo from bar ;
Devi
- 511
- 5
- 5