Yii Framework Forum: Lazy Loading Is Evil - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Lazy Loading Is Evil Rate Topic: -----

#1 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 23 September 2012 - 06:01 AM

Hey guys,
don't you think that AR lazy loading approach is evil?
God is real unless declared as integer
0

#2 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 2,601
  • Joined: 10-October 10
  • Location:Denmark

Posted 23 September 2012 - 07:35 AM

Isn't it great that you don't have to load it lazily, then? :)
"Less noise - more signal"
0

#3 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 23 September 2012 - 07:50 AM

I'd prefer to get an exception.
Here's the example.

Controller:
$list = MyModel::model()->with(array('rel1', 'rel2', 'rel3', 'rel5'))->findAll();


View:
<? foreach ($list as $item): ?>
<tr>
    <td><?= $item->rel1->name ?></td>
    <td><?= $item->rel2->name ?></td>
    <td><?= $item->rel3->name ?></td>
    <td><?= $item->rel4->name ?></td>
    <td><?= $item->rel5->name ?></td>
</tr>
<? endforeach ?>


As you have probably noticed, I forgot to add 'rel4' to 'with'.
And, because of my forgetfulness, bad things will happen.

And that brings me back to my original point: lazy loading is evil :)
God is real unless declared as integer
0

#4 User is offline   yiqing95 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 599
  • Joined: 27-December 10
  • Location:china

Posted 23 September 2012 - 09:44 AM

View PostORey, on 23 September 2012 - 07:50 AM, said:

I'd prefer to get an exception.
....

And that brings me back to my original point: lazy loading is evil :)

eager loading and lazy loading both have their usage scenarios . if you always use eager loading that may cause memory and time waste . some time you don't need the "join" (you see the underlying story about the "with" :lol: , "with" actually action in mysql(or some other DBMS) is JOIN table ,the JOIN action itself is need memory and time ! )
0

#5 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 23 September 2012 - 11:26 AM

Yeah, but lazy loading is less controllable.
If I want to use extra query , I prefer to do It manually :)

$project = Project::model()->findByPk($pid);
$company = Company::model()->find('projectId = :pid', array(':pid' => $company->projectId));


There's no need to use lazy loading for this.
God is real unless declared as integer
0

#6 User is offline   sudath 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 24-September 12

Posted 24 September 2012 - 01:06 AM

hmm. lazy loading. yeah it is a bit evil. i also prefer getting the exception.
0

#7 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,084
  • Joined: 16-February 11
  • Location:Japan

Posted 24 September 2012 - 03:03 AM

Yeah, it could be evil, but surely is convenient for a lazy programmer like me.

Usually I would start with lazy loading and will switch to eager loading if I think it's necessary. And, what I have to do then is just adding 'with'.
If referencing unloaded relation would have resulted in exception, it would be cumbersome to change loading manner between lazy and eager.
0

#8 User is offline   phtamas 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 529
  • Joined: 26-February 11
  • Location:Mezőtúr, Hungary

Posted 24 September 2012 - 12:49 PM

A graph of related objects might be needed by a complex piece of business logic which performs some calculation based on related data. Business logic is encapsulated in model (as it should be) and the controller must not know its details. Now, how would the controller know what relations have to be loaded?
Performance isn't always an issue. There are scenarios where some specific functionality of application (admin backend) or the whole application (intranet app) is available only for a few authorized users so database server is not likely to be overloaded.
0

#9 User is offline   ORey 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,700
  • Joined: 20-April 09
  • Location:Moscow, Russia

Posted 24 September 2012 - 01:14 PM

View Postphtamas, on 24 September 2012 - 12:49 PM, said:

Business logic is encapsulated in model (as it should be) and the controller must not know its details.


In a perfect world - yes.

In a real world we will have to move all the eager queries, like with('rel1', ..., 'relN'), to the model, right? Otherwise the controller will know the details about relations.

Quote

Now, how would the controller know what relations have to be loaded?


Speaking about business logic, we should create some methods like MyModel::getAllNeededData() and use only these methods.
And once we have done it, there's no difference on how the relations are loaded - using eager MyModel::model()->with('rel')->find or lazy MyModel::model()->find.

Quote

Performance isn't always an issue.


You're making me sad :)
God is real unless declared as integer
0

#10 User is offline   Nabi 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 208
  • Joined: 22-February 12
  • Location:Ahwaz

Posted 02 January 2014 - 02:57 PM

Yes, I opinion it's a real evil too B)
Greeting Eager Loading ;)
(Here good article)

وقتی پیغام میذارید، بی زحمت متنتون رو انتخاب کنید و این دو تا کار ناقابل رو هم انجام بدید:
1. از اون بالا RTL رو انتخاب کنید.
2. فونت رو هم Tahoma بگذارید.
که چشمون در نیاد.
دمت گرم!

www.IranPHP.ir
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users