CodeIgniter Forums
getUnbufferedRow() memory usage - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: getUnbufferedRow() memory usage (/showthread.php?tid=79385)



getUnbufferedRow() memory usage - Auriferous - 06-07-2021

Version 4.x

When using the getUnbufferedRow() for mysql results it seems that the driver does not properly apply the appropriate logic.

According to php docs, the default result mode is: MYSQLI_STORE_RESULT

When performing unbuffered queries you need to use MYSQLI_USE_RESULT while executing the query method.

https://www.php.net/manual/en/mysqlinfo.concepts.buffering.php

A simple hack:

in file: /system/Database/MySQLi/Connection.php

Add property to class:  public $result_mode = MYSQLI_STORE_RESULT; // default

on line 329 in the execute method change:
return $this->connID->query($this->prepQuery($sql));
to:
return $this->connID->query($this->prepQuery($sql),$this->result_mode);

Then in model when you need unbuffered results you can set result mode: $model->db->result_mode = MYSQLI_USE_RESULT;

I have a query when run in buffered mode consumes 300mb.

Then when I use getUnbufferedRow() it still consumes 100mb.

After applying the above hack and setting $model->db->result_mode = MYSQLI_USE_RESULT; it only consumes 5mb.

Perhaps there is a better fix than the one I outline above but this seems to make huge improvements in memory usage.