Use GROUPBY instead of DISTINCT and subqueries when counting

This commit is contained in:
Johannes Meyer 2014-11-13 16:32:20 +01:00
parent f86d3e7636
commit 77f5bc3932
2 changed files with 18 additions and 20 deletions

View File

@ -72,6 +72,12 @@ class DbQuery extends SimpleQuery
parent::init();
}
public function setUseSubqueryCount($useSubqueryCount = true)
{
$this->useSubqueryCount = $useSubqueryCount;
return $this;
}
public function where($condition, $value = null)
{
// $this->count = $this->select = null;
@ -259,7 +265,7 @@ class DbQuery extends SimpleQuery
*/
public function getCountQuery()
{
// TODO: there may be situations where we should clone the "select"
// TODO: there may be situations where we should clone the "select"
$count = $this->dbSelect();
$this->applyFilterSql($count);

View File

@ -69,11 +69,13 @@ class PivotTable
protected function prepareQueries()
{
$this->xAxisQuery = clone $this->baseQuery;
$this->xAxisQuery->distinct();
$this->xAxisQuery->group($this->xAxisColumn);
$this->xAxisQuery->columns(array($this->xAxisColumn));
$this->xAxisQuery->setUseSubqueryCount();
$this->yAxisQuery = clone $this->baseQuery;
$this->yAxisQuery->distinct();
$this->yAxisQuery->group($this->yAxisColumn);
$this->yAxisQuery->columns(array($this->yAxisColumn));
$this->yAxisQuery->setUseSubqueryCount();
return $this;
}
@ -85,24 +87,14 @@ class PivotTable
*/
protected function adjustSorting()
{
$currentOrderColumns = $this->baseQuery->getOrder();
$xAxisOrderColumns = array(array($this->baseQuery->getMappedField($this->xAxisColumn), SimpleQuery::SORT_ASC));
$yAxisOrderColumns = array(array($this->baseQuery->getMappedField($this->yAxisColumn), SimpleQuery::SORT_ASC));
foreach ($currentOrderColumns as $orderInfo) {
if ($orderInfo[0] === $xAxisOrderColumns[0][0]) {
$xAxisOrderColumns[0] = $orderInfo;
} elseif ($orderInfo[0] === $yAxisOrderColumns[0][0]) {
$yAxisOrderColumns[0] = $orderInfo;
} else {
$xAxisOrderColumns[] = $orderInfo;
$yAxisOrderColumns[] = $orderInfo;
}
if (false === $this->xAxisQuery->hasOrder($this->xAxisColumn)) {
$this->xAxisQuery->order($this->xAxisColumn, 'ASC');
}
//TODO: simplify this whole function. No need to care about mapping
// foreach ($xAxisOrderColumns as
// $this->xAxisQuery->setOrder($xAxisOrderColumns);
// $this->yAxisQuery->setOrder($yAxisOrderColumns);
if (false === $this->yAxisQuery->hasOrder($this->yAxisColumn)) {
$this->yAxisQuery->order($this->yAxisColumn, 'ASC');
}
return $this;
}