Strange Logic Of Building Sql Query By Activerecord


I have faced with next problem

I have user->comments HAS_MANY relation

and i want get user comments


when i use lazy load i have simple query

SELECT * FROM `comments` where id_user = 1 

when i use eager load i have joined query

SELECT , `c`.* FROM `user` LEFT OUTTER JOIN `comments` c on c.id_user= WHERE t.id_user =1 

Who can explain such strange behavior?

it is not strange. eager loading means you want to load related data in same call, possibly same SQL query. Less queries means lower latency, less to process on PHP side. In case of HAS_MANY, MANY_MANY Yii if possible uses join and then processes results and groups them by main table id (it is done in PHP processing resultset). It works pretty fast, but has one drawback - pagination does not work…

Nevertheless there is option "together" to separate queries for best performance

and how about fetching list of objects with related objects in MANY_MANY relation… how many queries then?

one, difference is with lazy load i have one join, and with eager load i have 2 joins