ios - SQLite 查询占用太多 CPU

标签 ios performance sqlite

在我的应用中,我使用了这两种方法:

- (NSString *)Method1:(NSString *)identificativo :(int)idUtente {
fileMgr = [NSFileManager defaultManager];
sqlite3_stmt *stmt=nil;
sqlite3 *dbase;
NSString *ora;
NSString *database = [self.GetDocumentDirectory stringByAppendingPathComponent:@"db.sqlite"];
sqlite3_open([database UTF8String], &dbase);
NSString *query = [NSString stringWithFormat:@"select MAX(orario) from orari where flag=0 and nome=\"%@\" and idutente=%d group by orario", identificativo, idUtente];
const char *sql = [query UTF8String];
sqlite3_prepare_v2(dbase, sql, -1, &stmt, NULL);
while(sqlite3_step(stmt) == SQLITE_ROW) {
    NSString *orario = [NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, 0)];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dataV = [formatter dateFromString:orario];
    ora = [formatter stringFromDate: dataV];
}
sqlite3_finalize(stmt);
sqlite3_close(dbase);
return ora;
}

第二个:

- (int)Method2:(NSString *)nomeM :(int)idUtente {
__block int conteggio = 0;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^(void) {
    fileMgr = [NSFileManager defaultManager];
    sqlite3_stmt *stmt=nil;
    sqlite3 *dbase;
    NSString *database = [self.GetDocumentDirectory stringByAppendingPathComponent:@"db.sqlite"];
    sqlite3_open([database UTF8String], &dbase);
    NSString *query = [NSString stringWithFormat:@"select nome, count(*) from orari where datetime(orario)>datetime('now','localtime') and flag=0 and nome=\"%@\" and idutente=%d group by nome", nomeM, idUtente];
    const char *sql = [query UTF8String];
    sqlite3_prepare_v2(dbase, sql, -1, &stmt, NULL);
    while(sqlite3_step(stmt) == SQLITE_ROW) {
    conteggio = [[NSNumber numberWithInt:(int)sqlite3_column_int(stmt, 1)] intValue];
    }
sqlite3_finalize(stmt);
sqlite3_close(dbase);
});
return conteggio;
}

这两种方法在执行时都会将模拟器 CPU 速度发送到 100% 并阻止 UI。在第二个中,我尝试使用另一个线程,但它是一样的。 他们正在读取的表包含大约 7000 条记录,因此这可能取决于查询优化不佳,或者可能是其他原因。我一点头绪都没有。

编辑:这是表模式:

dataid -> integer -> Primary key
orario -> datetime
nome -> varchar (150)
flag -> integer
pos -> varchar (150)
idutente -> integer

我应该在哪里使用索引以及使用何种索引?

另一件事:现在查看表模式,我注意到有一个错误:“nome”列应该是一个 varchar(实际上它包含一个字符串)但在我的模式中是整数类型。我不知道这是否与我的问题有关,整数列如何存储文本字符串...

最佳答案

这些行是个大问题:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];

他们可能需要 0.2+ 秒。这是一个非常昂贵的电话。您真的只需要一个 NSDateFormatter,您只需设置一次并让它们都使用它。要么将其设置在循环之外,要么更好,将其设为静态并在多次调用时保留它。

与此类似:

NSString *query = [NSString stringWithFormat:@"select nome, count(*) from orari where datetime(orario)>datetime('now','localtime') and flag=0 and nome=\"%@\" and idutente=%d group by nome", nomeM, idUtente];

如果您能够修改数据库,this solution可能有助于提高速度。

您正在为 datetime('now', 'localtime') 反复进行相同的计算。在数据库中存储和比较时间的方法要快得多。

关于ios - SQLite 查询占用太多 CPU,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19375558/

相关文章:

ios - 为 UIWebkit 赋予 Value 并更改实例化 ViewController [Swift 4]

c# - Swapchain.Present() 花费的时间太长,导致滞后

Amazon EC2 上的 MySQL 与 SQLite

python - 如何为整个 Tornado Web 应用程序建立与 SQLite 数据库的一个连接?

sqlite - Codeception - 由于配置无效,无法使用 DB 模块

ios - 托管内容对应用内购买有何好处?

ios - 如何在 MVVM 中快速从一个屏幕导航到另一个屏幕

iOS 蓝牙 retrievePeripherals 导致 EXC_BAD_ACCESS 崩溃

algorithm - Big-O 算法阶数的正式定义中的常量 k 和 n0 是什么?

java - 更新 sqlite 数据的最佳实践是什么?