From e5a9b9e9b5a7a188bf5f69d625aafb25a3548a55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Karel=20H=C3=A1k?= Date: Sat, 7 May 2016 09:22:33 +0200 Subject: [PATCH] Table: performance, prevents multiple parse of query parts by regexp (#131) --- src/Database/Table/GroupedSelection.php | 2 +- src/Database/Table/Selection.php | 2 +- src/Database/Table/SqlBuilder.php | 33 +++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) diff --git a/src/Database/Table/GroupedSelection.php b/src/Database/Table/GroupedSelection.php index 674536fca..ffe857e3c 100644 --- a/src/Database/Table/GroupedSelection.php +++ b/src/Database/Table/GroupedSelection.php @@ -87,7 +87,7 @@ public function order($columns, ...$params) public function aggregation($function) { - $aggregation = & $this->getRefTable($refPath)->aggregation[$refPath . $function . $this->getSql() . json_encode($this->sqlBuilder->getParameters())]; + $aggregation = & $this->getRefTable($refPath)->aggregation[$refPath . $function . $this->sqlBuilder->getSelectQueryHash($this->getPreviousAccessedColumns())]; if ($aggregation === NULL) { $aggregation = []; diff --git a/src/Database/Table/Selection.php b/src/Database/Table/Selection.php index 6fd24d114..f9c6213f0 100644 --- a/src/Database/Table/Selection.php +++ b/src/Database/Table/Selection.php @@ -677,7 +677,7 @@ protected function getSpecificCacheKey() return $this->specificCacheKey; } - return $this->specificCacheKey = md5($this->getSql() . json_encode($this->sqlBuilder->getParameters())); + return $this->specificCacheKey = $this->sqlBuilder->getSelectQueryHash($this->getPreviousAccessedColumns()); } diff --git a/src/Database/Table/SqlBuilder.php b/src/Database/Table/SqlBuilder.php index e84c1da61..0d1f5a8ec 100644 --- a/src/Database/Table/SqlBuilder.php +++ b/src/Database/Table/SqlBuilder.php @@ -136,6 +136,39 @@ public function buildDeleteQuery() } + /** + * Returns select query hash for caching. + * @return string + */ + public function getSelectQueryHash($columns = NULL) + { + $parts = [ + 'delimitedTable' => $this->delimitedTable, + 'queryCondition' => $this->buildConditions(), + 'queryEnd' => $this->buildQueryEnd(), + $this->aliases, + $this->limit, $this->offset, + ]; + if ($this->select) { + $parts[] = $this->select; + } elseif ($columns) { + $parts[] = [$this->delimitedTable, $columns]; + } elseif ($this->group && !$this->driver->isSupported(ISupplementalDriver::SUPPORT_SELECT_UNGROUPED_COLUMNS)) { + $parts[] = [$this->group]; + } else { + $parts[] = "{$this->delimitedTable}.*"; + } + return $this->getConditionHash(json_encode($parts), array_merge( + $this->parameters['select'], + $this->parameters['joinCondition'] ? call_user_func_array('array_merge', $this->parameters['joinCondition']) : [], + $this->parameters['where'], + $this->parameters['group'], + $this->parameters['having'], + $this->parameters['order'] + )); + } + + /** * Returns SQL query. * @param string list of columns