Active Record Join

I was reading this webpage about upgrading from 1.1 to 2.0 and this cought my eye :

Wouldn’t that be slower ?

So what happens if i want to load 500 primary records and 10 detail records for each primary. Will yii execute

1 query for 500 and then 1 query for each 500 records = 501 queries ?

Can someone elaborate on this ?




Btw, some db engines like MySQL restricts max number of values inside IN(…), this should be taken into account.

Also, you still can use a join (for example, if you need filtering by related column).

UPD: my bad, misread. Updated

No, Yii will use 2 queries when it’s an eager loading: one query for the main 500 records, and another query for the related 500 x 10 records. In the 2nd query Yii will use the PKs retrieved in the 1st query, using IN conditional clause.

Yes but in the 2nd query i will have 500 filtering parameters. Wouldn’t that be too much for MySQL and drop the query ?


It depends on your environment, I think.

They say that "max_allowed_packet" will limit the number of parameters.

I think you’ll hit the memory limit of PHP before you hit that of MySQL.