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
This commit is contained in:
Arlo Breault 2024-10-16 21:06:12 -04:00
parent 4029d93e77
commit be1f60b6ea
2 changed files with 22 additions and 1 deletions

View file

@ -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

View file

@ -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' ] );
}
}
/**