From fdf9d73bb1435076a5cd8a3652991d0e6fb434f9 Mon Sep 17 00:00:00 2001 From: Domas Mituzas Date: Sat, 7 Apr 2007 07:35:54 +0000 Subject: [PATCH] - add simple row estimation mechanism, allows preparing heavy queries better :) - use estimates instead of select count(*) for jobs in Special:Statistics, won't scan million-row-tables too often --- includes/Database.php | 31 +++++++++++++++++++++++++++++++ includes/SpecialStatistics.php | 2 +- 2 files changed, 32 insertions(+), 1 deletion(-) diff --git a/includes/Database.php b/includes/Database.php index 9af91352cc..6b7014f758 100644 --- a/includes/Database.php +++ b/includes/Database.php @@ -1124,6 +1124,10 @@ class Database { $sql = $this->limitResult($sql, $options['LIMIT'], isset($options['OFFSET']) ? $options['OFFSET'] : false); $sql = "$sql $postLimitTail"; + + if (isset($options['EXPLAIN'])) { + $sql = 'EXPLAIN ' . $sql; + } return $this->query( $sql, $fname ); } @@ -1156,6 +1160,33 @@ class Database { return $obj; } + + /** + * Estimate rows in dataset + * Returns estimated count, based on EXPLAIN output + * Takes same arguments as Database::select() + */ + + function estimateRowCount( $table, $vars='*', $conds='', $fname = 'Database::estimateRowCount', $options = array() ) { + $options['EXPLAIN']=true; + $res = $this->select ($table, $vars, $conds, $fname, $options ); + if ( $res === false ) + return false; + if (!$this->numRows($res)) { + $this->freeResult($res); + return 0; + } + + $rows=1; + + while( $plan = $this->fetchObject( $res ) ) { + $rows *= ($plan->rows > 0)?$plan->rows:1; // avoid resetting to zero + } + + $this->freeResult($res); + return $rows; + } + /** * Removes most variables from an SQL query and replaces them with X or N for numbers. diff --git a/includes/SpecialStatistics.php b/includes/SpecialStatistics.php index 59cca3ed77..1c9e0ab61c 100644 --- a/includes/SpecialStatistics.php +++ b/includes/SpecialStatistics.php @@ -23,7 +23,7 @@ function wfSpecialStatistics() { $users = SiteStats::users(); $admins = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), $fname ); - $numJobs = $dbr->selectField( 'job', 'COUNT(*)', '', $fname ); + $numJobs = $dbr->estimateRowCount('job'); if ($action == 'raw') { $wgOut->disable(); -- 2.20.1