2 つの内部結合テーブルから選択する SQL クエリがあります。select ステートメントの実行には約 50 秒かかります。ただし、fetchall() には 788 秒かかり、981 件の結果しか取得できません。これは、クエリと fetchall のコードです。
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'
fetchall に時間がかかる理由はありますか?
アップデート
やっている:
while 1:
info = self.cursor.fetchone()
if info:
<do something>
else:
break
と同じくらい遅くなります
allInfo = self.cursor.fetchall()
for info in allInfo:
<do something>