Python web2py上速度极慢的原始SQL查询
db.executesql()返回结果需要30秒,尽管在MySQL控制台或web2py调试控制台或web2py shell上使用相同的代码尝试相同的查询需要0.5-1秒Python web2py上速度极慢的原始SQL查询,python,mysql,performance,web2py,Python,Mysql,Performance,Web2py,db.executesql()返回结果需要30秒,尽管在MySQL控制台或web2py调试控制台或web2py shell上使用相同的代码尝试相同的查询需要0.5-1秒 dbs._timings #From the first test below [('SELECT 1;', 0.016000032424926758), #web2py or driver stuff, I didn't execute it. ('SET FOREIGN_KEY_CHECKS=1;', 0.0039
dbs._timings #From the first test below
[('SELECT 1;', 0.016000032424926758), #web2py or driver stuff, I didn't execute it.
('SET FOREIGN_KEY_CHECKS=1;', 0.003999948501586914), #web2py or driver stuff, I didn't execute it.
("SET sql_mode='NO_BACKSLASH_ESCAPES';", 0.00800013542175293), #web2py or driver stuff, I didn't execute it.
("SELECT TIMESTAMPDIFF(...) AS 'duration',\n
TIMESTAMPDIFF(...)\n
- INTERVAL 1 HOUR, TIMESTAMP(...)\n
+ INTERVAL CAST(...) AS 'timediff',\n
ris.ODATE as 'date',\n
CONCAT(...) as 'service'\n
FROM ... AS ris\n
JOIN ... as sd on ris.... = sd....\n
WHERE ris.... != '0000-00-00 00:00:00'\n
and ris.... != '0000-00-00 00:00:00'\n
and ris.... >= '2010-8-15'\n
and ris.... <= '2014-8-22'", 32.0460000038147), # Slow query/code, executed on page view.
("SELECT TIMESTAMPDIFF(...) AS 'duration',\n
TIMESTAMPDIFF(...)\n
- INTERVAL 1 HOUR, TIMESTAMP(...)\n
+ INTERVAL CAST(...) AS 'timediff',\n
ris.ODATE as 'date',\n
CONCAT(...) as 'service'\n
FROM ... AS ris\n
JOIN ... as sd on ris.... = sd....\n
WHERE ris.... != '0000-00-00 00:00:00'\n
and ris.... != '0000-00-00 00:00:00'\n
and ris.... >= '2010-8-15'\n
and ris.... <= '2014-8-22'", 0.6069998741149902) #Same query/code, exec. on the debug console]
如果您在第一次查询之后立即运行调试,它将不会有用,因为它可能使用内部mysql缓存。尝试安装phpmyadmin或其他允许您轻松运行配置文件或解释查询的工具。在那里,您将发现它真正执行的操作类型,您可以与我们分享它,以便更好地了解瓶颈。
... #Irrelevant part of the code
raw_data = dbs.executesql(query, as_dict=True) #Takes 30 seconds to return a result
from gluon.debug import dbg
dbg.set_trace() #After this line, I'm on the debug console
raw_data = dbs.executesql(query, as_dict=True) #Takes <1 second to return a result
dbs._timings #print
... #Irrelevant part of the code
from gluon.debug import dbg
dbg.set_trace() #After this line, I'm on the debug console
raw_data = dbs.executesql(query, as_dict=True) #Takes <1 second to return a result
... #Irrelevant part of the code
raw_data = dbs.executesql(query, as_dict=True) #Takes 7 seconds to return a result
raw_data = dbs.executesql(query, as_dict=True) #Takes 7 seconds to return a result
raw_data = dbs.executesql(query, as_dict=True) #Takes 7 seconds to return a result
from gluon.debug import dbg
dbg.set_trace() #After this line, I'm on the debug console
raw_data = dbs.executesql(query, as_dict=True) #Takes <1 second to return a result
/* Affected rows: 0 Found rows: 7,391 Warnings: 0 Duration for 1 query: 0.000 sec. (+ 0.078 sec. network) */