如何在iPhone上优化sqlite(加载100000行)?
我有大问题。我的数据库(sqlite)中有 100000 行。我现在无法减少这个数字。
我正在使用以下代码从这个大表中获取数据: wpis
-(NSMutableArray *) return_hours_for_near_two: (int) lacze : (int) hour :(int) okres {
NSString * query = [ NSString stringWithFormat:@"SELECT godzina, minuta FROM wpis WHERE liniaprzystanekid=%i AND okres=%i AND (godzina = %i OR godzina = %i)", lacze, okres, hour,hour+1];
NSLog(@"%@", query);
const char * querystring = [ query UTF8String];
NSMutableArray * temp = [ [NSMutableArray alloc] init];
sqlite3_stmt * statment;
if(sqlite3_prepare_v2(database, querystring, -1, &statment, nil) == SQLITE_OK){
while(sqlite3_step(statment) == SQLITE_ROW){
NSMutableArray * arr = [[[NSMutableArray alloc] init] autorelease];
[arr addObject:[NSNumber numberWithInt: sqlite3_column_int(statment, 0)]];
[arr addObject:[NSNumber numberWithInt: sqlite3_column_int(statment, 1)]];
[temp addObject:arr];
}
} else {
return NULL;
}
sqlite3_finalize(statment);
return temp;
}
编辑:抱歉在数据库中混合语言:)
我知道我可以使用 CoreData,但现在为时已晚。我可以做什么来优化这个查询?因为我必须为每个表格单元格调用它,而且它非常非常慢。有什么解决办法吗?
I have big problem. I have 100000 rows in my database(sqlite). I can't reduce this number now.
I' m using following code to get dataa from this large table: wpis
-(NSMutableArray *) return_hours_for_near_two: (int) lacze : (int) hour :(int) okres {
NSString * query = [ NSString stringWithFormat:@"SELECT godzina, minuta FROM wpis WHERE liniaprzystanekid=%i AND okres=%i AND (godzina = %i OR godzina = %i)", lacze, okres, hour,hour+1];
NSLog(@"%@", query);
const char * querystring = [ query UTF8String];
NSMutableArray * temp = [ [NSMutableArray alloc] init];
sqlite3_stmt * statment;
if(sqlite3_prepare_v2(database, querystring, -1, &statment, nil) == SQLITE_OK){
while(sqlite3_step(statment) == SQLITE_ROW){
NSMutableArray * arr = [[[NSMutableArray alloc] init] autorelease];
[arr addObject:[NSNumber numberWithInt: sqlite3_column_int(statment, 0)]];
[arr addObject:[NSNumber numberWithInt: sqlite3_column_int(statment, 1)]];
[temp addObject:arr];
}
} else {
return NULL;
}
sqlite3_finalize(statment);
return temp;
}
EDIT: sorry for mixing languages in database :)
I know I could use CoreData, but now it is too late. What I can do to optimize this query? Because I must call it for every table cell and it's very, very slow. Is there any solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
返回对象而不是返回 NSMutableArray 与 NSMutableArray 的 1000 行相比,它的处理速度更快。
您还可以检查此链接:- 对于非常大的数据库文件,sqlite 的性能特点是什么?
Return the object instead of returning NSMutableArray It will process fast as compare to your NSMutableArray for 1000 lines.
You can also check this link:- What are the performance characteristics of sqlite with very large database files?