Tim*_*ong 3 sqlite performance python-2.7 raspberry-pi3
情况:我的 SQLite 数据库 (~300MB) 的一个表中有大约 4000 万行、3 列无组织数据。我的数据示例如下:
| filehash | filename | filesize |
|------------|------------|------------|
| hash111 | fileA | 100 |
| hash222 | fileB | 250 |
| hash333 | fileC | 380 |
| hash111 | fileD | 250 | #Hash collision with fileA
| hash444 | fileE | 520 |
| ... | ... | ... |
Run Code Online (Sandbox Code Playgroud)
问题:单个SELECT语句可能需要 3 到 5 秒。我正在运行的应用程序需要很快。耗时 3 到 5 秒的单个查询太长了。
#calculates hash
md5hash = hasher(filename)
#I need all 3 columns so that I do not need to parse through the DB a second time
cursor.execute('SELECT * FROM hashtable WHERE filehash = ?', (md5hash,))
returned = cursor.fetchall()
Run Code Online (Sandbox Code Playgroud)
问题:如何使SELECT语句运行得更快(我知道这听起来很疯狂,但我希望速度低于 0.5 秒)?
附加信息 1:我在 RPi 3B(1GB RAM,默认 100MB SWAP)上的 Python 2.7 程序上运行它。我问主要是因为我担心它会因为“内存不足”而导致 RPi 崩溃。
作为参考,当我的应用程序正常从数据库读取时,我们正在查看最大 55MB 的可用 RAM,以及几百 MB 的缓存数据 - 我不确定这是否是 SQLite 缓存(尚未触及 SWAP)。
附加信息 2:我愿意使用其他数据库来存储表(我正在寻找 PyTables 或 ZODB 作为替代品 - 只能说我有点绝望)。
附加信息 3:没有唯一键,因为该SELECT语句将在列中查找匹配项,这些匹配项只是哈希值,显然存在冲突。
目前,数据库必须扫描整个表才能找到所有匹配项。要 加快搜索速度,请使用索引:
CREATE INDEX my_little_hash_index ON hashtable(filehash);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2064 次 |
| 最近记录: |