SQLite is amazingly fast. A test table with 4.5 million records has that structure:
CREATE TABLE testtable (numericid INTEGER PRIMARY KEY, testtext TEXT);
It is filled with increasing values for numericid (0, 1, .... ) and a string for testtext.
Doing all the insert atomically took 1 hour 42 minutes on a MacBook pro (2009). The resulting SQLite file is 94 MB of size.
Inside the iOS app, the database is opened in the viewDidLoad method. A simple button triggered database query like this:
- (void)btnPressed:(UIButton *)sender{
NSLog(@"btn pressed, start");
sqlite3_stmt *statement = nil;
NSString *querystring;
querystring= [NSString stringWithFormat:@"SELECT * FROM testtable WHERE numericid = 2571312;"];
const char *sql = [querystring UTF8String];
NSLog(@"sql is: %s", sql);
if (sqlite3_prepare_v2(dbConnection, sql, -1, &statement, NULL)!=SQLITE_OK){
NSLog(@"sql problem occured with: %s", sql);
NSLog(@"%s", sqlite3_errmsg(dbConnection));
}
else
{
while (sqlite3_step(statement) == SQLITE_ROW) {
NSString *numericid = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 1)];
NSString *testtext = [NSString stringWithUTF8String:(char*)sqlite3_column_text(statement, 0)];
NSLog(@"%@",[NSString stringWithFormat:@"%@ (%@)", numericid, testtext]);
} // while
}
sqlite3_finalize(statement);
NSLog(@"btn pressed, finished");
}
results in an output of:
2012-08-10 17:51:36.734 DBQueryTest[28462:707] Database Successfully Opened
2012-08-10 17:51:39.083 DBQueryTest[28462:707] btn pressed, start
2012-08-10 17:51:39.087 DBQueryTest[28462:707] sql is: SELECT * FROM testtable WHERE numericid = 2571312;
2012-08-10 17:51:39.099 DBQueryTest[28462:707] text2571312 (2571312)
2012-08-10 17:51:39.102 DBQueryTest[28462:707] btn pressed, finished
So a query takes under 19ms! This could be reproduced for several values of numericid, though I didn't run a fully randomized test for statistical evaluation.
Conclusion: This test setup fulfills your requirements. SQLite is definitely a way to go.
UPDATE:
A quick random access test with 100000 key values verifies the first result. Leaving sql statement string creation and time-consuming NSLog outputs out of time measurement, the average database query time drops by an order of magnitude to:
average query time: 1.8 ms
mean deviation: 0.4 ms
maximum query time: 25.9 ms
minimum query time: 0.6 ms