php – Optimization in yii2 Grid View loading-ThrowExceptions

Exception or error:

I am using the yii2 grid view – active record to load data.
I need to join 3 tables in which each table has more than 100k records with search functionality. Combining these table with large data slow downs the loading of Grid.
How to optimize it?

$query = ModelName::find();
$query->joinWith(['relationshipTable1', 'relationshipTable2']);

How to solve:

i think i m not sure about it

  • try to create a new model

    -after it fetch you data with join to select the data that you want

    -finally try to fetch those data in the new model and it will appear in the grid view

  • to optimize the time don’t do the join with model there is three way to write query a procedural way where you write your query
    in the form select … this manner will give you less time executing cause model using ActiveRecord consume mucch time compared to ActiveQuerry


Check the table’s structure. Add indexes to the the fields that contains the foreign keys on all tables.

Leave a Reply

Your email address will not be published. Required fields are marked *