Python's MySQLdb slow! using 'SSCurso'r vs PHP's 'mysql_fetch_assoc'
I need to go over an entire table in Python. I am using MySQLdb via 'SSCursor' and it is much slower than PHP's stuff.
PHP 5.3.5
$result = mysql_quer开发者_高级运维y("SELECT * FROM anytable");
while($row = mysql_fetch_assoc($result)){
#do stuff
}
Python2.7
cursor.execute("SELECT * FROM anytable")
for row in cursor:
pass
RESULTS
PHP: 5 seconds CPU around 35%, Python: 25 seconds CPU 100%
Does MySQLdb just suck or am I doing something wrong? If MySQLdb, what can I use instead for better performance/same functionality?
Are you printing python results to stdout? Because I'd expect around this kind of difference in performance if you ARE printing all the data to stdout (the console). PHP code normally is run without stdout output to the console. The problem is that outputing to the console involves the computer having to draw and move around milions of pixels as text scrolls by the window in real time.
If it is not that, than it looks like MySQLdb is indeed sucking, and you should put in more information so people could find a work around to optmize things for you.
You can use SQLAlchemy module. You can optimize your quires, for better memory and cpu usage.
Reference: http://www.sqlalchemy.org/features.html
精彩评论