Skip to content

Commit

Permalink
Remove fields from count() queries when associations are contained.
Browse files Browse the repository at this point in the history
Strip out any fields defined in contained associations as fetching
fields and throwing away the results is wasteful and can lead to slow
downs in larger datasets.

Refs #5955
  • Loading branch information
markstory committed Feb 25, 2015
1 parent 3490cd6 commit 3fcbe7f
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/ORM/Query.php
Expand Up @@ -507,8 +507,16 @@ public function count()
$complex = $complex || count($query->clause('union'));

if (!$complex) {
$cleanContain = [];
foreach ($query->contain() as $alias => $contain) {
unset($contain['fields']);
$cleanContain[$alias] = $contain;
}
$query->contain(null, true);

$statement = $query
->select($count, true)
->contain($cleanContain)
->autoFields(false)
->execute();
} else {
Expand Down

0 comments on commit 3fcbe7f

Please sign in to comment.