Home > Symfony > Speed up Symfony Queries using Result Sets

Speed up Symfony Queries using Result Sets

Hydration is the process used by the Symfony ORM to populate objects with the results of a database query. By making a call to the ::doSelect() method on a peer class Symfony automatically hydrates the resulting objects.  In Symfony 1.0 the hydration process was slow and it is much faster in Symfony 1.2. Still, in some cases you may want to bypass hydration. Consider a simple example. Suppose we want to get the title of some news articles.

Instead of using

$c = new Criteria();
$c->add(ArticlePeer::SECTION_ID, Article::SPORTS);
$articles = ArticlePeer::doSelect($c);

$names = array();

foreach($articles as $article)
  $names[$article->getId()] = $article->getName();

Instead we could use

// Symfony 1.0
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(ArticlePeer::NAME);
$c->add(ArticlePeer::SECTION_ID, Article::SPORTS);
$rs = ArticlePeer::doSelectRS($c);

$names = array();

while($rs->next())
  $names[] = $rs->getString(2);

// Symfony 1.2
$c = new Criteria();
$c->clearSelectColumns();
$c->addSelectColumn(ArticlePeer::NAME);
$c->add(ArticlePeer::SECTION_ID, Article::SPORTS);
$rs = ArticlePeer::doSelectStmt($c);

$names = array();

while($res = $stmt->fetchColumn(1))
  $names[] = $res;

The second method is slightly more efficient because it only returns the required columns, conserving memory and does not hydrate the Article objects.

Categories: Symfony Tags: ,
  1. No comments yet.
  1. No trackbacks yet.