【问题标题】:SQLite query takes too much CPUSQLite 查询占用过多 CPU
【发布时间】:2013-10-22 22:09:14
【问题描述】:

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

- (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(实际上它包含一个字符串)但在我的模式中是整数类型。我不知道这是否与我的问题有关,以及整数列如何存储文本字符串...

【问题讨论】:

  • 你在表上定义了适当的索引吗?
  • 第一个查询没有意义;使用GROUP BY orario,每个组只有一个不同的orario 值,那么为什么要在其上运行MAX
  • 你有什么索引?
  • “orario”没有更高的价值;每个组只有一个值。
  • 在表 orari 上放置一个索引,在 nome 或 idutente 上,以具有更多样化的值为准。并修复 NSDateFormater 的事情——将日期格式化程序的创建/设置移出循环。最终你应该学会如何做“准备好的”陈述,但这可能是以后的课程。

标签: ios performance sqlite


【解决方案1】:

这几行是个大问题:

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') 进行相同的计算。在数据库中存储和比较时间的方法要快得多。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-11
    • 1970-01-01
    • 2021-09-08
    相关资源
    最近更新 更多