简体   繁体   English

Python MySQL性能:在mysql命令行中快速运行,但使用cursor.execute则运行缓慢

[英]Python MySQL Performance: Runs fast in mysql command line, but slow with cursor.execute

I'm writing a script for exporting some data. 我正在编写用于导出一些数据的脚本。

Some details about the environment: 有关环境的一些细节:

  • The project is Django based 该项目基于Django
  • I'm using raw/custom SQL for the export 我正在使用原始/自定义SQL进行导出
  • The database engine is MySQL. 数据库引擎是MySQL。
  • The database and code are on the same box.- 数据库和代码在同一框中。

Details about the SQL: 有关SQL的详细信息:

  • A bunch of inner joins 一堆内部联接
  • A bunch of columns selected, some with a basic multiplication calculation. 选择了一堆列,其中一些具有基本的乘法计算。
  • The sql result has about 55K rows sql结果大约有55K行

When I run the SQL statement in the mysql command line, it takes 3-4 seconds 当我在mysql命令行中运行SQL语句时,它需要3-4秒

When I run the SQL in my python script the line cursor.execute(sql, [id]) takes over 60 seconds. 当我在python脚本中运行SQL时,cursor.execute(sql,[id])行需要60秒钟以上。

Any ideas on what might be causing this? 关于什么可能导致此的任何想法?

Two ideas: 两个想法:

  1. MySQL may have query caching enabled, which makes it difficult to get accurate timing when you run the same query repeatedly. MySQL可能启用了查询缓存,这使得在重复运行相同查询时很难获得准确的时间。 Try changing the ID in your query to make sure that it really does run in 3-4 seconds consistently. 尝试更改查询中的ID,以确保它确实能够在3-4秒内持续运行。

  2. Try using strace on the python process to see what it is doing during this time. 尝试在python进程上使用strace以查看其在这段时间内的运行情况。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 调用MySQL cursor.execute()(Python驱动程序)挂起 - Call to MySQL cursor.execute() (Python driver) hangs 带有 MySQL UPDATE 的 Python cursor.execute() 导致语法错误 - Python cursor.execute() with MySQL UPDATE causes syntax error python-mysql cursor.execute失败,访问被拒绝错误 - python-mysql cursor.execute failing with access denied error Python mysql.connector-cursor.execute(“ WHERE…)返回NoneType - Python mysql.connector - cursor.execute("WHERE … ) returns NoneType 使用 '.format()' vs. '%s' 在 cursor.execute() 中用于 mysql JSON 字段,使用 Python mysql.connector, - Use of '.format()' vs. '%s' in cursor.execute() for mysql JSON field, with Python mysql.connector, MySQL为什么cursor.execute(sql,multi = True)不起作用,但是2 cursor.execute(sql)起作用? - MySQL why cursor.execute(sql, multi=True) does not work but 2 cursor.execute(sql) works? Cursor.execute 数据在python中添加单引号并中断包含IN(1,2)语句的查询 - Cursor.execute data adds single quotes marks and breaks query that contains IN(1,2) statement with mysql in python 在python中使用mysql.connector时在cursor.execute中出错 - Getting Error in cursor.execute while using mysql.connector in python 使用Python和mySQL(以及Windows作为OS),cursor.execute()未返回任何结果,但正在连接 - Using Python and mySQL (and windows as OS), cursor.execute() is returning no results but it is connecting 在cursor.execute(query)mysql.connector Python中使用multi = True - Use multi=True in cursor.execute(query) mysql.connector Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM