Fix tag filtering by adding a FORCE INDEX to the relevant queries
authorAndrew Garrett <werdna@users.mediawiki.org>
Tue, 31 Mar 2009 13:01:05 +0000 (13:01 +0000)
committerAndrew Garrett <werdna@users.mediawiki.org>
Tue, 31 Mar 2009 13:01:05 +0000 (13:01 +0000)
includes/ChangeTags.php
includes/LogEventsList.php
includes/PageHistory.php
includes/specials/SpecialContributions.php
includes/specials/SpecialNewpages.php
includes/specials/SpecialRecentchanges.php
includes/specials/SpecialRecentchangeslinked.php
includes/specials/SpecialWatchlist.php

index 7432086..de804c5 100644 (file)
@@ -90,7 +90,8 @@ class ChangeTags {
         * Handles selecting tags, and filtering.
         * Needs $tables to be set up properly, so we can figure out which join conditions to use.
        */
-       static function modifyDisplayQuery( &$tables, &$fields,  &$conds, &$join_conds, $filter_tag = false ) {
+       static function modifyDisplayQuery( &$tables, &$fields,  &$conds,
+                                                                               &$join_conds, &$options, $filter_tag = false ) {
                global $wgRequest, $wgUseTagFilter;
                
                if ($filter_tag === false) {
@@ -118,6 +119,9 @@ class ChangeTags {
                        // Somebody wants to filter on a tag.
                        // Add an INNER JOIN on change_tag
 
+                       // FORCE INDEX -- change_tags will almost ALWAYS be the correct query plan.
+                       $options['USE INDEX'] = array( 'change_tag' => 'change_tag_tag_id' );
+                       unset( $options['FORCE INDEX'] );
                        $tables[] = 'change_tag';
                        $join_conds['change_tag'] = array( 'INNER JOIN', "ct_$join_cond=$join_cond" );
                        $conds['ct_tag'] = $filter_tag;
@@ -176,4 +180,4 @@ class ChangeTags {
                $wgMemc->set( $key, $emptyTags, 300 );
                return $emptyTags;
        }
-}
\ No newline at end of file
+}
index 9d2de44..f0b2756 100644 (file)
@@ -651,7 +651,7 @@ class LogPager extends ReverseChronologicalPager {
                );
 
                ChangeTags::modifyDisplayQuery( $info['tables'], $info['fields'], $info['conds'],
-                       $info['join_conds'], $this->mTagFilter );
+                       $info['join_conds'], $info['options'], $this->mTagFilter );
 
                return $info;
        }
index 9007187..a137235 100644 (file)
@@ -579,7 +579,12 @@ class PageHistoryPager extends ReverseChronologicalPager {
                        'options' => array( 'USE INDEX' => array('revision' => 'page_timestamp') ),
                        'join_conds' => array( 'tag_summary' => array( 'LEFT JOIN', 'ts_rev_id=rev_id' ) ),
                );
-               ChangeTags::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $this->tagFilter );
+               ChangeTags::modifyDisplayQuery( $queryInfo['tables'],
+                                                                               $queryInfo['fields'],
+                                                                               $queryInfo['conds'],
+                                                                               $queryInfo['join_conds'],
+                                                                               $queryInfo['options'],
+                                                                               $this->tagFilter );
                wfRunHooks( 'PageHistoryPager::getQueryInfo', array( &$this, &$queryInfo ) );
                return $queryInfo;
        }
index 6d4dce6..7be4950 100644 (file)
@@ -412,8 +412,12 @@ class ContribsPager extends ReverseChronologicalPager {
                        'join_conds' => $join_cond
                );
                
-               ChangeTags::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'],
-                       $queryInfo['join_conds'], $this->tagFilter );
+               ChangeTags::modifyDisplayQuery( $queryInfo['tables'],
+                                                                               $queryInfo['fields'],
+                                                                               $queryInfo['conds'],
+                                                                               $queryInfo['join_conds'],
+                                                                               $queryInfo['options'],
+                                                                               $this->tagFilter );
                
                wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
                return $queryInfo;
index 9cf7177..886c41a 100644 (file)
@@ -438,7 +438,12 @@ class NewPagesPager extends ReverseChronologicalPager {
                $fields = array();
 
                ## Modify query for tags
-               ChangeTags::modifyDisplayQuery( $info['tables'], $fields, $info['conds'], $info['join_conds'], $this->opts['tagfilter'] );
+               ChangeTags::modifyDisplayQuery( $info['tables'],
+                                                                               $fields,
+                                                                               $info['conds'],
+                                                                               $info['join_conds'],
+                                                                               $info['options'],
+                                                                               $this->opts['tagfilter'] );
 
                return $info;
        }
index b0eb591..91c0ecb 100644 (file)
@@ -270,6 +270,7 @@ class SpecialRecentChanges extends SpecialPage {
 
                $tables = array( 'recentchanges' );
                $join_conds = array();
+               $query_options = array( 'USE INDEX' => array('recentchanges' => 'rc_timestamp') );
 
                $uid = $wgUser->getId();
                $dbr = wfGetDB( DB_SLAVE );
@@ -290,17 +291,25 @@ class SpecialRecentChanges extends SpecialPage {
                        $join_conds['page'] = array('LEFT JOIN', 'rc_cur_id=page_id');
                }
                // Tag stuff.
-               $fields = array(); // Fields are * in this case, so let the function modify an empty array to keep it happy.
-               ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $opts['tagfilter'] );
+               $fields = array();
+               // Fields are * in this case, so let the function modify an empty array to keep it happy.
+               ChangeTags::modifyDisplayQuery( $tables,
+                                                                               $fields,
+                                                                               $conds,
+                                                                               $join_conds,
+                                                                               $query_options,
+                                                                               $opts['tagfilter']
+                                                                       );
 
                wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
 
                // Is there either one namespace selected or excluded?
+               // Tag filtering also has a better index.
                // Also, if this is "all" or main namespace, just use timestamp index.
-               if( is_null($namespace) || $invert || $namespace == NS_MAIN ) {
+               if( is_null($namespace) || $invert || $namespace == NS_MAIN || $opts['tagfilter'] ) {
                        $res = $dbr->select( $tables, '*', $conds, __METHOD__,
-                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit,
-                                       'USE INDEX' => array('recentchanges' => 'rc_timestamp') ),
+                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) +
+                               $query_options,
                                $join_conds );
                // We have a new_namespace_time index! UNION over new=(0,1) and sort result set!
                } else {
index 1812eb6..2a490c9 100644 (file)
@@ -76,6 +76,7 @@ class SpecialRecentchangeslinked extends SpecialRecentchanges {
                $tables = array( 'recentchanges' );
                $select = array( $dbr->tableName( 'recentchanges' ) . '.*' );
                $join_conds = array();
+               $query_options = array();
 
                // left join with watchlist table to highlight watched rows
                if( $uid = $wgUser->getId() ) {
@@ -84,7 +85,13 @@ class SpecialRecentchangeslinked extends SpecialRecentchanges {
                        $join_conds['watchlist'] = array( 'LEFT JOIN', "wl_user={$uid} AND wl_title=rc_title AND wl_namespace=rc_namespace" );
                }
 
-               ChangeTags::modifyDisplayQuery( $tables, $select, $conds, $join_conds, $opts['tagfilter'] );
+               ChangeTags::modifyDisplayQuery( $tables,
+                                                                               $select,
+                                                                               $conds,
+                                                                               $join_conds,
+                                                                               $query_options,
+                                                                               $opts['tagfilter'],
+                                                                               );
 
                // XXX: parent class does this, should we too?
                // wfRunHooks('SpecialRecentChangesQuery', array( &$conds, &$tables, &$join_conds, $opts ) );
@@ -142,7 +149,7 @@ class SpecialRecentchangeslinked extends SpecialRecentchanges {
                                $select, 
                                $conds + $subconds,
                                __METHOD__, 
-                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ),
+                               array( 'ORDER BY' => 'rc_timestamp DESC', 'LIMIT' => $limit ) + $query_options,
                                $join_conds + array( $link_table => array( 'INNER JOIN', $subjoin ) )
                        );
                }
index 79f054d..b14577b 100644 (file)
@@ -222,7 +222,7 @@ function wfSpecialWatchlist( $par ) {
                        $fields[] = 'page_latest';
        }
 
-       ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, '' );
+       ChangeTags::modifyDisplayQuery( $tables, $fields, $conds, $join_conds, $options, '' );
        wfRunHooks('SpecialWatchlistQuery', array(&$conds,&$tables,&$join_conds,&$fields) );
        
        $res = $dbr->select( $tables, $fields, $conds, __METHOD__, $options, $join_conds );