11

我尝试使用“Python 2.7.4 + sqlite3”和“Firefox SQLite Manager 0.8.0”处理对同一数据库的相同请求。

在小型数据库(8000 条记录)上,Python 和 Firefox 都运行得很快并且给出了相同的结果。

在更大的数据库(2600000 条记录)上:

  • SQLite Manager 在 28 秒内处理数据库(24 条记录)
  • Python程序已经运行了20分钟没有任何结果

下面的程序有什么问题,所以 python sqlite3 无法在合理的时间内处理查询,而相同的请求可以更快地处理?

import sqlite3

_sql1 = """SELECT DISTINCT J2.rule_description,
                J2.feature_type,
                J2.action_item_id,
                J2.rule_items
FROM journal J1,
     journal J2
WHERE J1.base = J2.base
    AND J1.action_item_id=J2.action_item_id
    AND J1.type="Action disabled"
    AND J2.type="Action applied"
    AND J1.rule_description="Some test rule"
    AND J1.action_item_id IN (1, 2, 3, 14, 15, 16, 17, 18, 19, 30, 31, 32)
"""

if __name__ == '__main__':
    sqlite_output = r'D:\results.sqlite'
    with sqlite3.connect(sqlite_output) as connection:
        for row in connection.execute(_sql1):
            print row

更新: SQLite 的命令行 Shell也返回相同的 24 条记录

UPDATE2: sqlite3.sqlite_version 是 '3.6.21'

4

1 回答 1

7

看来,这个问题与 Python 2.7 附带的旧版本的 sqlite 有关。在 python 3.3 中一切正常。

非常感谢@CL 的精彩评论!

在蟒蛇 2.7

>>> import sqlite3
>>> sqlite3.sqlite_version
'3.6.21'

在蟒蛇 3.3

>>> import sqlite3
>>> sqlite3.sqlite_version
'3.7.12'
于 2013-07-02T13:41:09.823 回答