Where is the data mysql data cached?

I use mysql for a custom field in the panel. The first time it takes a long time to load it. The second time it goes very fast.

Where is it cached?

  • Kirby Panel
  • MySql
  • Browser

If I use this more than one time, is it cached? Does anyone know?

$result = db::query('SELECT * FROM products');
$result = db::query('SELECT * FROM products');
$result = db::query('SELECT * FROM products');
$result = db::query('SELECT * FROM products');
$result = db::query('SELECT * FROM products');

Why I want to know is that I need to know if I can run the same query without a performance hit or if I need to build the cache (like storing the data in arrays with sql queries as keys).

Kirby does not have a cache for MySQL queries. In the end the same query might produce very different results. You need to do this yourself.

Maybe you are right but I just read this:


Yes, mySQL (in common with all other popular database products) caches the queries that are made to it.

It caches the queries, not the results. It might also cache the results, but that depends on the database structure.

However it might make sense to have a separate RAM cache in Kirby (as a class property somewhere). You should not cache the results to disk however.

Alright. I’ll use the memory then. Thanks!

It’s cached by MySQL. First of all, MySQL caches the disk pages it read in order to compute your result, and additionally the query result is also cached.
Be careful though, your query’s result can only be served from the cache if it is identical (byte for byte, i.e. SELECT != select != Select, …), otherwise the result will be recomputed and stored in the cache under the new query.

Unless your products table has so many records that it does not fit into the MySQL query cache (in which case you should probably not select-star it…) you should be fine without any additional PHP code; if necessary, you could still look into increasing the MySQL cache.

1 Like

Very good information. Thank you! :slight_smile: