What is this long executing query when using ActiveDataProvider

Hello everyone. This is the first time that I see this in debugger. I wasn’t using Yii2 for last few months, so maybe I missed some update. When I create model and CRUD using Gii, I get DocumentSearch model.

So I am using the default code made by Gii. Table has around 1.3M rows. And these are the queries that I see in debugger:






FROM information_schema.referential_constraints AS rc

JOIN information_schema.key_column_usage AS kcu ON


        kcu.constraint_catalog = rc.constraint_catalog OR

        (kcu.constraint_catalog IS NULL AND rc.constraint_catalog IS NULL)

    ) AND

    kcu.constraint_schema = rc.constraint_schema AND

    kcu.constraint_name = rc.constraint_name

WHERE rc.constraint_schema = database() AND kcu.table_schema = database()

AND rc.table_name = 'document' AND kcu.table_name = 'document'


SELECT * FROM `document` LIMIT 20

SELECT COUNT(*) FROM `document`

I have managed to cache 3) and 4), but I do not know what to do with 1). Becasue query 1) is executing for about 500ms, which is pretty much for me, since I will have to join few big tables, and I really want to avoid executing same queries over and over, especially if they are slow.

My question is, can someone explain to me the purpose of query 1), when/what is executing it, and can it be cached somehow ?

Here is my code of search method in DocumentSearch model:

public function search($params)


    $query = Document::find();

    // add conditions that should always apply here

    $dataProvider = new ActiveDataProvider([

        'query' => $query,



    if (!$this->validate()) {

        // uncomment the following line if you do not want to return any records when validation fails

        // $query->where('0=1');

        return $dataProvider;


    self::getDb()->cache(function ($db) use ($dataProvider) {


    }, 3600);

    return $dataProvider;


And btw, queries 3) and 4) are executing in less than 1ms each.

Please check this topic:


Thanks, that is it.