From be1f60b6ea1da6f488a3fbde498f011bddd31a39 Mon Sep 17 00:00:00 2001 From: Arlo Breault Date: Wed, 16 Oct 2024 21:06:12 -0400 Subject: [PATCH] Force the use of the category index when paging by category The EXPLAIN in P70205#281191 shows the PRIMARY index being used, resulting in a long scan in newer categories where all the linter_id are higher. Bug: T200517 Change-Id: I373692942121ff555565c9c2c310087cd097ef21 --- includes/ApiQueryLintErrors.php | 12 +++++++++++- includes/LintErrorsPager.php | 11 +++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/includes/ApiQueryLintErrors.php b/includes/ApiQueryLintErrors.php index 8cc4a47b..134f4db5 100644 --- a/includes/ApiQueryLintErrors.php +++ b/includes/ApiQueryLintErrors.php @@ -51,11 +51,14 @@ class ApiQueryLintErrors extends ApiQueryBase { $this->requireMaxOneParameter( $params, 'pageid', 'title' ); $this->requireMaxOneParameter( $params, 'namespace', 'title' ); + $useIndex = true; $categories = $params['categories']; if ( !$categories ) { $categories = $this->categoryManager->getVisibleCategories(); } - + if ( count( $categories ) > 1 ) { + $useIndex = false; + } $this->addTables( 'linter' ); $this->addWhereFld( 'linter_cat', array_values( $this->categoryManager->getCategoryIds( $categories @@ -67,14 +70,17 @@ class ApiQueryLintErrors extends ApiQueryBase { if ( $params['pageid'] !== null ) { // This can be an array or a single pageid $this->addWhereFld( 'linter_page', $params['pageid'] ); + $useIndex = false; } if ( $params['namespace'] !== null ) { $this->addWhereFld( 'page_namespace', $params['namespace'] ); + $useIndex = false; } if ( $params['title'] !== null ) { $title = $this->getTitleFromTitleOrPageId( [ 'title' => $params['title'] ] ); $this->addWhereFld( 'page_namespace', $title->getNamespace() ); $this->addWhereFld( 'page_title', $title->getDBkey() ); + $useIndex = false; } $this->addTables( 'page' ); $this->addJoinConds( [ 'page' => [ 'INNER JOIN', 'page_id=linter_page' ] ] ); @@ -83,6 +89,10 @@ class ApiQueryLintErrors extends ApiQueryBase { 'linter_start', 'linter_end', 'page_namespace', 'page_title', ] ); + if ( $useIndex ) { + // T200517#10236299: Force the use of the category index + $this->addOption( 'USE INDEX', [ 'linter' => 'linter_cat_page_position' ] ); + } // Be explicit about ORDER BY $this->addOption( 'ORDER BY', 'linter_id' ); // Add +1 to limit to know if there's another row for continuation diff --git a/includes/LintErrorsPager.php b/includes/LintErrorsPager.php index 9f65173d..b0d491d5 100644 --- a/includes/LintErrorsPager.php +++ b/includes/LintErrorsPager.php @@ -116,6 +116,8 @@ class LintErrorsPager extends TablePager { ] ) ->where( [ 'linter_cat' => $this->categoryId ] ); + $useIndex = false; + if ( $this->title !== '' ) { $namespaces = $this->namespaces ?: [ NS_MAIN ]; // Specify page_namespace so that the index can be used (T360865) @@ -134,15 +136,24 @@ class LintErrorsPager extends TablePager { } } elseif ( $this->namespaces ) { $queryBuilder->where( [ 'linter_namespace' => $this->namespaces ] ); + } else { + $useIndex = true; } if ( $this->throughTemplate !== 'all' ) { + $useIndex = false; $op = ( $this->throughTemplate === 'with' ) ? '!=' : '='; $queryBuilder->where( $this->mDb->expr( 'linter_template', $op, '' ) ); } if ( $this->tag !== 'all' && ( new HtmlTags( $this ) )->checkAllowedHTMLTags( $this->tag ) ) { + $useIndex = false; $queryBuilder->where( [ 'linter_tag' => $this->tag ] ); } + + if ( $useIndex ) { + // T200517#10236299: Force the use of the category index + $queryBuilder->option( 'USE INDEX', [ 'linter' => 'linter_cat_page_position' ] ); + } } /**