Optimization of SQL queries: fixed two issues (SELECT to track object linked to... and SELECT ExternalUser)

SVN:trunk[2496]
This commit is contained in:
Romain Quetiez
2012-12-03 17:00:38 +00:00
parent fe27ee4f22
commit 90bc24d5c0
4 changed files with 56 additions and 39 deletions

View File

@@ -597,12 +597,12 @@ class SQLQuery
return $this->m_sTableAlias;
}
public function OptimizeJoins($aUsedTables = null)
public function OptimizeJoins($aUsedTables, $bTopCall = true)
{
if (is_null($aUsedTables))
if ($bTopCall)
{
// Top call: build the list of tables absolutely required to perform the query
$aUsedTables = $this->CollectUsedTables();
// Top call: complete the list of tables absolutely required to perform the right query
$this->CollectUsedTables($aUsedTables);
}
$aToDiscard = array();
@@ -610,7 +610,7 @@ class SQLQuery
{
$oSQLQuery = $aJoinInfo["select"];
$sTableAlias = $oSQLQuery->GetTableAlias();
if ($oSQLQuery->OptimizeJoins($aUsedTables) && !array_key_exists($sTableAlias, $aUsedTables))
if ($oSQLQuery->OptimizeJoins($aUsedTables, false) && !array_key_exists($sTableAlias, $aUsedTables))
{
$aToDiscard[] = $i;
}
@@ -623,29 +623,24 @@ class SQLQuery
return (count($this->m_aJoinSelects) == 0);
}
protected function CollectUsedTables(&$aTables = null)
protected function CollectUsedTables(&$aTables)
{
if (is_null($aTables))
$this->m_oConditionExpr->CollectUsedParents($aTables);
foreach($this->m_aFields as $sFieldAlias => $oField)
{
$aTables = array();
$this->m_oConditionExpr->CollectUsedParents($aTables);
foreach($this->m_aFields as $sFieldAlias => $oField)
$oField->CollectUsedParents($aTables);
}
if ($this->m_aGroupBy)
{
foreach($this->m_aGroupBy as $sAlias => $oExpression)
{
$oField->CollectUsedParents($aTables);
}
if ($this->m_aGroupBy)
{
foreach($this->m_aGroupBy as $sAlias => $oExpression)
{
$oExpression->CollectUsedParents($aTables);
}
}
if (!is_null($this->m_oSelectedIdField))
{
$this->m_oSelectedIdField->CollectUsedParents($aTables);
$oExpression->CollectUsedParents($aTables);
}
}
if (!is_null($this->m_oSelectedIdField))
{
$this->m_oSelectedIdField->CollectUsedParents($aTables);
}
foreach ($this->m_aJoinSelects as $i => $aJoinInfo)
{