Home » Php » memory – PHP: how are query results stored in mysqli_result

memory – PHP: how are query results stored in mysqli_result

Posted by: admin July 12, 2020 Leave a comment

Questions:

When I made a query to the database and retrieve the results in mysqli_result, the memory usage is extremely small. However, when I fetch all the rows in the query results in to an associative array, the memory usage becomes extremely high.

<?php
    require_once("../config.php"); //db connection config
    $db = new mysqli(DB_HOST,DB_USER,DB_PASSWORD,DB_DBASE);

    $query ="select * from table_name";
    if($r = $db->query($query)){
    echo "MEMORY USAGE before :  ". memory_get_usage()."<br><br>";
    $rows = array();
    while($row = $r->fetch_assoc()){

        $rows[]= $row;
    }
    echo "MEMORY USAGE after :  ". memory_get_usage()."<br><br>";


    //before: 660880
    //after:  114655768
    // # of records: around 30 thousands
?>

It makes sense to me that storing this many results is very memory consuming, but I’m just wondering how come mysqli_result is so small. It can’t be that the results are queried to the dbase every time fetch_assoc is called. So then where are the results stored in the memory.

How to&Answers:

There is a HUGE difference between fetching results and storing a pointer to a resource.

If you echo $r; before your first call to memory_get_usage();, you will realize it is just a pointer. This is the pointer to your result set. Until you fetch your results, the result set will not actually be stored into memory.

I would suggest that you run fetchAll() for what you are trying to do. This will then result in 1 method accessing all your results with better performance since it’s pawned off on the mysqli extension (C Library) rather than a loop in PHP.

You can also use the free results function to clear your results from memory when you are done with them. This is like closing a cursor in Java if you are familiar.

Answer:

I think you should to this instead:

while($row = $r->fetch_assoc()){
   //Do whatever you need with the record, then:
   unset($row);
}

The way you posted is gathering a huge array in $rows, and memory usage reflects that.