Changeset 106 for devel/units/db

Show
Ignore:
Timestamp:
01/12/06 08:02:45 (3 years ago)
Author:
sven
Message:

some sql optimisation
- count(*) is more efficient than equivalent count(fieldname) of primary key
- unlefting left joins where the right is required
- "a IN (x,y)" is easier to optimise than "a=x OR a=y"

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • devel/units/db/library.php

    r94 r106  
    11<?php 
    2  
    3 /* 
    4 * TODO: This could probably do with using the $db_connection variable in the mysql functions, 
    5 * as PHP can be a bit arbitrary in its choice of db session. Would also be required for a  
    6 * replicated mysql system, which could be a future need for large user bases. 
    7 * Not actually running a test copy of Elgg atm so dunno what scope $db_connection's in. 
    8 * - Sven 
    9 */ 
    102 
    113        // Database library functions 
     
    168                        global $querynum; 
    179                        global $querycache; 
     10                        global $db_connection; 
    1811                         
    1912                        /*if (isset($querycache[$sql_query])) { 
     
    2922                        // echo "<b>" . $run_context . "</b>&nbsp;&nbsp;" . $sql_query . "<br />"; 
    3023                        if ($sql_query != "") { 
    31                                 // echo "<!-- $sql_query -->\n"; 
    32                                 if ($result = @mysql_query($sql_query)) { 
     24                                if ($result = @mysql_query($sql_query, $db_connection)) { 
    3325                                        $data = array(); 
    3426                                        if (!is_bool($result)) { 
     
    4234                                } else { 
    4335                                        if (ELGG_DEBUG) { 
    44                                                 echo $sql_query . " :: " . @mysql_error() . "<br />\n"; 
     36                                                echo $sql_query . " :: " . @mysql_error($db_connection) . "<br />\n"; 
    4537                                        } 
    4638                                        $querycache[$sql_query] = FALSE; 
     
    5446        // Rows affected by the last MySQL transaction 
    5547                function db_affected_rows() { 
    56                         return @mysql_affected_rows(); 
     48                        global $db_connection; 
     49                        return @mysql_affected_rows($db_connection); 
    5750                } 
    5851                 
    5952        // Returns the ID of the last MySQL transaction 
    6053                function db_id() { 
    61                         return @mysql_insert_id(); 
     54                        global $db_connection; 
     55                        return @mysql_insert_id($db_connection); 
    6256                } 
    6357