这几天研究了一下SQLite这个嵌入式数据库在多线程环境下的应用,感觉里面的学问还挺多,于是就在此分享一下。
先说下初衷吧,实际上我经常看到有人抱怨SQLite不支持多线程。而在iOS开发时,为了不阻塞主线程,数据库访问必须移到子线程中。为了解决这个矛盾,很有必要对此一探究竟。
关于这个问题,最权威的解答当然是SQLite官网上的“Is SQLite threadsafe?”这个问答。
简单来说,从3.3.1版本开始,它就是线程安全的了。而iOS的SQLite版本没有低于这个版本的:
3.4.0 - iPhone OS 2.2.1当然,你也可以自己编译最新版本。 只是我发现自己编译出来的3.7.8居然比iOS 4.3.3内置的3.7.2慢了一半,不知道苹果做了什么优化。 发现是我编译成了debug版本,改成release后性能比内置版本高5%左右,不过构建出来的app会大420k左右。
3.6.12 - iPhone OS 3.0 / 3.1
3.6.22 - iPhone OS 4.0
3.6.23.2 - iPhone OS 4.1 / 4.2
3.7.2 - iPhone OS 4.3
3.7.7 - iPhone OS 5.0
# -*- coding: utf-8 -*-
import sqlite3
import threading
def f():
con.rollback()
con = sqlite3.connect('test.db', check_same_thread=False) # 允许在其他线程中使用这个连接
cu = con.cursor()
cu.execute('CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY)')
print cu.execute('SELECT count(*) FROM test').fetchone()[0] # 0
cu.execute('INSERT INTO test VALUES (NULL)')
print cu.execute('SELECT count(*) FROM test').fetchone()[0] # 1
thread = threading.Thread(target=f)
thread.start()
thread.join()
print cu.execute('SELECT count(*) FROM test').fetchone()[0] # 0
cu.close()
con.close()
在这个例子中,虽然是在子线程中执行rollback,但由于和主线程用的是同一个数据库连接,所以主线程所做的更改也被回滚了。
连接1:BEGIN (UNLOCKED)现在2个连接都在等待对方释放锁,于是就死锁了。当然,实际情况并没那么糟糕,任何一方选择不继续等待,回滚事务就行了。
连接1:SELECT ... (SHARED)
连接1:INSERT ... (RESERVED)
连接2:BEGIN (UNLOCKED)
连接2:SELECT ... (SHARED)
连接1:COMMIT (PENDING,尝试获取EXCLUSIVE锁,但还有SHARED锁未释放,返回SQLITE_BUSY)
连接2:INSERT ... (尝试获取RESERVED锁,但已有PENDING锁未释放,返回SQLITE_BUSY)
连接1:BEGIN IMMEDIATE (RESERVED)这样死锁就被避免了。
连接1:SELECT ... (RESERVED)
连接1:INSERT ... (RESERVED)
连接2:BEGIN IMMEDIATE (尝试获取RESERVED锁,但已有RESERVED锁未释放,因此事务开始失败,返回SQLITE_BUSY,等待用户重试)
连接1:COMMIT (EXCLUSIVE,写入完成后释放)
连接2:BEGIN IMMEDIATE (RESERVED)
连接2:SELECT ... (RESERVED)
连接2:INSERT ... (RESERVED)
连接2:COMMIT (EXCLUSIVE,写入完成后释放)
连接1:BEGIN EXCLUSIVE (EXCLUSIVE)不过在并非很高的情况下,直接获取EXCLUSIVE锁的难度比较大;而且为了避免EXCLUSIVE状态长期阻塞其他请求,最好的方式还是让所有写事务都以IMMEDIATE方式开始。
连接1:SELECT ... (EXCLUSIVE)
连接1:INSERT ... (EXCLUSIVE)
连接2:BEGIN (UNLOCKED)
连接2:SELECT ... (尝试获取SHARED锁,但已有EXCLUSIVE锁未释放,返回SQLITE_BUSY,等待用户重试)
连接1:COMMIT (EXCLUSIVE,写入完成后释放)
连接2:SELECT ... (SHARED)
连接2:INSERT ... (RESERVED)
连接2:COMMIT (EXCLUSIVE,写入完成后释放)
#import <sqlite3.h>
static char dbPath[200];
static sqlite3 *database;
static sqlite3 *openDb() {
if (sqlite3_open(dbPath, &database) != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"Failed to open database: %s", sqlite3_errmsg(database));
}
return database;
}
- (void)viewDidLoad {
[super viewDidLoad];
sqlite3_config(SQLITE_CONFIG_SINGLETHREAD);
NSLog(@"%d", sqlite3_threadsafe());
NSLog(@"%s", sqlite3_libversion());
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
strcpy(dbPath, [[documentsDirectory stringByAppendingPathComponent:@"data.sqlite3"] UTF8String]);
database = openDb();
char *errorMsg;
if (sqlite3_exec(database, "CREATE TABLE IF NOT EXISTS test (id INTEGER PRIMARY KEY AUTOINCREMENT, value INTEGER);", NULL, NULL, &errorMsg) != SQLITE_OK) {
NSLog(@"Failed to create table: %s", errorMsg);
}
}
static void insertData() {
char *errorMsg;
if (sqlite3_exec(database, "BEGIN TRANSACTION", NULL, NULL, &errorMsg) != SQLITE_OK) {
NSLog(@"Failed to begin transaction: %s", errorMsg);
}
static const char *insert = "INSERT INTO test VALUES (NULL, ?);";
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, insert, -1, &stmt, NULL) == SQLITE_OK) {
for (int i = 0; i < 1000; ++i) {
sqlite3_bind_int(stmt, 1, arc4random());
if (sqlite3_step(stmt) != SQLITE_DONE) {
--i;
NSLog(@"Error inserting table: %s", sqlite3_errmsg(database));
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
}
if (sqlite3_exec(database, "COMMIT TRANSACTION", NULL, NULL, &errorMsg) != SQLITE_OK) {
NSLog(@"Failed to commit transaction: %s", errorMsg);
}
static const char *query = "SELECT count(*) FROM test;";
if (sqlite3_prepare_v2(database, query, -1, &stmt, NULL) == SQLITE_OK) {
if (sqlite3_step(stmt) == SQLITE_ROW) {
NSLog(@"Table size: %d", sqlite3_column_int(stmt, 0));
} else {
NSLog(@"Failed to read table: %s", sqlite3_errmsg(database));
}
sqlite3_finalize(stmt);
}
}
static dispatch_queue_t queue;
- (void)viewDidLoad {
// ...
queue = dispatch_queue_create("net.keakon.db", NULL);
}
static int lastReadCount = 0;
static int readCount = 0;
static int lastWriteCount = 0;
static int writeCount = 0;
- (void)count {
int lastRead = lastReadCount;
int lastWrite = lastWriteCount;
lastReadCount = readCount;
lastWriteCount = writeCount;
NSLog(@"%d, %d", lastReadCount - lastRead, lastWriteCount - lastWrite);
}
- (void)viewDidLoad {
// ...
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(count) userInfo:nil repeats:YES];
}
static void readData() {
static const char *query = "SELECT value FROM test WHERE value < ? ORDER BY value DESC LIMIT 1;";
void (^ __block readBlock)() = Block_copy(^{
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, query, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, arc4random());
int returnCode = sqlite3_step(stmt);
if (returnCode == SQLITE_ROW || returnCode == SQLITE_DONE) {
++readCount;
}
sqlite3_finalize(stmt);
} else {
NSLog(@"Failed to prepare statement: %s", sqlite3_errmsg(database));
}
dispatch_async(queue, readBlock);
});
dispatch_async(queue, readBlock);
}
static void writeData() {
static const char *update = "UPDATE test SET value = ? WHERE id = ?;";
void (^ __block writeBlock)() = Block_copy(^{
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update, -1, &stmt, NULL) == SQLITE_OK) {
sqlite3_bind_int(stmt, 1, arc4random());
sqlite3_bind_int(stmt, 2, arc4random() % 1000 + 1);
if (sqlite3_step(stmt) == SQLITE_DONE) {
++writeCount;
}
sqlite3_finalize(stmt);
} else {
NSLog(@"Failed to prepare statement: %s", sqlite3_errmsg(database));
}
dispatch_async(queue, writeBlock);
});
dispatch_async(queue, writeBlock);
}
这里是用dispatch_async()来异步地递归调用block。
if (sqlite3_exec(database, "PRAGMA journal_mode=WAL;", NULL, NULL, &errorMsg) != SQLITE_OK) {
NSLog(@"Failed to set WAL mode: %s", errorMsg);
}
sqlite3_wal_checkpoint(database, NULL); // 每次测试前先checkpoint,避免WAL文件过大而影响性能
测试结果为只读时平均每秒166次,只写时每秒244次,同时读写时每秒各97次。并发性增加了1倍有木有!更夸张的是写入比读取还快了。
static sqlite3 *openDb() {
sqlite3 *database = NULL;
if (sqlite3_open(dbPath, &database) != SQLITE_OK) {
sqlite3_close(database);
NSLog(@"Failed to open database: %s", sqlite3_errmsg(database));
}
return database;
}
sqlite3_config(SQLITE_CONFIG_MULTITHREAD);
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
static void readData() {
static const char *query = "SELECT value FROM test WHERE value < ? ORDER BY value DESC LIMIT 1;";
dispatch_async(queue, ^{
sqlite3 *database = openDb();
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, query, -1, &stmt, NULL) == SQLITE_OK) {
while (YES) {
sqlite3_bind_int(stmt, 1, arc4random());
int returnCode = sqlite3_step(stmt);
if (returnCode == SQLITE_ROW || returnCode == SQLITE_DONE) {
++readCount;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
} else {
NSLog(@"Failed to prepare statement: %s", sqlite3_errmsg(database));
}
sqlite3_close(database);
});
}
static void writeData() {
static const char *update = "UPDATE test SET value = ? WHERE id = ?;";
dispatch_async(queue, ^{
sqlite3 *database = openDb();
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update, -1, &stmt, nil) == SQLITE_OK) {
while (YES) {
sqlite3_bind_int(stmt, 1, arc4random());
sqlite3_bind_int(stmt, 2, arc4random() % 1000 + 1);
if (sqlite3_step(stmt) == SQLITE_DONE) {
++writeCount;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
} else {
NSLog(@"Failed to prepare statement: %s", sqlite3_errmsg(database));
}
sqlite3_close(database);
});
}
这里就无需递归调用了,直接在子线程中循环即可。
static void readData() {
static const char *query = "SELECT value FROM test WHERE value < ? ORDER BY value DESC LIMIT 1;";
dispatch_async(queue, ^{
sqlite3 *database = openDb();
sqlite3_stmt *stmt;
while (sqlite3_prepare_v2(database, query, -1, &stmt, NULL) != SQLITE_OK);
while (YES) {
sqlite3_bind_int(stmt, 1, arc4random());
int returnCode = sqlite3_step(stmt);
if (returnCode == SQLITE_ROW || returnCode == SQLITE_DONE) {
++readCount;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(database);
});
}
static void writeData() {
static const char *update = "UPDATE test SET value = ? WHERE id = ?;";
dispatch_async(queue, ^{
sqlite3 *database = openDb();
sqlite3_stmt *stmt;
while (sqlite3_prepare_v2(database, update, -1, &stmt, nil) != SQLITE_OK);
while (YES) {
sqlite3_bind_int(stmt, 1, arc4random());
sqlite3_bind_int(stmt, 2, arc4random() % 1000 + 1);
if (sqlite3_step(stmt) == SQLITE_DONE) {
++writeCount;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
sqlite3_close(database);
});
}
结果为只读时平均每秒169次,只写时每秒246次,同时读写时每秒分别为90和57次(波动较大)。并发效率有了显著提升,但仍不及第二种方式。
sqlite3_config(SQLITE_CONFIG_SERIALIZED);
static void readData() {
static const char *query = "SELECT value FROM test WHERE value < ? ORDER BY value DESC LIMIT 1;";
dispatch_async(queue, ^{
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, query, -1, &stmt, NULL) == SQLITE_OK) {
while (YES) {
sqlite3_bind_int(stmt, 1, arc4random());
int returnCode = sqlite3_step(stmt);
if (returnCode == SQLITE_ROW || returnCode == SQLITE_DONE) {
++readCount;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
} else {
NSLog(@"Failed to prepare statement: %s", sqlite3_errmsg(database));
}
});
}
static void writeData() {
static const char *update = "UPDATE test SET value = ? WHERE id = ?;";
dispatch_async(queue, ^{
sqlite3_stmt *stmt;
if (sqlite3_prepare_v2(database, update, -1, &stmt, NULL) == SQLITE_OK) {
while (YES) {
sqlite3_bind_int(stmt, 1, arc4random());
sqlite3_bind_int(stmt, 2, arc4random() % 1000 + 1);
if (sqlite3_step(stmt) == SQLITE_DONE) {
++writeCount;
}
sqlite3_reset(stmt);
}
sqlite3_finalize(stmt);
} else {
NSLog(@"Failed to prepare statement: %s", sqlite3_errmsg(database));
}
});
}
测试结果为只读时平均每秒164次,只写时每秒68次,同时读写时每秒分别为57和43次。读线程比写线程的速率更高,而且新线程的加入不需要等待。
WAL模式下,只读时平均每秒176次,只写时每秒254次,同时读写时每秒分别为109和85次。
由此可见,要获得最好的性能的话,WAL模式是必须启用的,为此也有必要自己编译SQLite 3.7.0以上的版本(除非不支持iOS 4.2及以下版本)。
而在测试过的后3种方式中:第3种是效率最低的,不建议使用;第4种读取性能更高,适合无需使用事务的场合;第2种适用范围更广,效率也足够优秀,一般应采用这种方式。
不过要注意的是,第2种方式在测试时的逻辑是完全与数据库相关的。实际中可能要做计算或IO访问等工作,在此期间其他线程都是被阻塞的,这样就会大大降低效率了。因此只建议把访问数据库的逻辑放入队列,其余工作在其他线程里完成。
刚才洗澡时我又想到一点,既然第2种方式不能并行,第4种方式不能保证事务性,那么能否将各自的优点结合起来呢?
于是一个新的实现方案又浮出水面了:使用2个串行队列,分别负责读和写,每个队列各使用一个数据库连接,线程模式可以采用多线程或串行模式。
代码拿方式2稍做修改就行了,这里就不列出了。测试结果波动比较大(估计是checkpoint的影响),多线程模式下平均约为89和73次,串行模式下为91和86次。
但在iPad 2这种双核的机型上,多线程明显要比单队列更具优势:方式2的成绩是每秒各85次,方式3是94和124次(写波动较大),方式4是95和72次,而新方案在多线程模式下是104和168次(写波动很大,40~280之间),串行模式下为108和177次(写波动很大)。
因此极端的优化情况下,可以根据CPU核心数来创建队列数,然后把数据库访问线程随机分配到某个队列中。不过考虑到iOS设备这种嵌入式平台并不需要密集地访问数据库,而且除数据库线程以外还有其他事要做,如果没遇到瓶颈的话,简单的方案2其实也够用了。
转载自http://www.keakon.net/2011/10/25/SQLite%E5%9C%A8%E5%A4%9A%E7%BA%BF%E7%A8%8B%E7%8E%AF%E5%A2%83%E4%B8%8B%E7%9A%84%E5%BA%94%E7%94%A8
参考:
http://sqlite.org/pragma.html#pragma_read_uncommitted
http://www.sqlite.org/threadsafe.html