Running a same query multiple times? |
Hi,
Is it OK to run a single query multiple times? In my profiler, it shows that I ran the same query about 5 times. That query came from my model named "Forms_model" with method name "get_content()" Now this method Forms_model::get_content() called from different models 8 times minus 3 because it will only be called if the condition (in some part) is TRUE. Maybe this is a bad practice so I need your advice. Thank you.
Obviously if you can do the same thing in one query it is better, but not always practical. I think it depends on your situation or application.
Practical guide to IgnitedCMS - Book coming soon, www.ignitedcms.com
If it's exact same query, you probably ought to try and reuse values. Dependency injection would work quite well for this, or you could implement internal caching just for that model method:
PHP Code: private $_get_content_cache = null;
@kaitenz,
Try to avoid running a query multiple times (for a single page/controller) it is not efficient. You definitely have options (such as the ones mentioned above) or session variables or flashdata variables or arrays with multiple variable values.
Another question guys.
I want to create a query that will select ALL the rows from a database table and put/cache the result to an array property (<- is this correct?). Then when I need a specific row, instead of running the query, I'll just pull it out from that array (the keys would be the rowId). Is this OK? ![]() Let's say I have 2,000+ rows in my table. Is it bad to put all 2,000+ rows in a single variable/property? |
Welcome Guest, Not a member yet? Register Sign In |