Sqlite3读写速度跟什么有关?
时间: 2019-10-31来源:开源中国
前景提要
HDC调试需求开发(15万预算),能者速来!>>>
用同一个Python脚本,测试读写sqlite3 1000次所用的时间,在四台机器上测试,发现结果差别巨大,搞不清到底跟什么指标有关系。请大神指点。
测试结果如下:
(1)个人工作PC,I5-7400,Win10,Python2。
测试结果如下图所示,总耗时178.8秒。

(2)公司40核服务器,Ubuntu,Python2。
测试结果如下图所示,总耗时3.8秒。

(3)徐州租用32核服务器,Ubuntu,Python2。
测试结果如下图所示,总耗时272.8秒。

(4)阿里云1核云主机,Ubuntu,Python2。
测试结果如下图所示,总耗时9.6秒。


测试结果总结如下表:
平台 CPU核数 耗时
个人工作PC 4核(I5-7400) 178.8秒
公司40核服务器 40核 3.88秒
徐州租用32核服务器 阿里云1核云主机
32核 1核
372.8秒 9.6秒

刚开始以为是跟CPU关系很大,但是测试结果看起来并非如此。请大神指点。

最后附上测试脚本: # coding: utf-8 # speed_test.py import zlib import pickle import time import sqlite3 from datetime import datetime def sqlite_test(): """测试sqlite读写速度 """ # 待写入数据html with open('page.html') as f: html = f.read() print 'Original data size = {}'.format(len(html)) # 连接sqlite数据库,创建表 start_time = time.time() conn = sqlite3.connect('test.db', timeout=10000, isolation_level=None, detect_types=sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES) conn.text_factory = lambda x: unicode(x, 'utf-8', 'replace') sql = """ CREATE TABLE IF NOT EXISTS cache ( key TEXT NOT NULL PRIMARY KEY UNIQUE, value BLOB, updated timestamp DEFAULT (datetime('now', 'localtime')) ); """ conn.execute(sql) conn.execute("CREATE INDEX IF NOT EXISTS keys ON cache (key);") print 'Time used to create sqlite database: {} seconds.'.format(round(time.time() - start_time, 2)) # 读写1000次测试 test_times = 1000 all_times_used = [] start_time = time.time() for i in range(test_times): # 写入数据 updated = datetime.now() key = 'cache-{}'.format(i) # 写数据 conn.execute("INSERT OR REPLACE INTO cache (key, value, updated) VALUES(?, ?, ?);", ( key, html, updated) ) # 读取数据 row = conn.execute("SELECT value, updated FROM cache WHERE key=?;", (key,)).fetchone() i += 1 if i % 100 == 0: # 每100次,输出一次用时 times_used = round(time.time() - start_time, 2) all_times_used.append(times_used) print '[{}] {}'.format(times_used, i) start_time = time.time() print 'Total used {} seconds, avg time used for per write-read op: {} seconds.'.format(sum(all_times_used), round(sum(all_times_used)/float(test_times), 4)) if __name__ == '__main__': sqlite_test()
再补充一下做这个测试的背景:公司一爬虫脚本在不同机器上运行速度差别很大,一路追踪最后发现是跟读写缓存数据库有关,缓存数据库用的sqlite,于是有了该测试。

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行