作为对我的问题的回答:sqlite.fetchall() 这么慢是否正常?似乎 fetch-all 和 fetch-one 对于 sqlite 来说可能非常慢。
正如我在那里提到的,我有以下查询:
time0 = time.time()
self.cursor.execute("SELECT spectrum_id, feature_table_id "+
"FROM spectrum AS s "+
"INNER JOIN feature AS f "+
"ON f.msrun_msrun_id = s.msrun_msrun_id "+
"INNER JOIN (SELECT feature_feature_table_id, min(rt) AS rtMin, max(rt) AS rtMax, min(mz) AS mzMin, max(mz) as mzMax "+
"FROM convexhull GROUP BY feature_feature_table_id) AS t "+
"ON t.feature_feature_table_id = f.feature_table_id "+
"WHERE s.msrun_msrun_id = ? "+
"AND s.scan_start_time >= t.rtMin "+
"AND s.scan_start_time <= t.rtMax "+
"AND base_peak_mz >= t.mzMin "+
"AND base_peak_mz <= t.mzMax", spectrumFeature_InputValues)
print 'query took:',time.time()-time0,'seconds'
time0 = time.time()
spectrumAndFeature_ids = self.cursor.fetchall()
print time.time()-time0,'seconds since to fetchall'
select 语句的执行大约需要 50 秒(可以接受)。但是,fetchall() 需要 788 秒,只获取 981 个结果。
作为对我的问题的回答而提出的加快查询速度的方法:sqlite.fetchall() 这么慢是否正常?使用 fetchmany() 并没有提高获取结果的速度。
运行 sqlite 查询后如何加快获取结果的速度?
与我尝试在命令行上执行它的 sql 完全相同:
sqlite> SELECT spectrum_id, feature_table_id
...> FROM spectrum AS s
...> INNER JOIN feature AS f
...> ON f.msrun_msrun_id = s.msrun_msrun_id
...> INNER JOIN (SELECT feature_feature_table_id, min(rt) AS rtMin, max(rt) AS rtMax, min(mz) AS mzMin, max(mz) as mzMax
...> FROM convexhull GROUP BY feature_feature_table_id) AS t
...> ON t.feature_feature_table_id = f.feature_table_id
...> WHERE s.msrun_msrun_id = 1
...> AND s.scan_start_time >= t.rtMin
...> AND s.scan_start_time <= t.rtMax
...> AND base_peak_mz >= t.mzMin
...> AND base_peak_mz <= t.mzMax;
更新:
所以我大约 45 分钟前开始在命令行上运行查询,它仍然很忙,所以使用命令行也很慢。