• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Running a same query multiple times?

#1
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.
Reply

#2
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
Reply

#3
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;

public function 
get_content()
{
    if (
is_null($this->_get_content_cache)) {
        
$q $this->db->get(...);
        
$this->_get_content_cache $q->result();
    }
    return 
$this->_get_content_cache;

Reply

#4
@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.
Reply

#5
To refresh the cache if I will use AJAX, I'll just add either a parameter from get_content($refresh = FALSE) method or a new function called refresh_content_cache()
Reply

#6
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?  Huh

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?
Reply

#7
@kaitenz,

This might be helpful... https://www.codeigniter.com/user_guide/d...er-caching
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.