Make WordPress Core

Changeset 16320


Ignore:
Timestamp:
11/12/2010 10:15:18 AM (13 years ago)
Author:
nacin
Message:

Memory usage and execution improvements in wpdb. Store and work with resources directly, rather than full copies of results. Plugins which incorrectly used wpdb->last_result (a private property) will need to shift to wpdb->get_results() with no \$query. Magic getter is introduced for back compat when using PHP5. props joelhardi, fixes #12257.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/wp-db.php

    r15904 r16320  
    126126
    127127    /**
    128      * Results of the last query made
    129      *
    130      * @since 1.0.0
    131      * @access private
    132      * @var array|null
    133      */
    134     var $last_result;
     128     * MySQL result resource of the last query made
     129     *
     130     * @since 3.1.0
     131     * @access private
     132     * @var resource|null
     133     */
     134    var $_mysql_last_result;
    135135
    136136    /**
     
    10161016     */
    10171017    function flush() {
    1018         $this->last_result = array();
     1018        @mysql_free_result( $this->_mysql_last_result );
    10191019        $this->col_info    = null;
    10201020        $this->last_query  = null;
     
    10861086            $this->timer_start();
    10871087
    1088         $this->result = @mysql_query( $query, $this->dbh );
     1088        $this->_mysql_last_result = @mysql_query( $query, $this->dbh );
    10891089        $this->num_queries++;
    10901090
     
    11081108        } else {
    11091109            $i = 0;
    1110             while ( $i < @mysql_num_fields( $this->result ) ) {
    1111                 $this->col_info[$i] = @mysql_fetch_field( $this->result );
     1110            while ( $i < @mysql_num_fields( $this->_mysql_last_result ) ) {
     1111                $this->col_info[$i] = @mysql_fetch_field( $this->_mysql_last_result );
    11121112                $i++;
    11131113            }
    1114             $num_rows = 0;
    1115             while ( $row = @mysql_fetch_object( $this->result ) ) {
    1116                 $this->last_result[$num_rows] = $row;
    1117                 $num_rows++;
    1118             }
    1119 
    1120             @mysql_free_result( $this->result );
    11211114
    11221115            // Log number of rows the query returned
    11231116            // and return number of rows selected
    1124             $this->num_rows = $num_rows;
    1125             $return_val     = $num_rows;
     1117            $this->num_rows = @mysql_num_rows( $this->_mysql_last_result );
     1118            $return_val     = $this->num_rows;
    11261119        }
    11271120
     
    12821275            $this->query( $query );
    12831276
    1284         // Extract var out of cached results based x,y vals
    1285         if ( !empty( $this->last_result[$y] ) ) {
    1286             $values = array_values( get_object_vars( $this->last_result[$y] ) );
     1277        // Extract var from result resource based x,y vals
     1278        if ( $this->num_rows > $y ) {
     1279            @mysql_data_seek( $this->_mysql_last_result, $y );
     1280            $values = @mysql_fetch_row( $this->_mysql_last_result );
    12871281        }
    12881282
     
    13111305            return null;
    13121306
    1313         if ( !isset( $this->last_result[$y] ) )
     1307        if ( $this->num_rows <= $y )
    13141308            return null;
    13151309
     1310        @mysql_data_seek( $this->_mysql_last_result, $y );
     1311
    13161312        if ( $output == OBJECT ) {
    1317             return $this->last_result[$y] ? $this->last_result[$y] : null;
     1313            return @mysql_fetch_object( $this->_mysql_last_result );
    13181314        } elseif ( $output == ARRAY_A ) {
    1319             return $this->last_result[$y] ? get_object_vars( $this->last_result[$y] ) : null;
     1315            return @mysql_fetch_assoc( $this->_mysql_last_result );
    13201316        } elseif ( $output == ARRAY_N ) {
    1321             return $this->last_result[$y] ? array_values( get_object_vars( $this->last_result[$y] ) ) : null;
     1317            return @mysql_fetch_row( $this->_mysql_last_result );
    13221318        } else {
    13231319            $this->print_error(/*WP_I18N_DB_GETROW_ERROR*/" \$db->get_row(string query, output type, int offset) -- Output type must be one of: OBJECT, ARRAY_A, ARRAY_N"/*/WP_I18N_DB_GETROW_ERROR*/);
     
    13441340        $new_array = array();
    13451341        // Extract the column values
    1346         for ( $i = 0, $j = count( $this->last_result ); $i < $j; $i++ ) {
    1347             $new_array[$i] = $this->get_var( null, $x, $i );
     1342        @mysql_data_seek( $this->_mysql_last_result, 0 );
     1343        for ( $i = 0, $j = $this->num_rows; $i < $j; $i++ ) {
     1344            $values = @mysql_fetch_row( $this->_mysql_last_result );
     1345            if ( isset( $values[$x] ) && $values[$x] !== '' )
     1346                $new_array[$i] = $values[$x];
     1347            else
     1348                $new_array[$i] = null;         
    13481349        }
    13491350        return $new_array;
     
    13721373
    13731374        $new_array = array();
     1375        @mysql_data_seek( $this->_mysql_last_result, 0 );
    13741376        if ( $output == OBJECT ) {
    13751377            // Return an integer-keyed array of row objects
    1376             return $this->last_result;
     1378            for ( $i = 0, $j = $this->num_rows; $i < $j; $i++ ) {
     1379                $new_array[] = @mysql_fetch_object( $this->_mysql_last_result );
     1380            }
     1381            return $new_array;
    13771382        } elseif ( $output == OBJECT_K ) {
    13781383            // Return an array of row objects with keys from column 1
    13791384            // (Duplicates are discarded)
    1380             foreach ( $this->last_result as $row ) {
     1385            while ( $row = @mysql_fetch_object( $this->_mysql_last_result ) ) {
    13811386                $key = array_shift( $var_by_ref = get_object_vars( $row ) );
    13821387                if ( ! isset( $new_array[ $key ] ) )
     
    13841389            }
    13851390            return $new_array;
    1386         } elseif ( $output == ARRAY_A || $output == ARRAY_N ) {
    1387             // Return an integer-keyed array of...
    1388             if ( $this->last_result ) {
    1389                 foreach( (array) $this->last_result as $row ) {
    1390                     if ( $output == ARRAY_N ) {
    1391                         // ...integer-keyed row arrays
    1392                         $new_array[] = array_values( get_object_vars( $row ) );
    1393                     } else {
    1394                         // ...column name-keyed row arrays
    1395                         $new_array[] = get_object_vars( $row );
    1396                     }
    1397                 }
     1391        } elseif ( $output == ARRAY_A ) {
     1392            // Return an integer-keyed array of column name-keyed row arrays
     1393            for ( $i = 0, $j = $this->num_rows; $i < $j; $i++ ) {
     1394                $new_array[] = @mysql_fetch_assoc( $this->_mysql_last_result );
     1395            }
     1396            return $new_array;
     1397        } elseif ( $output == ARRAY_N ) {
     1398            // Return an integer-keyed array of integer-keyed row arrays
     1399            for ( $i = 0, $j = $this->num_rows; $i < $j; $i++ ) {
     1400                $new_array[] = @mysql_fetch_row( $this->_mysql_last_result );
    13981401            }
    13991402            return $new_array;
     
    15601563        return preg_replace( '/[^0-9.].*/', '', mysql_get_server_info( $this->dbh ) );
    15611564    }
     1565
     1566    /**
     1567     * Magic getter used for the deprecated last_result property.
     1568     *
     1569     * @since 3.1.0
     1570     * @access private
     1571     */
     1572    function __get( $name ) {
     1573        if ( 'last_result' == $name ) {
     1574            _deprecated_argument( 'wpdb', '3.1', __( 'The last_result property is deprecated. Use $wpdb->get_result().' ) );
     1575            return $this->get_results();
     1576        }
     1577        return null;
     1578    }
    15621579}
    15631580
Note: See TracChangeset for help on using the changeset viewer.