我使用pythons内置的sqlite3
模块来访问数据库。我的查询在包含150000个条目的表和40000个条目的表之间执行连接,结果再次包含约150000个条目。如果我在SQLite Manager执行查询需要几秒钟,但如果我从python执行相同的查询,它在一分钟后还没有完成。这里是我使用的代码:加入pythons sqlite模块比手动执行要慢
cursor = self._connection.cursor()
annotationList = cursor.execute("SELECT PrimaryId, GOId " +
"FROM Proteins, Annotations " +
"WHERE Proteins.Id = Annotations.ProteinId")
annotations = defaultdict(list)
for protein, goterm in annotationList:
annotations[protein].append(goterm)
我做了fetchall
只是为了衡量执行时间。有没有人有解释性能的巨大差异?我在Mac OS X 10.6.4上使用Python 2.6.1。
编辑
我实现手动加入,这样更快。代码如下所示:
cursor = self._connection.cursor()
proteinList = cursor.execute("SELECT Id, PrimaryId FROM Proteins ").fetchall()
annotationList = cursor.execute("SELECT ProteinId, GOId FROM Annotations").fetchall()
proteins = dict(proteinList)
annotations = defaultdict(list)
for protein, goterm in annotationList:
annotations[proteins[protein]].append(goterm)
因此,当我自己提取表,然后在python中执行连接时,大约需要2秒。上面的代码需要永远。我在这里错过了什么吗?
第二编辑 我试图用同样现在apsw,它工作得很好(代码并不需要在所有改变),性能也不错。我仍然想知道为什么sqlite3
-模块这么慢。
SQLite的指标,我使用Python 2.6.5。它在PySqlite主页上说,Python自带的'sqlite3'包与'PySqlite'相同,但它没有说明哪个版本。 – 2010-09-15 12:01:13
尝试获得较新版本 – tamasd 2010-09-15 12:49:12