Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I run VACUUM in transaction or after?

I have a mobile application sync process. The transaction does a lot of modification on the database. Since this is done on mobile I need to issue a VACUUM to compact the database.

I am wondering when should I issue a VACUUM

  • in the transaction, as final statement
  • or after the transaction?

I am currently looking for SQLite, but if it's different for other engines, let me know in the answers (PostgreSQL, MySQL, Oracle, SQLServer)

like image 563
Pentium10 Avatar asked Feb 12 '10 08:02

Pentium10


2 Answers

I would say outside of the transaction. Certainly in PostgreSQL, VACUUM is designed to remove the "dead" tuples (i.e. the old row when a record has been changed or deleted.)

If you're running VACUUM in a transaction that has modified records, these dead rows won't have been marked for deletion.

Depending on which type of VACUUM you're doing, it may also require a table lock which will block if there are other transactions running, so you could potentially end up in a deadlock situation (transaction 1 is blocked waiting for a table lock to do its VACUUM, transaction 2 gets blocked waiting for a row to be released that transaction 1 has locked.)

I'd also recommend that this isn't done in an application (perhaps as a scheduled task) as it can take a while to complete and can negatively affect speed of other queries.

As for SQL Server, there is no VACUUM - what you're looking for is shrink. You can turn on auto shrink in 2005 which will automatically reclaim space when it the server decides, or issue a DBCC statement to shrink the database and log file, but this depends on your backup routine and strategy on a per-database level.

like image 187
Andy Shellam Avatar answered Sep 27 '22 23:09

Andy Shellam


Want it or not when using PostgreSQL you can't run VACUUM in transaction as stated in the manual:

VACUUM cannot be executed inside a transaction block.

like image 21
Milen A. Radev Avatar answered Sep 27 '22 21:09

Milen A. Radev