(bug 20268) Fixed Database::estimateRowCount on SQLite backend. This involved moving...
authorMax Semenik <maxsem@users.mediawiki.org>
Wed, 21 Oct 2009 12:21:09 +0000 (12:21 +0000)
committerMax Semenik <maxsem@users.mediawiki.org>
Wed, 21 Oct 2009 12:21:09 +0000 (12:21 +0000)
RELEASE-NOTES
includes/db/Database.php
includes/db/DatabaseIbm_db2.php
includes/db/DatabaseMssql.php
includes/db/DatabaseMysql.php

index f89cc3c..8e81535 100644 (file)
@@ -580,6 +580,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 20847) Deprecated deprecated akeytt() removed in wikibits.js leaving
   dummy
 * (bug 21161) Changing $wgCaCacheEpoch now always invalidates file cache
+* (bug 20268) Fixed row count estimation on SQLite backend
 
 == API changes in 1.16 ==
 
index c8deb33..e9e8753 100644 (file)
@@ -961,26 +961,26 @@ abstract class DatabaseBase {
        
        /**
         * Estimate rows in dataset
-        * Returns estimated count, based on EXPLAIN output
+        * Returns estimated count - not necessarily an accurate estimate across different databases,
+        * so use sparingly
         * Takes same arguments as Database::select()
+        *
+        * @param string $table table name
+        * @param array $vars unused
+        * @param array $conds filters on the table
+        * @param string $fname function name for profiling
+        * @param array $options options for select
+        * @return int row count
         */
        public 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 = 0;
+               $res = $this->select ( $table, 'COUNT(*) AS rowcount', $conds, $fname, $options );
+               if ( $res ) {
+                       $row = $this->fetchRow( $res );
+                       $rows = ( isset( $row['rowcount'] ) ) ? $row['rowcount'] : 0;
                }
-
-               $rows = 1;
-               while( $plan = $this->fetchObject( $res ) ) {
-                       $rows *= $plan->rows > 0 ? $plan->rows : 1; // avoid resetting to zero
-               }
-
-               $this->freeResult($res);
-               return $rows;           
+               $this->freeResult( $res );
+               return $rows;
        }
 
        /**
index ed493e2..b6866c5 100644 (file)
@@ -1662,29 +1662,7 @@ SQL;
 
                $this->query( $sql, $fname );
        }
-       
-       /**
-        * Estimate rows in dataset
-        * Returns estimated count, based on COUNT(*) output
-        * Takes same arguments as Database::select()
-        * @param string $table table name
-        * @param array $vars unused
-        * @param array $conds filters on the table
-        * @param string $fname function name for profiling
-        * @param array $options options for select
-        * @return int row count
-        */
-       public function estimateRowCount( $table, $vars='*', $conds='', $fname = 'Database::estimateRowCount', $options = array() ) {
-               $rows = 0;
-               $res = $this->select ($table, 'COUNT(*) as mwrowcount', $conds, $fname, $options );
-               if ($res) {
-                       $row = $this->fetchRow($res);
-                       $rows = (isset($row['mwrowcount'])) ? $row['mwrowcount'] : 0;
-               }
-               $this->freeResult($res);
-               return $rows;
-       }
-       
+
        /**
         * Description is left as an exercise for the reader
         * @param mixed $b data to be encoded
index 533d0cd..923ffe4 100644 (file)
@@ -445,22 +445,6 @@ class DatabaseMssql extends DatabaseBase {
                return $this->query( $sql, $fname );
        }
 
-       /**
-        * 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() ) {
-               $rows = 0;
-               $res = $this->select ($table, 'COUNT(*)', $conds, $fname, $options );
-               if ($res) {
-                       $row = $this->fetchObject($res);
-                       $rows = $row[0];
-               }
-               $this->freeResult($res);
-               return $rows;
-       }
-       
        /**
         * Determines whether a field exists in a table
         * Usually aborts on failure
index 9727eaf..4042546 100644 (file)
@@ -229,6 +229,30 @@ class DatabaseMysql extends DatabaseBase {
        }
 
        function affectedRows() { return mysql_affected_rows( $this->mConn ); }
+       
+       /**
+        * Estimate rows in dataset
+        * Returns estimated count, based on EXPLAIN output
+        * Takes same arguments as Database::select()
+        */
+       public 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;           
+       }
 
        function fieldInfo( $table, $field ) {
                $table = $this->tableName( $table );