让 SQLite 运行 SELECT 更快

Making SQLite run SELECT faster

情况:我的 SQLite 数据库(~300MB)中的 table 中有大约 4000 万行、3 列无组织数据。我的数据示例如下:

|  filehash  |  filename  |  filesize  |
|------------|------------|------------|
|   hash111  |    fileA   |    100     |
|   hash222  |    fileB   |    250     |
|   hash333  |    fileC   |    380     |
|   hash111  |    fileD   |    250     |  #Hash collision with fileA
|   hash444  |    fileE   |    520     |
|     ...    |     ...    |    ...     |

问题:单个 SELECT 语句可能需要 3 到 5 秒。我 运行ning 的应用程序需要快速。单个查询耗时 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()

问题:如何使 SELECT 语句 运行 更快(我知道这听起来很疯狂,但我希望速度低于 0.5 秒)?

附加信息 1:我在 运行RPi 3B(1GB RAM,默认 100MB SWAP)上的 Python 2.7 程序上安装它。我问主要是因为我担心它会使 RPi 崩溃,因为 'not enough RAM'.

作为参考,当使用我的应用 运行ning 从数据库正常读取时,我们看到最多 55MB 的可用 RAM,以及几百 MB 的缓存数据 - 我不确定这是否是SQLite 缓存(SWAP 尚未触及)。

附加信息 2:我愿意使用其他数据库来存储 table(我正在寻找 PyTables 或 ZODB 作为替代品 - 这么说吧,我有点绝望了)。

附加信息 3:有 NO 个唯一键,因为 SELECT 语句将在列中查找匹配项,这些匹配项只是散列值,显然存在冲突.

目前,数据库必须扫描整个 table 才能找到所有匹配项。要speed up searches,使用索引:

CREATE INDEX my_little_hash_index ON hashtable(filehash);