我花了数小时试图弄清楚为什么此更新语句很慢。我在iOS上有一个SQLite数据库,当前STEP需要大约2秒钟才能完成此更新语句。在继续之前,我需要重复执行10到100次此代码,最终要花一分钟到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;

最佳答案

我最终将我的应用程序的较旧备份将SQLite3库还原为一个副本,并以某种方式使它更快。另外,我注意到在代码的其他地方,我在不使用事务的情况下重复执行更新语句(如@HotLicks所述),因此进行修复也产生了很大的不同。仍然无法弄清楚为什么该更新语句花很长时间会导致原始问题,但是我猜测也许我正在使用的SQLite3库存在某种缺陷。

奇怪的。

关于ios - SQLite3更新语句真的很慢,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25252246/

10-11 02:29