From: Aaron Schulz Date: Tue, 22 Mar 2011 13:11:28 +0000 (+0000) Subject: * Avoid adding DISTINCT if possible X-Git-Tag: 1.31.0-rc.0~31252 X-Git-Url: https://git.cyclocoop.org/%28%28?a=commitdiff_plain;h=45191d2a50d5559025ddc6f09537deb8c75f3b5e;p=lhc%2Fweb%2Fwiklou.git * Avoid adding DISTINCT if possible * Added comments --- diff --git a/includes/LogEventsList.php b/includes/LogEventsList.php index 9fd82848eb..2d918ed25f 100644 --- a/includes/LogEventsList.php +++ b/includes/LogEventsList.php @@ -938,12 +938,21 @@ class LogPager extends ReverseChronologicalPager { $this->mConds[] = 'user_id = log_user'; $index = array(); $options = array(); - # Add log_search table if there are conditions on it - if( array_key_exists('ls_field',$this->mConds) ) { + # Add log_search table if there are conditions on it. + # This filters the results to only include log rows that have + # log_search records with the specified ls_field and ls_value values. + if( array_key_exists( 'ls_field', $this->mConds ) ) { $tables[] = 'log_search'; $index['log_search'] = 'ls_field_val'; $index['logging'] = 'PRIMARY'; - $options[] = 'DISTINCT'; + if ( !$this->hasEqualsClause( 'ls_field' ) + || !$this->hasEqualsClause( 'ls_value' ) ) + { + # Since (ls_field,ls_value,ls_logid) is unique, if the condition is + # to match a specific (ls_field,ls_value) tuple, then there will be + # no duplicate log rows. Otherwise, we need to remove the duplicates. + $options[] = 'DISTINCT'; + } # Avoid usage of the wrong index by limiting # the choices of available indexes. This mainly # avoids site-breaking filesorts. @@ -967,7 +976,7 @@ class LogPager extends ReverseChronologicalPager { 'conds' => $this->mConds, 'options' => $options, 'join_conds' => array( - 'user' => array( 'INNER JOIN', 'user_id=log_user' ), + 'user' => array( 'INNER JOIN', 'user_id=log_user' ), 'log_search' => array( 'INNER JOIN', 'ls_log_id=log_id' ) ) ); @@ -977,6 +986,14 @@ class LogPager extends ReverseChronologicalPager { return $info; } + // Checks if $this->mConds has $field matched to a *single* value + protected function hasEqualsClause( $field ) { + return ( + array_key_exists( $field, $this->mConds ) && + ( !is_array( $this->mConds[$field] ) || count( $this->mConds[$field] ) == 1 ) + ); + } + function getIndexField() { return 'log_timestamp'; }