我花了好几个小时试图弄清楚为什么这个更新语句很慢。我在iOS上有一个SQLite数据库,它目前需要约2秒才能完成此更新语句的STEP。由于我在继续之前已经完成了10到100次重复此代码,因此最终需要花费1到12分钟,这对我的应用程序来说是不行的。
请帮忙。 : - )
BOOL result;
if (status == -1) {
return -1;
}
else {
if(updateStmt == nil) {
const char *sql = "update Points set name = ?, type = ?, subtype = ?, description = ?, address = ?, latitude = ?, longitude = ?, porder = ?, phone = ?, url = ?, segmentTime = ?, segmentDistance = ?, segmentMins =?, userSelectedDirectionType =?, currentDirectionType=?, distanceType = ?, dateTime1 = ?, dateTime2 = ?, recalcDir = ? where id = ? ";
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK)
NSLog(0, @"Error while creating add statement. '%s'", sqlite3_errmsg(database));
}
sqlite3_bind_text(updateStmt, 1, [annotation.title UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_int(updateStmt, 2, annotation.typeID);
sqlite3_bind_int(updateStmt, 3, annotation.subtypeID);
sqlite3_bind_text(updateStmt, 4, [annotation.description UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 5, [annotation.address UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(updateStmt, 6, annotation.coordinate.latitude);
sqlite3_bind_double(updateStmt, 7, annotation.coordinate.longitude);
sqlite3_bind_int(updateStmt, 8, annotation.porder);
sqlite3_bind_text(updateStmt, 9, [annotation.phone UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_text(updateStmt, 10, [annotation.URL UTF8String], -1, SQ
sqlite3_bind_text(updateStmt, 11, [annotation.segmentArrivalTime UTF8String], -1, SQLITE_TRANSIENT);
sqlite3_bind_double(updateStmt, 12, annotation.Segment);
sqlite3_bind_int(updateStmt, 13, annotation.segmentMins);
sqlite3_bind_int(updateStmt, 14, annotation.userSelectedDirectionType);
sqlite3_bind_int(updateStmt, 15, annotation.currentDirectionType);
sqlite3_bind_int(updateStmt, 16, annotation.distanceType);
sqlite3_bind_double(updateStmt, 17, annotation.arrivalDateAndTime.timeIntervalSince1970);
sqlite3_bind_double(updateStmt, 18, annotation.departureDateAndTime.timeIntervalSince1970);
sqlite3_bind_int(updateStmt, 19, annotation.shouldRecalculateNextPointsDirections);
sqlite3_bind_int(updateStmt, 20, annotation.pointID);
if(SQLITE_DONE != sqlite3_step(updateStmt))
NSLog(@"sqlite: Error while updating a point id = %d. '%s'", annotation.pointID, sqlite3_errmsg(database));
result = YES;
}
sqlite3_reset(updateStmt);
return result;
答案 0 :(得分:0)
我最终将我的SQLite3库恢复为我的应用程序的旧备份中的副本,并以某种方式使其更快。另外,我注意到我的代码中的其他地方我在不使用事务的情况下重复更新语句(如@HotLicks所提到的那样),因此修复它也会产生很大的不同。仍然无法弄清楚为什么原始问题发生在更新语句上需要很长时间,但我猜测我使用的SQLite3库可能存在缺陷。
怪异。