Welcome Guest, Not a member yet? Register   Sign In
Performance question about storing DB results in an array
#1

[eluser]TenPound[/eluser]
I have a short question about the way CI passed data to its views: getting all rows from the database, putting them in the $data array, and passing them to the view to be echo'ed.

Isn't it extra memory usage to first get all dabatase rows you need, then storing then in array? Isn't it more efficient to loop through the database results in the view?
#2

[eluser]xwero[/eluser]
How are you going to pass the data from the database to the view if you don't add variables to the view file?

You can make it easier on yourself if you use the load->set_vars in the model method, then in your controller the only thing you have to do is call the method but internally the set_vars method adds the array to an array variable in the loader class.

If you can come up with a way to pass data from the model to the view without a mediator you can apply for a job at Ellislab Wink
#3

[eluser]Yash[/eluser]
My way to access DB data is like
I've a function in model that returns a array containing values.

Create view containing known variables

Call this function in controller and load view...done
#4

[eluser]TenPound[/eluser]
Yes, I understand this practise, but my question is how this will impact memory usage (or other system load).

It's a matter of "query database -> echo result" versus "query database -> store result in array -> echo array"... Doesn't that mean you effectively doubling the load on the server? No big deal for 10 records with 5 pageviews an hour, but what about 200 records with 100.000 pageviews an hour?

Thanks so far for the response Smile
#5

[eluser]xwero[/eluser]
yes you have a overhead but that is inherit to the MVC pattern. If you don't want the overhead you will have to work without MVC.

But once the popularity of your site/app is growing you are not going to create the pages every time a surfer requests them. The database results are cached so if you have many people requesting the same page the results will be served out of the memory.

Bottomline is don't save on memory for the sql server if you are running a popular site/app.
#6

[eluser]TenPound[/eluser]
Thanks for clearing this up Smile




Theme © iAndrew 2016 - Forum software by © MyBB