Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to VACUUM a Core Data SQLite db?

By design, Core Data does not issue a VACUUM SQL command to its SQLite database(s), as detailed here. I'm creating a Core Data application that'll store, and later delete, large binary files (2-10MB in size) in a SQLite db. Over time this will lead to fragmentation and a larger-than-necessary SQLite database. I'd like to periodically issue a VACUUM command, say, during a cleanup operation I run.

  • How can I progmatically issue a VACUUM command to Core Data's SQLite stores?
  • Is it possible to do this through Core Data, or must I mount the SQLite db and connect to it directly to execute the VACUUM SQL?
like image 373
Dave Avatar asked Oct 07 '09 14:10

Dave


People also ask

How do you VACUUM SQLite?

The VACUUM command works by copying the contents of the database into a temporary database file and then overwriting the original with the contents of the temporary file. When overwriting the original, a rollback journal or write-ahead log WAL file is used just as it would be for any other database transaction.

Is Core Data equal to SQLite?

The most important difference between Core Data and SQLite is that SQLite is a database while Core Data is not. That is the most important difference because there is very little to compare. Core Data and SQLite are solutions to different problems.

Where is Core Data SQLite file?

Yes, there is always a SQLite behind it. You can find it in the documents folder. As with every new build a new documents folder is created on the simulator it's getting quite cumbersome to search it manually everytime. Create a function in your app and print it out.

Why Core Data is faster than SQLite?

Core Data is heavily optimized with regards to caching, lazy-loading and memory management. If you use it (with the SQLite store type), especially in conjunction with NSFetchedResultsController, you should get better performance than you could get with SQLite on your own.


2 Answers

The supported way to do this, on OS X 10.6 / iOS 3.0 and later, is to set the NSSQLiteManualVacuumOption in the options when you are adding the store to the persistent store coordinator.

like image 126
Jim Correia Avatar answered Oct 19 '22 18:10

Jim Correia


Yes, vacuum is a recognized SQL statement in SQLite. It can be used as a normal query, or so it says.

Beware, though, as it can lead to excessive file system reads and writes—the bottleneck of practically any system—not to mention server file system fragmentation on Windows servers.

like image 35
amphetamachine Avatar answered Oct 19 '22 19:10

amphetamachine