2013-10-15 98 views
0

在我的應用我使用這兩種方法:當執行仿真CPU速度提高到100SQLite的查詢需要太多的CPU

- (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; 
} 

這兩種方法,:

- (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; 
} 

第二個%並阻止用戶界面。在第二個我試圖使用另一個線程,但它是一樣的。 他們從中讀取的表格包含類似7000條記錄的表,因此可能取決於查詢的優化不佳,或者可能是其他情況。我沒有任何線索。

任何幫助將非常感激。

編輯:這是表模式:

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

我應該在哪裏使用索引和什麼樣的?另一件事:現在看錶模式,我注意到有一個錯誤:列「nome」應該是一個varchar(實際上它包含一個字符串),但在我的模式是整數類型。我不知道這是有關我的問題怎麼會一個整數列存儲的文本字符串...

+0

您是否在表格中定義了適當的索引? –

+0

第一個查詢沒有意義; 'GROUP BY orario',每個組只有一個不同的'orario'值,那麼爲什麼要在它上面運行MAX呢? –

+0

你有什麼指數? –

回答

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')進行相同的計算。在數據庫中存儲和比較時間的方法要快得多。