Skip to content

Commit 26cc742

Browse files
norbedg
authored andcommitted
Table: performance, prevents multiple parse of query parts by regexp (#131)
1 parent 31e1b1c commit 26cc742

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

src/Database/Table/GroupedSelection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function order($columns, ...$params)
8787

8888
public function aggregation($function)
8989
{
90-
$aggregation = & $this->getRefTable($refPath)->aggregation[$refPath . $function . $this->getSql() . json_encode($this->sqlBuilder->getParameters())];
90+
$aggregation = & $this->getRefTable($refPath)->aggregation[$refPath . $function . $this->sqlBuilder->getSelectQueryHash($this->getPreviousAccessedColumns())];
9191

9292
if ($aggregation === NULL) {
9393
$aggregation = [];

src/Database/Table/Selection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -677,7 +677,7 @@ protected function getSpecificCacheKey()
677677
return $this->specificCacheKey;
678678
}
679679

680-
return $this->specificCacheKey = md5($this->getSql() . json_encode($this->sqlBuilder->getParameters()));
680+
return $this->specificCacheKey = $this->sqlBuilder->getSelectQueryHash($this->getPreviousAccessedColumns());
681681
}
682682

683683

src/Database/Table/SqlBuilder.php

+33
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,39 @@ public function buildDeleteQuery()
136136
}
137137

138138

139+
/**
140+
* Returns select query hash for caching.
141+
* @return string
142+
*/
143+
public function getSelectQueryHash($columns = NULL)
144+
{
145+
$parts = [
146+
'delimitedTable' => $this->delimitedTable,
147+
'queryCondition' => $this->buildConditions(),
148+
'queryEnd' => $this->buildQueryEnd(),
149+
$this->aliases,
150+
$this->limit, $this->offset,
151+
];
152+
if ($this->select) {
153+
$parts[] = $this->select;
154+
} elseif ($columns) {
155+
$parts[] = [$this->delimitedTable, $columns];
156+
} elseif ($this->group && !$this->driver->isSupported(ISupplementalDriver::SUPPORT_SELECT_UNGROUPED_COLUMNS)) {
157+
$parts[] = [$this->group];
158+
} else {
159+
$parts[] = "{$this->delimitedTable}.*";
160+
}
161+
return $this->getConditionHash(json_encode($parts), array_merge(
162+
$this->parameters['select'],
163+
$this->parameters['joinCondition'] ? array_merge(...$this->parameters['joinCondition']) : [],
164+
$this->parameters['where'],
165+
$this->parameters['group'],
166+
$this->parameters['having'],
167+
$this->parameters['order']
168+
));
169+
}
170+
171+
139172
/**
140173
* Returns SQL query.
141174
* @param string list of columns

0 commit comments

Comments
 (0)