From b7ea9a9c7f39c52bcd5af0ee30a95c76a13a3546 Mon Sep 17 00:00:00 2001 From: Constantine Karnaukhov Date: Mon, 6 Dec 2021 16:59:34 +0400 Subject: [PATCH] refactor: use count(*) for better performance when possible --- src/Select/RootLoader.php | 3 +-- tests/ORM/SelectorTest.php | 6 +++--- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/Select/RootLoader.php b/src/Select/RootLoader.php index 3e297ab55..4d8a90764 100644 --- a/src/Select/RootLoader.php +++ b/src/Select/RootLoader.php @@ -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 '*'; } /** diff --git a/tests/ORM/SelectorTest.php b/tests/ORM/SelectorTest.php index 06e08bfb4..fd6756ad6 100644 --- a/tests/ORM/SelectorTest.php +++ b/tests/ORM/SelectorTest.php @@ -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(