Welcome Guest, Not a member yet? Register   Sign In
Database results passing by reference or by value
#1

[eluser]himalking[/eluser]
Hello guys,
Is passing database results from model to controller then to view is considered a duplication for database results, I’m afraid if my database results is very large this will consume more memory.

I also used to (before using codeignter) loop through the result, and just out put the result row by row instead of storing all the results in a single variable. Your help will be much appreciated.

Excuse me for my silly question as I’m new for codeigniter as well PHP
#2

[eluser]tonanbarbarian[/eluser]
any variable that is created inside a function, and is not static, is supposed to only exist while that function is executing.
so if you have a variable in your model and you store a dataset of 1000 records in it and then return that data, the variable inside the function should cease to exist as soon as the return is finished returning the value.

the controller method that is called is really no different.
the call to load->view() with data passed to it stores the view data to be used in a variable of the load object of the controller.
so if you are running the index method of the controller and you cal a model and store the results in a variable called $data, and then pass $data to the view using $this->load->view() or any of the normal CI methods of setting view data, then once the index function has finished $data no longer exists

the core CI code does extra processing after the controller method has processed to then finish displaying the view.

if you override the display of the view and force it to process in the controller then you do use more memory

however in all of this you do have to consider that it is true that at any given time in the processing any dataset you pass between model, controller and view is likely to exist in memory twice at least temporarily.
I am not sure how PHP actually handles things, it may just use pointers and move the data around when you return it from a function, but it might have to briefly have 2 copies of it.

This means that technical the memory_get_peak_usage() may be higher than expected.

Personally I would like some way to be able to retrieve results from a query one at a time if needed
maybe this will come in future versions of CI

If you are still concerned about this then I would suggest turning on the profilers and if necessary adding some more profiling of your own to determine the memory_get_usage() and memory_get_peak_usage() of the code and strategic places in your code.
#3

[eluser]himalking[/eluser]
thanx a lot for your help, I didn't think of variable disposal after function call (how silly is me).

As you said there might be a temp duplication of the dataset while passing between model and controller or viewer, that's in case passing by value, but I guess if it's passed by reference there wouldn't be that duplication, and that is what I'm not sure about.

The duplication might be a decrease in server resources while dealing with large Blob dataset.




Theme © iAndrew 2016 - Forum software by © MyBB