1

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_query("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?

4

2 回答 2

0

您是否将 python 结果打印到标准输出?因为如果您将所有数据打印到标准输出(控制台),我预计会出现这种性能差异。PHP 代码通常在没有标准输出输出到控制台的情况下运行。问题是输出到控制台涉及计算机必须在文本实时滚动窗口时绘制和移动数百万像素。

如果不是这样,那么看起来 MySQLdb 确实很糟糕,您应该输入更多信息,以便人们可以找到解决方法来为您优化事情。

于 2011-11-25T19:28:35.453 回答
-2

您可以使用SQLAlchemy模块。您可以优化您的查询,以获得更好的内存和 CPU 使用率。

参考http ://www.sqlalchemy.org/features.html

于 2011-11-25T19:11:08.790 回答