python mysql execute()非常

2024-09-28 22:22:45 发布

您现在位置:Python中文网/ 问答频道 /正文

我在我的应用程序中发现了mysql的execute()函数的缓慢性。我设计了一个简单的sql查询来说明这个问题:

SELECT * FROM `cid444_agg_big` c WHERE 1

一。在

^{pr2}$

在mysql shell中运行这个查询,可以得到0.27秒,或者快10倍!!!在

我唯一的想法是返回的数据的大小。这将返回21600个“宽”行。所以有很多数据被发送到python。数据库是本地主机,因此没有网络延迟。在

为什么要花这么长时间?在

更新更多信息

我用php编写了一个类似的脚本:

$c = mysql_connect ( 'localhost', '*****', '****', true );
mysql_select_db ( 'cachedata', $c );

$time_start = microtime_float();
$sql="SELECT * FROM `cid444_agg_big` c WHERE 1";
$q=mysql_query($sql);$c=0;
while($r=mysql_fetch_array($q))
$c++;//do something?
echo "Did ".$c." loops in ".(microtime_float() - $time_start)." seconds\n";


function microtime_float(){//function taken from php.net
  list($usec, $sec) = explode(" ", microtime());
  return ((float)$usec + (float)$sec);
}

打印:

Did 21600 loops in 0.56120800971985 seconds

这将循环所有数据,而不是一次检索所有数据。PHP似乎比python版本快了6倍。。。。在


Tags: 数据fromsqltimemysqlfloatwhereselect
1条回答
网友
1楼 · 发布于 2024-09-28 22:22:45

The default MySQLdb cursor将完整的结果集获取到execute上的客户机,fetchall()只将数据从内存复制到内存。在

如果要将结果集存储在服务器上并按需获取,则应使用SSCursor。在

Cursor:
This is the standard Cursor class that returns rows as tuples and stores the result set in the client.

SSCursor:
This is a Cursor class that returns rows as tuples and stores the result set in the server.

相关问题 更多 >