Optimize amount of SQL queries for Special:TopRatings

Put the SUM(vote_value) directly in a column, instead of executing separate
queries.

Change-Id: I9fbf6b836a6e149e1cd5b16567306997a18446ed
This commit is contained in:
Southparkfan 2016-11-14 11:04:47 -05:00
parent 81d9a5bdd6
commit f5596b4217

View file

@ -63,7 +63,7 @@ class SpecialTopRatings extends IncludableSpecialPage {
$dbr = wfGetDB( DB_SLAVE ); $dbr = wfGetDB( DB_SLAVE );
$tables = $where = $joinConds = array(); $tables = $where = $joinConds = array();
$whatToSelect = array( 'DISTINCT vote_page_id' ); $whatToSelect = array( 'DISTINCT vote_page_id', 'SUM(vote_value) AS vote_value_sum' );
// By default we have no category and no namespace // By default we have no category and no namespace
$tables = array( 'Vote' ); $tables = array( 'Vote' );
@ -85,29 +85,24 @@ class SpecialTopRatings extends IncludableSpecialPage {
// Perform the SQL query with the given conditions; the basic idea is // Perform the SQL query with the given conditions; the basic idea is
// that we get $limit (however, 100 or less) unique page IDs from the // that we get $limit (however, 100 or less) unique page IDs from the
// Vote table. If a category and a namespace have been given, we also // Vote table. The GROUP BY is to make SUM(vote_value) give the SUM of
// do an INNER JOIN with page and categorylinks table to get the // all vote_values for a page. If a category and a namespace have been
// correct data. // given, we also do an INNER JOIN with page and categorylinks table
// to get the correct data.
$res = $dbr->select( $res = $dbr->select(
$tables, $tables,
$whatToSelect, $whatToSelect,
$where, $where,
__METHOD__, __METHOD__,
array( 'LIMIT' => intval( $limit ) ), array( 'GROUP BY' => 'vote_page_id', 'LIMIT' => intval( $limit ) ),
$joinConds $joinConds
); );
foreach ( $res as $row ) { foreach ( $res as $row ) {
// Add the results to the $ratings array and get the amount of // Add the results to the $ratings array
// votes the given page ID has
// For example: $ratings[1] = 11 = page with the page ID 1 has 11 // For example: $ratings[1] = 11 = page with the page ID 1 has 11
// votes // votes
$ratings[$row->vote_page_id] = (int)$dbr->selectField( $ratings[$row->vote_page_id] = (int)$row->vote_value_sum;
'Vote',
'SUM(vote_value)',
array( 'vote_page_id' => $row->vote_page_id ),
__METHOD__
);
} }
// If we have some ratings, start building HTML output // If we have some ratings, start building HTML output
@ -196,4 +191,4 @@ class SpecialTopRatings extends IncludableSpecialPage {
return $voteAvg; return $voteAvg;
} }
} }