我是数据库新手,我正在尝试在Flask-SQLalchemy(Python 2.7)应用程序中加快查询速度。我已经对代码进行了分析,但我对如何解释它感到很遗憾。我会喜欢关于这个主题的更多一般提示,但我的具体问题是:" string_decoder"查询的执行时间似乎非常昂贵,我假设它正在将字符串转换为unicode,以便它可以以unicode格式返回它们。
259619 function calls (259567 primitive calls) in 1.108 seconds
Ordered by: cumulative time
ncalls tottime percall cumtime percall filename:lineno(function)
1 0.007 0.007 1.108 1.108 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py:2235(all)
5947 0.019 0.000 0.651 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py:27(instances)
5946 0.116 0.000 0.515 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py:323(_instance)
1 0.000 0.000 0.449 0.449 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py:2348(__iter__)
1 0.000 0.000 0.448 0.448 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/query.py:2362(_execute_and_instances)
1 0.000 0.000 0.448 0.448 /usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py:597(execute)
1 0.000 0.000 0.448 0.448 /usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py:727(_execute_clauseelement)
1 0.000 0.000 0.446 0.446 /usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py:812(_execute_context)
1 0.000 0.000 0.445 0.445 /usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py:323(do_execute)
1 0.000 0.000 0.445 0.445 /usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py:164(execute)
1 0.000 0.000 0.445 0.445 /usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py:343(_query)
1 0.000 0.000 0.427 0.427 /usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py:348(_post_get_result)
1 0.000 0.000 0.427 0.427 /usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py:314(_fetch_row)
1 0.068 0.068 0.427 0.427 {built-in method fetch_row}
5946 0.092 0.000 0.231 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/loading.py:280(populate_state)
12547 0.040 0.000 0.208 0.000 /usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py:206(string_decoder)
12547 0.053 0.000 0.168 0.000 {method 'decode' of 'str' objects}
5946 0.121 0.000 0.152 0.000 /usr/local/lib/python2.7/dist-packages/MySQLdb/times.py:44(DateTime_or_None)
12547 0.064 0.000 0.115 0.000 /usr/lib/python2.7/encodings/utf_8.py:15(decode)
53514 0.086 0.000 0.086 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/strategies.py:150(fetch_col)
5946 0.038 0.000 0.085 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py:286(new_instance)
1 0.037 0.037 0.070 0.070 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/state.py:505(_commit_all_states)
5946 0.015 0.000 0.052 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/strategies.py:619(reset_for_lazy_callable)
12547 0.051 0.000 0.051 0.000 {_codecs.utf_8_decode}
5946 0.028 0.000 0.037 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/identity.py:107(add)
5946 0.023 0.000 0.037 0.000 /usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/state.py:285(_reset)
17838 0.031 0.000 0.031 0.000 {method 'split' of 'str' objects}
2 0.018 0.009 0.029 0.014 /usr/local/lib/python2.7/dist-packages/sqlalchemy/util/_collections.py:687(unique_list)
5946 0.027 0.000 0.027 0.000 /usr/local/lib/python2.7/dist- packages/sqlalchemy/orm/state.py:44(__init__)
但是,我不需要这样做。我不需要字符串是unicode格式,典型的ASCII或python' str'类型会很好。我无法弄清楚如何摆脱这个,或者为什么我需要它。我可能会遗漏一些东西。
最后,有人可以确认检索6k行所需的总时间超过一秒,真的很慢吗?对我来说,这似乎太长了,但我没有太大的依据。
答案 0 :(得分:1)
推动查询时间的主要原因是您获得了6000行。你真的需要所有6000行的所有列吗?解码时间不到总时间的1/5。试图对其进行微观优化可能是浪费时间。考虑到需要移动并让数据库完成繁重工作的数据可能会更好地利用您的时间。