rolek
October 18, 2023, 1:12pm
1
Hi!
In my application, there is one query that I need to execute unbuffered, because it returns too many results to fit in memory. I would like to execute this one query unbuffered, while leaving the rest of the queries as buffered. I’m using laminas-db with PDO to MySQL.
There doesn’t seem to be a way to run
setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
on the PDO resource after the database connection is established, and I don’t want to configure the entire connection as unbuffered. Ideally, I would like to avoid using two database connections as well.
Is there any way to do this?
Thanks in advance, Roel
Hello and welcome to our forums!
This means to fetch the resource.
Here is an example for an adapter:
$adapter = new Laminas\Db\Adapter\Adapter([
'driver' => 'Pdo_Sqlite',
'database' => 'sqlite::memory:',
]);
/** @var PDO $resource */
$resource = $adapter->getDriver()->getConnection()->getResource();
$resource->setAttribute(…, …);
Database abstraction layer, SQL abstraction, result set abstraction, and RowDataGateway and TableDataGateway implementations
Here is an example for a table gateway:
$tableGateway = new Laminas\Db\TableGateway\TableGateway(
'table_name',
new Laminas\Db\Adapter\Adapter([
'driver' => 'Pdo_Sqlite',
'database' => 'sqlite::memory:',
])
);
/** @var PDO $resource */
$resource = $tableGateway->getAdapter()->getDriver()->getConnection()->getResource();
$resource->setAttribute(…, …);
Database abstraction layer, SQL abstraction, result set abstraction, and RowDataGateway and TableDataGateway implementations
rolek
October 18, 2023, 1:48pm
3
Thanks, this should definitely get me going!
rolek
October 18, 2023, 2:09pm
4
A quick follow-up question: Can I initialize a ResultSet with an unbuffered result to process it?
Like this, I mean:
$result = $statement->execute();
$resultSet = new ResultSet(ResultSet::TYPE_ARRAY);
$resultSet->initialize($result);
Or does this mean that the entire result is read into the ResultSet at once?
Thanks!
rolek
October 18, 2023, 2:39pm
5
And answering my own question: Yes, this works.