Optimization in Relational AR

Everything is working, but slowly. I have 20 tables, related each other.

I want to know

example:

3 tables: Grandfather, Father, Son

Getting a Son, sometimes I want get a Son with his Father and Grandfather,

sometimes I want get a Son only without Father and Grandfather.

I want to know

Is AR query return me object with relational objects only when I call that(Son->Father, Son->Father->Grandfather)

or it return me relational objects allways calling Son object?

I want to optimize my queries, want it works quickly, not slowly.

Is it posible, what do I have to do?

How much do you know about SQL databases? What normal form are they in (3N, 5N, …)? What database are you using (MySQL, Oracle, …)? How many joins are you doing on one page?

In general what makes something slow in SQL is dependent on many variables so it’s hard to say without more information. By default yii usually does lazy loading, meaning it will return you an object (Son for example)… then when you call Son->Father it will do another query to get the Father object. Again this all depends on how you query Son (also see: Relation Active Record and look at the section titled Relational Query Performance).

If you have a ton of joins on one page though, your best bet is to use DAO and manually do your query.