Search code examples
ruby-on-railspostgresqlpostgresql-performance

Why PostgreSQL queries are slower in the first request after server start than during the subsequent requests?


I'm using PostgreSQL 9.1.1 and Rails 3.2.8. Using the development mode of NewRelic I've noticed that several SQL queries takes much longer during the first request following my server start or restart than during the subsequent requests.

Is there any reason for that, is that due to prepared statements?


Solution

  • Just after start none of the indexes is loaded in memory, so the server will have to do a lot of very slow disk reads. As activity progresses more and more index pages get loaded in memory, and consulting these pages is of course a whole lot faster.