iOS大型SQLite数据集性能低下
我正在为iOS开发一个应用程序,该应用程序基于当前视图的边界框在地图上显示轨迹。因此,当用户在地图上移动时,它将加载一组适当的轨迹 SQLite表很大,有260k行,其中每一行都是轨迹上的一个点。该表具有主键int、纬度浮点、经度浮点和名称varchar。纬度和经度列被索引。当我查询时,我在寻找纬度在右下和左上纬度之间,经度在左上和右下经度之间的位置。当查询返回预期结果时,它在桌面和手机上都能完美地工作。问题是,在我的Mac电脑上,查询会立即返回,而在手机上,可能只需要4秒钟就可以返回任何内容。瓶颈似乎是数据库查询,我开始认为这是硬件的限制 我尝试使用CoreData,这是我首先注意到的问题。然后我开始使用FMDB访问数据库,但仍然存在问题 我没有对数据库或连接进行任何调整 queryForTrails方法的精髓iOS大型SQLite数据集性能低下,ios,performance,sqlite,core-data,Ios,Performance,Sqlite,Core Data,我正在为iOS开发一个应用程序,该应用程序基于当前视图的边界框在地图上显示轨迹。因此,当用户在地图上移动时,它将加载一组适当的轨迹 SQLite表很大,有260k行,其中每一行都是轨迹上的一个点。该表具有主键int、纬度浮点、经度浮点和名称varchar。纬度和经度列被索引。当我查询时,我在寻找纬度在右下和左上纬度之间,经度在左上和右下经度之间的位置。当查询返回预期结果时,它在桌面和手机上都能完美地工作。问题是,在我的Mac电脑上,查询会立即返回,而在手机上,可能只需要4秒钟就可以返回任何内容。
if( ![db open] ) {
[db release];
NSLog(@"Error opening DB: %@", dbPath);
}
FMResultSet *trails = [db executeQueryWithFormat:@"SELECT zname, zlatitude, zlongitude FROM ztrail WHERE ztype = 1 and zlatitude BETWEEN %@ and %@ AND zlongitude BETWEEN %@ and %@ order by zname", lrLat, ulLat, ulLon,lrLon];
//Start to load the map with data
NSString *lastTrailName=@"";
int idx = 0;
CLLocationCoordinate2D *trailCoords = nil;
NSUInteger coordSize = 20;
trailCoords = malloc(sizeof(CLLocationCoordinate2D)*coordSize);
while( [trails next] ) {
NSString *trailName = [trails stringForColumnIndex:0];
NSString *lat = [trails stringForColumnIndex:1];
NSString *lon = [trails stringForColumnIndex:2];
if( [lastTrailName compare:trailName] != NSOrderedSame ) {
if(idx > 0) {
[trailLines addObject:[MKPolyline polylineWithCoordinates:trailCoords count:idx]];
free(trailCoords);
idx = 0;
coordSize = 20;
}
lastTrailName = trailName;
trailCoords = malloc(sizeof(CLLocationCoordinate2D)*coordSize);
}
if(idx == coordSize) {
coordSize *= 2;
trailCoords = realloc(trailCoords, sizeof(CLLocationCoordinate2D) * coordSize);
}
trailCoords[idx++] = CLLocationCoordinate2DMake([lat doubleValue], [lon doubleValue]);
}
//Build the new polyline
[trailLines addObject:[MKPolyline polylineWithCoordinates:trailCoords count:idx]];
//NSLog(@"Num Trails: %d", [trailLines count]);
free(trailCoords);
//NSLog(@"Num of Points %d for %@",idx, lastTrailName);
if( [trailLines count] > 0 ) {
dispatch_async(dispatch_get_main_queue(),^{
[mapView addOverlays:trailLines];
});
}
如果需要,我可以提供一些NSLog数据。我也将为Android做同样的应用程序,所以我现在想尝试解决性能问题。您可以尝试检查索引/查询是否可以通过ztrail表中ztype、zlatitude、Zlongite、zorder的覆盖索引进行优化。如果它仍然是缓慢的,考虑编译SQLite自己与R树模块包括在内。