Skip to content

Commit

Permalink
refactor: use count(*) for better performance when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
hustlahusky committed Dec 6, 2021
1 parent e680b3f commit b7ea9a9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/Select/RootLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,10 @@ public function getPK(): string
public function getCountField(): string
{
if ($this->isDataDuplicationPossible()) {
// @tuneyourserver solves the issue with counting on queries with joins.
return sprintf('DISTINCT(%s)', $this->getPK());
}

return $this->getPK();
return '*';
}

/**
Expand Down
6 changes: 3 additions & 3 deletions tests/ORM/SelectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,9 +218,9 @@ public function testCountField(): void
$pk = \sprintf('%s.%s', $role, 'id');
$distinct = \sprintf('DISTINCT(%s)', $pk);

$this->assertSame($pk, (new RootLoader($this->orm, $role))->getCountField());
$this->assertSame($pk, $this->joinRelation(new RootLoader($this->orm, $role), 'profile')->getCountField());
$this->assertSame($pk, $this->joinRelation(new RootLoader($this->orm, $role), 'lastComment', [
$this->assertSame('*', (new RootLoader($this->orm, $role))->getCountField());
$this->assertSame('*', $this->joinRelation(new RootLoader($this->orm, $role), 'profile')->getCountField());
$this->assertSame('*', $this->joinRelation(new RootLoader($this->orm, $role), 'lastComment', [
'method' => Select\AbstractLoader::LEFT_JOIN,
])->getCountField());
$this->assertSame(
Expand Down

0 comments on commit b7ea9a9

Please sign in to comment.