[英]Speed up an sqlite3 database search
I"m new to databases and just wrote my first code using sqlite3
. It does the job, but is running extremely slowly and I'm hoping to get some advice regarding how to speed things up. 我是数据库的新手,只是使用sqlite3
编写了我的第一个代码。它完成了工作,但运行速度非常慢,我希望得到一些关于如何加快速度的建议。
Right now my code looks something like this: 现在我的代码看起来像这样:
For Line in File:
Line= Line.strip('\n').split('\n')
Location = int(Line[1])
MChr = Line[0]
cur = db.execute('''SELECT Start, End, Chr, Feature1, Feature2, Feature3, Feature4, FROM DataBase
WHERE Start <= ? AND End >= ? AND Chr == ?''', (Location, Location, MChr))
for (Start, Stop, Chr, Feature1, Feature2, Feature3, Feature4) in cur:
if Feature1 == "A string":
do something....
if Feature2 == "A string":
do something....
My database is a little over one million entries which is probably why my program is running slow but I was wondering if there is a way to make the search more efficient to circumvent having to run through all million for every line. 我的数据库有一百多万个条目,这可能就是为什么我的程序运行缓慢,但我想知道是否有办法让搜索更有效率,以避免每行扫描所有百万。 (Perhaps first pull out all matching Chrs?) (或许首先拉出所有匹配的Chrs?)
You should index your db: 你应该索引你的数据库:
http://www.sqlite.org/lang_createindex.html http://www.sqlite.org/lang_createindex.html
This should speed things up. 这应该加快速度。
Create indexes on the columns in question. 在相关列上创建索引。 If your table name is DataBase
, then try something like: 如果您的表名是DataBase
,那么请尝试以下方法:
db.execute('''CREATE UNIQUE INDEX Start_index ON `DataBase` (`Start`(64))''')
db.execute('''CREATE UNIQUE INDEX End_index ON `DataBase` (`End`(64))''')
db.execute('''CREATE UNIQUE INDEX Chr_index ON `DataBase` (`Chr`(64))''')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.