Skip to content

Commit a6257d8

Browse files
Fix #20226: Revert all PR for "Data providers perform unnecessary COUNT queries that negatively affect performance"
1 parent 5f5ef64 commit a6257d8

File tree

10 files changed

+86
-158
lines changed

10 files changed

+86
-158
lines changed

composer.lock

Lines changed: 36 additions & 34 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

framework/CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ Yii Framework 2 Change Log
77
- Bug #20195: Do not set non abstract values into `ColumnSchema->type` on MSSQL version less then 2017 (axeltomasson)
88
- Bug #16116: Codeception: oci does not support enabling/disabling integrity check (@terabytesoftw)
99
- Bug #20191: Fix `ActiveRecord::getDirtyAttributes()` for JSON columns with multi-dimensional array values (brandonkelly)
10-
- Bug #20175: Fix bad result for pagination when used with GridView (@lav45)
1110
- Bug #20211: Add acceptable parameters to `MaskedInput::init()` method (alxlnk)
11+
- Bug #20226: Revert all PR for "Data providers perform unnecessary COUNT queries that negatively affect performance" (@terabytesoftw)
1212

1313

1414
2.0.50 May 30, 2024

framework/data/ActiveDataProvider.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ protected function prepareModels()
102102
}
103103
$query = clone $this->query;
104104
if (($pagination = $this->getPagination()) !== false) {
105+
$pagination->totalCount = $this->getTotalCount();
105106
if ($pagination->totalCount === 0) {
106107
return [];
107108
}
@@ -110,6 +111,7 @@ protected function prepareModels()
110111
if (($sort = $this->getSort()) !== false) {
111112
$query->addOrderBy($sort->getOrders());
112113
}
114+
113115
return $query->all($this->db);
114116
}
115117

@@ -127,6 +129,7 @@ protected function prepareKeys($models)
127129
$keys[] = call_user_func($this->key, $model);
128130
}
129131
}
132+
130133
return $keys;
131134
} elseif ($this->query instanceof ActiveQueryInterface) {
132135
/* @var $class \yii\db\ActiveRecordInterface */
@@ -146,8 +149,10 @@ protected function prepareKeys($models)
146149
$keys[] = $kk;
147150
}
148151
}
152+
149153
return $keys;
150154
}
155+
151156
return array_keys($models);
152157
}
153158

@@ -192,6 +197,7 @@ public function __clone()
192197
if (is_object($this->query)) {
193198
$this->query = clone $this->query;
194199
}
200+
195201
parent::__clone();
196202
}
197203
}

framework/data/ArrayDataProvider.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,14 @@ protected function prepareModels()
8686
$models = $this->sortModels($models, $sort);
8787
}
8888

89-
$pagination = $this->getPagination();
90-
if ($pagination !== false && $pagination->getPageSize() > 0) {
91-
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
89+
if (($pagination = $this->getPagination()) !== false) {
90+
$pagination->totalCount = $this->getTotalCount();
91+
92+
if ($pagination->getPageSize() > 0) {
93+
$models = array_slice($models, $pagination->getOffset(), $pagination->getLimit(), true);
94+
}
9295
}
96+
9397
return $models;
9498
}
9599

framework/data/BaseDataProvider.php

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,13 +164,12 @@ public function getCount()
164164
*/
165165
public function getTotalCount()
166166
{
167-
if ($this->_pagination === false) {
167+
if ($this->getPagination() === false) {
168168
return $this->getCount();
169+
} elseif ($this->_totalCount === null) {
170+
$this->_totalCount = $this->prepareTotalCount();
169171
}
170-
if ($this->_totalCount !== null) {
171-
return (int)$this->_totalCount;
172-
}
173-
return $this->prepareTotalCount();
172+
return $this->_totalCount;
174173
}
175174

176175
/**
@@ -193,6 +192,7 @@ public function getPagination()
193192
if ($this->_pagination === null) {
194193
$this->setPagination([]);
195194
}
195+
196196
return $this->_pagination;
197197
}
198198

@@ -216,15 +216,9 @@ public function setPagination($value)
216216
$config['pageParam'] = $this->id . '-page';
217217
$config['pageSizeParam'] = $this->id . '-per-page';
218218
}
219-
$value = Yii::createObject(array_merge($config, $value));
220-
}
221-
if ($value instanceof Pagination) {
222-
$value->setTotalCount(function () {
223-
return $this->getTotalCount();
224-
});
219+
$this->_pagination = Yii::createObject(array_merge($config, $value));
220+
} elseif ($value instanceof Pagination || $value === false) {
225221
$this->_pagination = $value;
226-
} elseif ($value === false) {
227-
$this->_pagination = false;
228222
} else {
229223
throw new InvalidArgumentException('Only Pagination instance, configuration array or false is allowed.');
230224
}

0 commit comments

Comments
 (0)