3. Everything you need to
know about making PG
perform well.
Tuesday, October 22, 13
4. Got a slow query?
Run an EXPLAIN ANALYZE:
startup time
EXPLAIN ANALYZE SELECT * FROM events WHERE app_info_id = 7559;
QUERY PLAN
------------------------------------------------------------------Seq Scan on events (cost=0.00..63749.03 rows=38 width=688) (actual time=2.538..660.785 rows=89 loops=1)
Filter: (app_info_id = 7559)
Total runtime: 660.885 ms
startup time
total time (from ANALYZE)
Sequence scan is BAD!
Credit to @craigkersteins for this example
Tuesday, October 22, 13
rows looked at
5. Need extra help?
• http://explain.depesz.com/s/Aa3
Tuesday, October 22, 13
6. Throw and index on
that sucka!
CREATE INDEX CONCURRENTLY idx_events_app_info_id ON events(app_info_id);
EXPLAIN ANALYZE SELECT * FROM events WHERE app_info_id = 7559;
---------------------------------------------------------------------Index Scan using idx_events_app_info_id on events (cost=0.00..23.40 rows=38 width=688) (actual
time=0.021..0.115 rows=89 loops=1)
Index Cond: (app_info_id = 7559)
Total runtime: 0.200 ms
660.885ms down to 0.200ms
Tuesday, October 22, 13
7. Need to add an index
on a massive table?
Use CREATE INDEX CONCURRENTLY to avoid locking
If you have MySQL just start crying.
Tuesday, October 22, 13
14. Only need to index
part of the data?
CREATE INDEX access_log_client_ip_ix ON access_log (client_ip)
WHERE NOT (client_ip > inet '192.168.100.0' AND client_ip < inet '192.168.100.255');
Tuesday, October 22, 13
15. Golden rules on PG
performance
• Use indexes when you see seq scan
• Keep index size in check
• Make sure you don’t have duplicate indexes
Tuesday, October 22, 13