没有什么可补充的,如果你看到这个问题的标题。
我有一个从 MySQL 表中检索单行的查询,我对特定的列感兴趣,它是一个 BLOB。我希望 PHP 将它写入输出缓冲区,而不是将 ~500 KB 存储到一个字符串中(而且我不确定这是否是二进制安全的)。
PDOStatement 函数如下:
string PDOStatement::fetchColumn ([ int $column_number = 0 ] )
别帮我
你能至少给我一个方向吗?提前致谢。
P.S.:我知道在数据库表中存储 ~500 KB 的东西不好,但这不是我的选择,我只能坚持下去。
最佳答案
我坚信使用 Doctrine 进行批处理或使用 MySQL(PDO 或 mysqli)进行任何类型的迭代只是一种幻想。
@dimitri-k 提供了很好的解释,尤其是关于工作单元的解释。问题是错误的引导:“$query->iterate()”,它并没有真正遍历数据源。它只是一个\Traversable 包装器,围绕着已经完全获取 的数据源。
一个例子表明,即使从图片中完全删除 Doctrine 抽象层,我们仍然会遇到内存问题:
echo 'Starting with memory usage: ' . memory_get_usage(true) / 1024 / 1024 . " MB \n";
$pdo = new \PDO("mysql:dbname=DBNAME;host=HOST", "USER", "PW");
$stmt = $pdo->prepare('SELECT * FROM my_big_table LIMIT 100000');
$stmt->execute();
while ($rawCampaign = $stmt->fetch()) {
// echo $rawCampaign['id'] . "\n";
}
echo 'Ending with memory usage: ' . memory_get_usage(true) / 1024 / 1024 . " MB \n";
输出:
Starting with memory usage: 6 MB
Ending with memory usage: 109.46875 MB
在这里,令人失望的 getIterator() 方法:
namespace Doctrine\DBAL\Driver\Mysqli\MysqliStatement
/**
* {@inheritdoc}
*/
public function getIterator()
{
$data = $this->fetchAll();
return new \ArrayIterator($data);
}
您可以使用我的小库来实际上使用 PHP Doctrine 或 DQL 或纯 SQL 流式传输繁重的表。但是你觉得合适:https://github.com/EnchanterIO/remote-collection-stream
关于php - 有什么办法可以将 'stream' 一个 PDO 查询结果 'into' 输出缓冲区,而不是将它存储到一个字符串中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7571892/