sqlite大量数据插入优化总结

前端之家收集整理的这篇文章主要介绍了sqlite大量数据插入优化总结前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
1.将插入操作放到一个Transaction里,默认的每条insert语句都会开启一个Transaction,参见:
2.手动拼写插入语句,不要运用ORM,虽然代码会看起来很多,但是效率绝对大幅度提升
未优化前代码:

    NSArray *organizations = [organizationsDic allValues];
    for (Organization *organization in organizations) {
        [organization save];

    }
优化后代码:
    //手动拼接sql语句,不采用OR模式,提升点性能,有待进入内网测试
    FMDatabase *db = [MD_DataBaseTool getDb];
    NSString *sql;
    //保存公司或部门
    [db beginTransaction];
    NSArray *organizations = [organizationsDic allValues];
    for (Organization *organization in organizations) {
        sql = [NSString stringWithFormat:@"insert into Organization(organizationId,name,pId,memberNumber,type,sortNumber,groupId) values('%@','%@','%@')",organization.organizationId,organization.name,organization.pId,organization.memberNumber,organization.type,organization.sortNumber,organization.groupId];
        [db executeUpdate:sql];
    }

    //保存用户-部门映射表
    for (UserOrganizationMap *userOrganizationMap in userOrganizations) {
        sql = [NSString stringWithFormat:@"insert into UserOrganizationMap(userOrganizationId,userId,organizationId,position,recentlyMap) values('%@',userOrganizationMap.userOrganizationId,userOrganizationMap.userId,userOrganizationMap.organizationId,userOrganizationMap.position,userOrganizationMap.recentlyMap];
        [db executeUpdate:sql];
    }
    [db commit];
原文链接:https://www.f2er.com/sqlite/200590.html

猜你在找的Sqlite相关文章