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

StackOverflow https://stackoverflow.com/questions/12903575

Question

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?

Était-ce utile?

La 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.

Autres conseils

What OS is your server hosted on? In Linux, the file system uses a RAM cache for files. When a new file needs to be opened, if there is room, it is stored in the cache. If there is no room, the oldest/last-accessed file is removed to make room for the new file. PostgreSQL stores relations (tables and indexes) as files. When they are first read, the Linux file system loads them into memory. This makes accessing the data much faster. But the overhead of that initial read is the "slow" that you are experiencing. Subsequent reads of the data are directly from RAM - unless the cache swaps.

From my experience, you should monitor the size of your tables and indexes. If they get too large, a single query from one such "monster table" can cause all of the other commonly used files to swap out of cache, and the entire system will suffer. Keep you tables smaller (try partitioning if you get in the range of a million rows), and don't index on highly precise fields (like timestamps) - index on a date instead - you will have fewer index nodes and therefore a smaller index file (assuming you have many records for each day). This will allow the OS to load your relations in and out of cache much more efficiently, and you will not notice the speed difference so much.

If you are hosted on Windows, there may be no explaining your performance issues. You should seriously read PostgreSQL's documentation about performance if you need convincing to move to a *nix server.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top