• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Query Caching or NOT?!!!

Hi to all developers...
I'm using last version of CI, created a blog system (some posts, series, commenting, payment link, login,... and more) and want run it on a shared host.

In order to get best performance, I want to use CACHE. As you an option is to cache whole page, but I think it is not good (at least for me!). Another option is Query Caching, that I think it's good (but not sure!). Instead to get a post or other thing from database (for second attempt and up), it read from generated files. And when I update a post, I could delete files in order to regenerating.
(I test output profiler to see loading time and memory usage, loading time is different and same to when do not use query caching, memory usage is a little more.)

My Questions:
What's your opinion about this option? What are advantages/disadvantages? How could I handle this issue in best way? What about other options (like libraries)? And any guide to reach best performance for CI app.

thanks to all (especially experts!) Smile

mysql is already very very fast in getting results when you use normal queries.
Queries that only have one table and use indexed keys to get result. Caching will only start making a visible effect when used on joined tables and when this query is trigger numerous times per page view.

I dont think on a simple post page you will much any visible speed improvement when you use query cache.

Personally I think its far better to optimize your data structure and use indexed field correctly.
On the package it said needs Windows 7 or better. So I installed Linux.

I think that you can use Memcached (https://codeigniter.com/user_guide/libra...ed-caching).

Add the content selected from the database to a memcached key and get it when load the page.

Also, update the memcached value associated to the same key when you update the post. Then the contents will be the same. Or, if you do not want save in the cache every time that you update a post, you can simply check if the key exists. If it exists, then update.

You're right that full page caching wouldn't work well, but you could still use the cache engine and cache any views or view fragments that might take a bit to generate. Especially when combined with in-memory cache like Memcached it can work great.

DHH (from Ruby on Rails/Basecamp) had a couple of great posts about his "Russian Doll" caching technique. They're great reads for another view:

How Basecamp Next got to be so damn fast without using much client-side UI
How key-based cache expiration works
The performance impact of "Russian doll" caching
Practical CodeIgniter 3  • CodeIgniter 4 Foundations - Coming Soon • Vulcan - CLI Tools for CI4

In CI3 I don't use the built-in query caching. Within code I prefer manually to gain speed in exchange of memory. If a query has a big chance to be put within a cycle, then I store its results within a hidden static variable.

// Model Enums

    public function get_country_name($id) {

        static $cache = array();

        $id = (int) $id;

        if (array_key_exists($id, $cache)) {
            return $cache[$id];

        $cache[$id] = $this
            ->where('type', CONST_ENUM_NATIONALITY)

        return $cache[$id];

Client side templating using pre-compiled and gzipped views so your browser can cache them like CSS and JS files. Render them using live data. Run explain on all your queries and add appropriate indices.

For queries that are unlikely to change during a request use static caching like ivantcholakov says;

    public function get_countries( ) {
        static $results;

        if ( $results == null ) {
            $results = $this->db->query( "SELECT c.* FROM country c WHERE c.archive = 0 ORDER BY c.name ASC" )->result_array( );

        return $results;

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

Users browsing this thread:
1 Guest(s)

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