Flask-SQLAlchemy批量插入數(shù)據(jù)性能測(cè)試

2022年7月7日14:44:40 發(fā)表評(píng)論 3,564 ℃

Mysql數(shù)據(jù)庫(kù)服務(wù)器4核,批量寫入1萬(wàn)條數(shù)據(jù)、每條數(shù)據(jù)4個(gè)字段 性能測(cè)試:

1、常規(guī)for循環(huán)一條條寫入 (這種方式波動(dòng)比較大,基本在60-120s之間)

startTime = time.time()
for i in range(10000):
    db.session.add(
        Test(
            blog_name='阿湯博客{}'.format(i),
            blog_url='http://www.zhongjima.net/'.format(i),
            blog_desc='我是阿湯博客,地址是http://www.zhongjima.net/'.format(i),
            create_time=datetime.datetime.now()
        )
    )
db.session.commit()
endTime = time.time()
diff = round(endTime - startTime, 3)
print("耗時(shí):{}s").format(diff)

耗時(shí):95.468s

2、通過(guò)bulk_save_objects批量寫入

startTime = time.time()
db.session.bulk_save_objects(
    [
        Test(
            blog_name='阿湯博客{}'.format(i),
            blog_url='http://www.zhongjima.net/'.format(i),
            blog_desc='我是阿湯博客,地址是http://www.zhongjima.net/'.format(i),
            create_time=datetime.datetime.now()
        )
        for i in range(10000)
    ]
)
endTime = time.time()
diff = round(endTime - startTime, 3)
print("耗時(shí):{}s").format(diff)
耗時(shí):0.695s

3、通過(guò)bulk_insert_mappings批量寫入

startTime = time.time()
db.session.bulk_insert_mappings(
    Test,
    [
        dict(
            blog_name='阿湯博客{}'.format(i), 
            blog_url='http://www.zhongjima.net/'.format(i),
            blog_desc='我是阿湯博客,地址是http://www.zhongjima.net/'.format(i), 
            create_time=datetime.datetime.now()
        )
        for i in range(10000)
    ]
)
endTime = time.time()
diff = round(endTime - startTime, 3)
print("耗時(shí):{}s").format(diff)

耗時(shí):0.658s

4、原生insert批量寫入

startTime = time.time()
db.session.execute(
    Test.__table__.insert(),
    [
        {
            "blog_name": '阿湯博客{}'.format(i),
            "blog_url": 'http://www.zhongjima.net/'.format(i),
            "blog_desc": '我是阿湯博客,地址是http://www.zhongjima.net/'.format(i),
            "create_time": datetime.datetime.now()
         }
        for i in range(10000)
    ]
)
endTime = time.time()
diff = round(endTime - startTime, 3)
print("耗時(shí):{}s").format(diff)

耗時(shí):0.434s

總得來(lái)說(shuō),只要不是使用第一種方式批量寫入,基本上不會(huì)有太大的性能問(wèn)題。

【騰訊云】云服務(wù)器、云數(shù)據(jù)庫(kù)、COS、CDN、短信等云產(chǎn)品特惠熱賣中

發(fā)表評(píng)論

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: