From fe029f8777ccb7d7138b5cf6238b8857b6598996 Mon Sep 17 00:00:00 2001 From: Aaron Schulz Date: Fri, 15 Aug 2014 10:52:20 -0700 Subject: [PATCH] Added DatabaseBase::selectRowCount() method * Useful for doing COUNT(*) with a LIMIT for performance * Made MergeHistory use it Change-Id: If1cfc52ea22fd09d34915c29b45d0b487d28d15a --- includes/db/Database.php | 36 +++++++++++++++++++++-- includes/specials/SpecialMergeHistory.php | 4 +-- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/includes/db/Database.php b/includes/db/Database.php index 9584e466f1..9b783a99d6 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -1752,7 +1752,7 @@ abstract class DatabaseBase implements IDatabase, DatabaseType { } /** - * Estimate rows in dataset. + * Estimate the number of rows in dataset * * MySQL allows you to estimate the number of rows that would be returned * by a SELECT query, using EXPLAIN SELECT. The estimate is provided using @@ -1771,8 +1771,8 @@ abstract class DatabaseBase implements IDatabase, DatabaseType { * @param array $options Options for select * @return int Row count */ - public function estimateRowCount( $table, $vars = '*', $conds = '', - $fname = __METHOD__, $options = array() + public function estimateRowCount( + $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = array() ) { $rows = 0; $res = $this->select( $table, array( 'rowcount' => 'COUNT(*)' ), $conds, $fname, $options ); @@ -1785,6 +1785,36 @@ abstract class DatabaseBase implements IDatabase, DatabaseType { return $rows; } + /** + * Get the number of rows in dataset + * + * This is useful when trying to do COUNT(*) but with a LIMIT for performance. + * + * Takes the same arguments as DatabaseBase::select(). + * + * @param string $table Table name + * @param string $vars Unused + * @param array|string $conds Filters on the table + * @param string $fname Function name for profiling + * @param array $options Options for select + * @return int Row count + * @since 1.24 + */ + public function selectRowCount( + $table, $vars = '*', $conds = '', $fname = __METHOD__, $options = array() + ) { + $rows = 0; + $sql = $this->selectSQLText( $table, '1', $conds, $fname, $options ); + $res = $this->query( "SELECT COUNT(*) AS rowcount FROM ($sql) tmp_count" ); + + if ( $res ) { + $row = $this->fetchRow( $res ); + $rows = ( isset( $row['rowcount'] ) ) ? $row['rowcount'] : 0; + } + + return $rows; + } + /** * Removes most variables from an SQL query and replaces them with X or N for numbers. * It's only slightly flawed. Don't use for anything important. diff --git a/includes/specials/SpecialMergeHistory.php b/includes/specials/SpecialMergeHistory.php index 492eb3b5b1..3c9b515086 100644 --- a/includes/specials/SpecialMergeHistory.php +++ b/includes/specials/SpecialMergeHistory.php @@ -388,11 +388,11 @@ class SpecialMergeHistory extends SpecialPage { } # Check that there are not too many revisions to move $limit = 5000; // avoid too much slave lag - $count = $dbw->select( 'revision', '1', + $count = $dbw->selectRowCount( 'revision', '1', array( 'rev_page' => $this->mTargetID, $timewhere ), __METHOD__, array( 'LIMIT' => $limit + 1 ) - )->numRows(); + ); if ( $count > $limit ) { $this->getOutput()->addWikiMsg( 'mergehistory-fail-toobig' ); -- 2.20.1