美文网首页
SQLITE3插入大量数据,效率翻百倍

SQLITE3插入大量数据,效率翻百倍

作者: 晓函 | 来源:发表于2017-07-30 15:16 被阅读549次

    用SQLITE插入商品数据,发现才500条 就需要10多秒时间,太慢了。后来查了一下原因。如下:
    大量插入数据 SQLITE插入一条数据,事务就会被反复地开启、关闭,会增大IO量。如果在插入数据前显式开启事务,插入后再一起提交,则会大大提高IO效率,进而加数据快插入速度。
    根据测试结果:不预先开启事务,效率20 条/s。预先开启事务效率34095条/s。
    这个效率差距挺大的。
    //预先开启事务
    db.execDML("begin;");
    //提交并关闭事务
    db.execDML("commit;");
    加上后,几百条商品数据1秒都不要 就插入完毕了,不错。
    记录一下

    
            //预先开启事务
            db.execDML("begin;");
            //写入
            CppSQLite3Query q;
            for (auto&goods : goods_list)
            {
                
                sqlw = StrTool::Format(L"insert into %s VALUES(%lld,'%s','%s','%s','%s','%s','%s','%s',%d,'%s',%d,%.2f,%.2f,%d,'%s',%.2f,%.2f,%.2f,%d,%d,'%s');", table_name.c_str(), \
                    goods.item_id, goods.activity_id.c_str(),goods.title.c_str(), goods.intro.c_str(), goods.item_url.c_str(), goods.pic_url.c_str(), goods.pic_url_2.c_str(), \
                    goods.coupon_url.c_str(), goods.shop_type, goods.shop_name.c_str(), goods.post_free, goods.price_org, goods.price_discount, goods.sales, goods.me_code.c_str(),\
                    goods.max_campaign.commission_rate, goods.coupon_start_fee, goods.coupon_fee, goods.coupon_count, goods.coupon_surplus, goods.coupon_expire.c_str());
                
                sqla = StringParse::GBToUTF8(StringParse::WStringToString(sqlw).c_str());
                db.execDML(sqla.c_str());
    
                //printf("insert sql:%s\n",sql);
    
    
                q.finalize();
            }
    
            //提交后 关闭事务
            db.execDML("commit;");
    
    

    相关文章

      网友评论

          本文标题:SQLITE3插入大量数据,效率翻百倍

          本文链接:https://www.haomeiwen.com/subject/bntklxtx.html