加入Pythons sqlite模块比手动操作要慢
发布时间:2020-12-12 19:12:23 所属栏目:百科 来源:网络整理
导读:我使用pythons内置的sqlite3模块来访问数据库.我的查询执行150000个条目的表和40000个条目的表之间的连接,结果再次包含大约150000个条目.如果我在 SQLite Manager中执行查询需要几秒钟,但如果我从python执行相同的查询,它在一分钟后就没有完成.这是我使用的
我使用pythons内置的sqlite3模块来访问数据库.我的查询执行150000个条目的表和40000个条目的表之间的连接,结果再次包含大约150000个条目.如果我在
SQLite Manager中执行查询需要几秒钟,但如果我从python执行相同的查询,它在一分钟后就没有完成.这是我使用的代码:
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) 我做了胎儿只是为了衡量执行时间.有没有人对性能的巨大差异有解释?我在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秒钟.上面的代码需要永远.我在这里错过了什么吗? 第二次编辑 似乎sqlite3模块存在性能瓶颈.有一个advice如何使你的查询更快: >确保您在连接列上有索引>使用pysqlite (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |