Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bulk inserts with SQLite and CoreData

I have a CoreData model that uses SQLite as persistence store. I need to insert large numbers of rows after doing some processing to each record. Is there any way to send those commands to SQLite

PRAGMA synchronous=OFF
PRAGMA count_changes=OFF
PRAGMA journal_mode=MEMORY
PRAGMA temp_store=MEMORY

I need to speed up the processing time, as it takes couple of hours to complete.

Any hints will be appreciated.

Thanks

like image 598
محمد الاشرف أنور Avatar asked Oct 27 '11 03:10

محمد الاشرف أنور


1 Answers

You can specify the pragmas when adding your store to the store coordinator:

NSMutableDictionary *pragmaOptions = [NSMutableDictionary dictionary];
[pragmaOptions setObject:@"OFF" forKey:@"synchronous"];
[pragmaOptions setObject:@"OFF" forKey:@"count_changes"];
[pragmaOptions setObject:@"MEMORY" forKey:@"journal_mode"];
[pragmaOptions setObject:@"MEMORY" forKey:@"temp_store"];
NSDictionary *storeOptions =
    [NSDictionary dictionaryWithObject:pragmaOptions forKey:NSSQLitePragmasOption];
NSPersistentStore *store;
NSError *error = nil;
store = [psc addPersistentStoreWithType:NSSQLiteStoreType
            configuration: nil
            URL:url
            options:storeOptions
            error:&error];

(Adapted from Persistent Store Features)

I strongly suggest to also read "Efficiently Importing Data".

Related documentation: NSSQLitePragmasOption Efficiently Importing Data

like image 73
Christian Kienle Avatar answered Oct 15 '22 04:10

Christian Kienle