不间断睡眠是我的Python程序非常慢的原因吗(如果是的话,我如何解决这个问题?)?

2024-04-26 17:52:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下select语句(使用sqlite3和pysqlite模块):

self.cursor.execute("SELECT precursor_id FROM MSMS_precursor "+
  "JOIN spectrum ON spectrum_id = spectrum_spectrum_id "+
  "WHERE spectrum_id = spectrum_spectrum_id "+
  "AND ROUND(ion_mz,9) = ? AND ROUND(scan_start_time,4) = ? "+
  "AND msrun_msrun_id = ?", select_inputValues)

在Python中运行需要55秒。当直接在SQLite命令行上运行它时,只需要15毫秒,现在,我注意到,在这一步中,Python程序进入不间断睡眠状态(31283 ndeklein 18 0 126m 24m 3192 D 1.0 0.0 2:02.50 python,在最上面的输出中是D),它从100%的CPU下降到大约1%的CPU。既然我在这个查询过程中注意到了这一点,那么在运行我询问的关于here的查询时,我还查看了顶部的输出。在这段时间里,top也显示它进入了不间断的睡眠状态,尽管它在R和D之间来回切换,只会减慢到50%左右(它的波动取决于它是处于D还是R状态)。在

如果我的程序没有被打断的话,我认为这是正确的。如果这是真的,我如何确保程序不会进入这种状态?在


更新1:

使用Python的EXPLAIN查询计划返回:

^{pr2}$

使用sqlite命令行的EXPLAIN查询计划返回:

0|0|1|SCAN TABLE spectrum (~50000 rows)
0|1|0|SEARCH TABLE MSMS_precursor USING INDEX fk_MSMS_precursor_spectrum_spectrum_id_1 (spectrum_spectrum_id=?) (~2 rows)

EXPLAIN using Python返回:

(0, u'Trace', 0, 0, 0, u'', u'00', None)

使用sqlite返回解释:

0|Trace|0|0|0||00|
1|Real|0|1|0|438.718658447|00|
2|Real|0|2|0|692.6345000000001|00|
3|Integer|1|3|0||00|
4|Goto|0|39|0||00|
5|OpenRead|1|33|0|13|00|
6|OpenRead|0|39|0|5|00|
7|OpenRead|2|41|0|keyinfo(1,BINARY)|00|
8|Rewind|1|35|0||00|
9|Column|1|8|5||00|
10|RealAffinity|5|0|0||00|
11|Integer|4|6|0||00|
12|Function|2|5|4|round(2)|02|
13|Ne|2|34|4||6a|
14|Column|1|12|4||00|
15|Ne|3|34|4|collseq(BINARY)|6c|
16|Column|1|0|8||00|
17|IsNull|8|34|0||00|
18|Affinity|8|1|0|d|00|
19|SeekGe|2|34|8|1|00|
20|IdxGE|2|34|8|1|01|
21|IdxRowid|2|7|0||00|
22|Seek|0|7|0||00|
23|Column|1|0|9||00|
24|Column|2|0|10||00|
25|Ne|10|33|9|collseq(BINARY)|6b|
26|Column|0|1|5||00|
27|RealAffinity|5|0|0||00|
28|Integer|9|6|0||00|
29|Function|2|5|11|round(2)|02|
30|Ne|1|33|11||6a|
31|Column|0|0|13||00|
32|ResultRow|13|1|0||00|
33|Next|2|20|0||00|
34|Next|1|9|0||01|
35|Close|1|0|0||00|
36|Close|0|0|0||00|
37|Close|2|0|0||00|
38|Halt|0|0|0||00|
39|Transaction|0|0|0||00|
40|VerifyCookie|0|31|0||00|
41|TableLock|0|33|0|spectrum|00|
42|TableLock|0|39|0|MSMS_precursor|00|
43|Goto|0|5|0||00|

iostat回来了:

io-bash-3.2$ iostat
Linux 2.6.18-194.26.1.el5 (ningal.cluster.lifesci.ac.uk)         06/04/2012

avg-cpu:  %user   %nice %system %iowait  %steal   %idle
           14.35    0.00    0.30    0.01    0.00   85.34

Device:            tps   Blk_read/s   Blk_wrtn/s   Blk_read   Blk_wrtn
sda               1.16         4.55        17.22    1520566    5752802
sda1              0.00         0.02         0.00       5074         34
sda2              1.16         4.53        17.22    1515184    5752768
sdb               0.00         0.02         0.00       5108          0
dm-0              2.29         3.88        16.70    1297226    5579336
dm-1              0.00         0.00         0.00        928          0
dm-2              0.11         0.65         0.52     216106     173432

更新2

我把数据库迁移到MySQL,这里的查询只需要0.001秒, 尽管对于我正在执行的所有其他查询来说,它实际上比sqlite慢(我为sqlite进行了优化,所以这可能也可能并不令人惊讶)。在


Tags: and程序idsqlite状态columnintegerspectrum
2条回答

SQLite和Python存在性能问题。阅读此线程了解更多信息。这里有一些建议-试试看,可能会有用-比如在连接字段中添加索引或使用pysqlite。在

{a1}

正如我在an answer to a prior question you asked中提到的,您是否尝试过sqlite模块apsw?From the website

APSW is a Python wrapper for the SQLite embedded relational database engine. In contrast to other wrappers such as pysqlite it focuses on being a minimal layer over SQLite attempting just to translate the complete SQLite API into Python. The documentation has a section on the differences between APSW and pysqlite.

我亲自尝试过,它似乎确实更好地反映了SQL语句是如何由“真正的”Sqlite(即客户机或C库)执行的。在

相关问题 更多 >