Skip to content

Commit 0ff4796

Browse files
committed
Run scalar element queries on the built subquery
Avoids unnecessary overhead when we’re not actually fetching the full element results, or applying ORDER BY, etc.
1 parent 3efa06d commit 0ff4796

2 files changed

Lines changed: 39 additions & 2 deletions

File tree

CHANGELOG-WIP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@
1212

1313
### System
1414
- Craft now calls `setlocale()` based on the target language, so that `SORT_LOCALE_STRING` behaves as expected. ([#14509](https://github.com/craftcms/cms/issues/14509), [#14513](https://github.com/craftcms/cms/pull/14513))
15+
- Improved the performance of scalar element queries like `count()`.

src/elements/db/ElementQuery.php

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1569,11 +1569,47 @@ public function afterPopulate(array $elements): array
15691569
public function count($q = '*', $db = null): bool|int|string|null
15701570
{
15711571
// Cached?
1572-
if (($cachedResult = $this->getCachedResult()) !== null) {
1572+
if (
1573+
!$this->offset &
1574+
!$this->limit &
1575+
($cachedResult = $this->getCachedResult()) !== null
1576+
) {
15731577
return count($cachedResult);
15741578
}
15751579

1576-
return parent::count($q, $db) ?: 0;
1580+
return $this->prepareSubquery()->count($q, $db) ?: 0;
1581+
}
1582+
1583+
/**
1584+
* @inheritdoc
1585+
*/
1586+
public function sum($q, $db = null)
1587+
{
1588+
return $this->prepareSubquery()->sum($q, $db);
1589+
}
1590+
1591+
/**
1592+
* @inheritdoc
1593+
*/
1594+
public function average($q, $db = null)
1595+
{
1596+
return $this->prepareSubquery()->average($q, $db);
1597+
}
1598+
1599+
/**
1600+
* @inheritdoc
1601+
*/
1602+
public function min($q, $db = null)
1603+
{
1604+
return $this->prepareSubquery()->min($q, $db);
1605+
}
1606+
1607+
/**
1608+
* @inheritdoc
1609+
*/
1610+
public function max($q, $db = null)
1611+
{
1612+
return $this->prepareSubquery()->max($q, $db);
15771613
}
15781614

15791615
/**

0 commit comments

Comments
 (0)