Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should one vacuum a database, and when analyze?

I just want to check that my understanding of these two things is correct. If it's relevant, I am using Postgres 9.4.

I believe that one should vacuum a database when looking to reclaim space from the filesystem, e.g. periodically after deleting tables or large numbers of rows.

I believe that one should analyse a database after creating new indexes, or (periodically) after adding or deleting large numbers of rows from a table, so that the query planner can make good calls.

Does that sound right?

like image 412
Richard Avatar asked Mar 15 '23 15:03

Richard


2 Answers

vacuum analyze; 

collects statistics and should be run as often as much data is dynamic (especially bulk inserts). It does not lock objects exclusive. It loads the system, but is worth of. It does not reduce the size of table, but marks scattered freed up place (Eg. deleted rows) for reuse.

vacuum full;

reorganises the table by creating a copy of it and switching to it. This vacuum requires additional space to run, but reclaims all not used space of the object. Therefore it requires exclusive lock on the object (other sessions shall wait it to complete). Should be run as often as data is changed (deletes, updates) and when you can afford others to wait.

Both are very important on dynamic database

like image 180
Vao Tsun Avatar answered Mar 21 '23 06:03

Vao Tsun


Correct.

I would add that you can change the value of the default_statistics_target parameter (default to 100) in the postgresql.conf file to a higher number, after which, you should restart your server and run analyze to obtain more accurate statistics.

like image 30
rzcs Avatar answered Mar 21 '23 04:03

rzcs