Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The schema caching in Yii

Tags:

php

caching

yii

Here http://www.yiiframework.com/doc/guide/1.1/en/topics.performance we can see the following information:

"If the application is using Active Record, we should turn on the schema caching to save the time of parsing database schema. This can be done by configuring the CDbConnection::schemaCachingDuration property to be a value greater than 0."

I have some questions for it:

1. Why should we use schema caching?

2. How does schema caching work?

3. Where can I see the tests?

like image 603
Bohdan Vorona Avatar asked May 05 '14 06:05

Bohdan Vorona


People also ask

How to use caching in yii2?

You can register several cache application components. yii\caching\DbCache − Uses a database table to store cached data. Uou must create a table as specified in yii\caching\DbCache::$cacheTable. yii\caching\ApcCache − Uses PHP APC extension.

How to clear cache in Yii?

When you need to invalidate all the stored cache data, you can call yii\caching\Cache::flush(). You can flush the cache from the console by calling yii cache/flush as well. Info: Console application uses a separate configuration file by default.

What is $App in Yii?

Applications are objects that govern the overall structure and lifecycle of Yii application systems. Each Yii application system contains a single application object which is created in the entry script and is globally accessible through the expression \Yii::$app .

How long does it take to learn Yii?

Basics - can take days, weeks or years. ;) Yii - Step 1 & 2, takes a few hours. The rest takes days, or a few weeks if you're doing things parttime. After all that, you should go into the depths of building your own custom Yii stuff!


1 Answers

If you set Yii to log database queries ('enableParamLogging' => true in the db settings of your config file) you can see that Yii queries the database frequently for ActiveRecord queries.

For example: Say you have a model named User and you tell Yii to get you a user by primary key, Yii will query the database three(!) times. It will do a show columns query, then a show create table query, then finally it will query the database for the actual data. Those first two queries are so Yii knows the schema of your user table. If the round trip time from your application server to your database server is 100ms (if it's really this slow, you should do something about it), then those two queries to get the schema will add a minimum of 200ms to your application response time. It will do this for every single request that populates your User model. Depending on how your application is written, it might even do that multiple times in a single request.

If, on the other hand, you tell Yii to use schema caching, Yii will check the cache to see if it already knows the schema to your user table and if it does, it will use that instead of hitting the database. There is still latency involved in checking the cache, but hopefully it's less than or equal to the latency to the database server. Say you're using a Redis server for caching and it also has a latency of 100ms (also ridiculously long). Caching the schema will still be faster than querying the database, because Yii will only need to query the cache a single time to retrieve the schema, versus two trips to the database.

So in this example switching from querying a high latency database for the schema to querying a high latency cache server will still save you time. In practice, your cache should be much lower latency than your database and therefore save you much more time and if your application is remotely complicated, Yii will be loading schema from multiple tables on each request so schema caching can make a huge difference in request response time.

like image 137
Tim Gautier Avatar answered Oct 03 '22 12:10

Tim Gautier