Skip to content

Instantly share code, notes, and snippets.

@webdevilopers
Created February 13, 2017 10:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webdevilopers/a0c7e16a416fa6d99ee8064b1f8f887b to your computer and use it in GitHub Desktop.
Save webdevilopers/a0c7e16a416fa6d99ee8064b1f8f887b to your computer and use it in GitHub Desktop.
OutOfMemoryException in PDOConnection using Doctrine iterate result
<?php
// 7041 rows with content being BLOB content - total table size ~ 2.4 GB
$instructionRepository = $this->getDoctrine()->getRepository('PlusquamContractBundle:Instruction');
$instructionQueryBuilder = $instructionRepository->createQueryBuilder('i')
->where('i.content IS NOT NULL')
->orderBy('i.id', 'desc');
$instructions = $instructionQueryBuilder->getQuery()->iterate();
?>
@webdevilopers
Copy link
Author

I tried to use raw SQL too:

        $sql = "SELECT i.id, i.content FROM instructions AS i";

        $em = $this->getDoctrine()->getManager();
        $stmt = $em->getConnection()->prepare($sql);
        $stmt->execute();

Againg: memory exhausted!

@mablae
Copy link

mablae commented Feb 13, 2017

@webdevilopers
Copy link
Author

You made my day @mablae!

Indeed changing the USE_BUFFERED_QUERY attribute on PDO prevents loading anything into memory:

$pdo = $this->getDoctrine()->getManager()->getConnection()->getWrappedConnection();
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

$sql = "SELECT i.id FROM instructions AS i WHERE i.content IS NOT NULL";

$em = $this->getDoctrine()->getManager();
$stmt = $em->getConnection()->prepare($sql);
$stmt->execute();
$instructionIds = $stmt->fetchAll();

I also tried using the repository too. But it looks like the Manager then does not recognize my attribute change then?

$pdo = $this->getDoctrine()->getManager()->getConnection()->getWrappedConnection();
$pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);

$instructionQueryBuilder = $instructionRepository->createQueryBuilder('i')
    ->where('i.content IS NOT NULL')
    ->orderBy('i.id', 'desc');

@mablae
Copy link

mablae commented Feb 13, 2017

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment