#22 Eager Loading (revised)
Mar 20, 2013 | 7 minutes | Performance, Active Record
One way to improve performance is to reduce the number of database queries through eager loading. Here I demonstrate this and compare the difference between the "includes" and "joins" methods.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
good episode. But what about delegate? Where is the differnence to your:
Thank you
+1
+1
Using
delegate :name, :to => :category, :prefix => 'category'
would simply docategory.name
rather than read the loaded/cachedcategory_name
attribute. AFAIK+1
I agree !
I'm Too !
Pretty cool !
That's okay for me
+1
+1
Good!
Off topic but how do you get SQL syntax highlighting when using tail?
It's really easy, just type it on Google ;)
I went through many of the original RailsCasts about a year ago. They where are a great way to refresh on the basics, but rails has changed so much that it would be difficult for a new rails hopeful, such as my son, to make some of them work. Thanks for giving us a fresh, back-to-basics episode.
I agree. They are they a good reminder for optimization techniques and what has been deprecated as we move move into new Rails versions.
Great episode! But I was hoping you'd touch eager loading with polymorphic associations. I couldn't find good information about it, and I feel it's a common problem.
The way I approached eager loading with polymorphic associations is to define the relationship as non-polymorphic for the types of polymorphic you need to eager load. Only works for cases where your eager loading is of the same type.
Yeah, but thats exactly what I wanted to avoid doing. My model is this message board:
A
User
hasFeedItem
s. Each feed item is polymorphically related to one other model object e.g.TextPost
,Picture
,File
,SpreadSheet
and many more, viaFeedItem
'spostable
polymorphic propertySo the problem here is with
FeedItem
: When showingUser
'sFeedItems
, I want to do something like:But since
postable
is polymorphic, i cant and i get the n+1 problem...By using includes there, the polymorphic association is loaded in the best possible way (meaning that first it runs a query to load the feed items, and then, judging by the id/types it does a query for each type, and not for each instance, which from my point of view it's the best possible way to do it)
I had a more difficult problem posted here where I wanted to do an includes on the polymorphic model to eager-load other associated models, but ended up doing a manual sweep
You are totally correct, it really does what you said, which is the best solution. But I too share your problem:
TextPost
andPicture
have comments (which I'd like to eagerly load), andSpreadSheet
has collaborators, which I'd also like to eagerly load.I think this can be resolved with default scopes? If you want your
Picture
to include:comments
and yourSpreadsheet
to include:collaborators
:... and if you didn't want to include somewhere, use something like
Picture.all.unscoped
+1 for eager loading with polymorphic associations. A very common and real problem that should be addressed in a Pro episode. Thanks.
+1
+1
+1
+100
+100
+1
Unfortunately eager loading polymorphic associations isn't actually possible. The polymorphic association holds a a class name and an id present on that class. The polymorphic associations, though, don't have a way to join them in a SQL query since SQL doesn't support any sort of meta-programing.
For instance:
Address is kept in addresses table, and has an addressable reference. In the database there would be "addressable_id" and "addressable_type". There's no way to use "addressable_type" to make an INNER JOIN in the SQL query.
Just to add up more info for future visitors:
You can, instead of "include" use "preload". But this will cause a second query to be triggered. This method is recommended since it'll help you to avoid N+1 queries.
An interesting article for future visitors:
http://blog.arkency.com/2013/12/rails4-preloading/
THX SBU, It's for me
THX SBU, It's for me
I went through many of the original RailsCasts about a year ago. They where are a great way to refresh on the basics, but rails has changed so much that it would be difficult for a new rails hopeful, such as my son, to make some of them work. Thanks for giving us a fresh, back-to-basics episode.
jjkmjhjh
Great! Thanks for sharing the information. That is very helpful for increasing my knowledge in this field.
Good to be here and to read this marvelous information which i need to know more. Thanks a lot sir
Thanks for sharing information, good advise.
Perfect website, thanks for sharing.
Cool content !very pleased with the author
Good Post
The above article presents a very simple and clear to understand!!The author well done!
Very helpful advice in this particular post! It’s the little changes that make the largest changes. Thanks for sharing!
I create a comment when I like a post on a website or if I have something to contribute to the conversation. It's caused by the passion communicated in the post I browsed. And after this article
Thank you gracias!
I like this post so much and I will share this post to my friends because all the details are in this post is brilliant.
NICE ONE THANK YOU FOR SHARING .
very good
Thank you for your work on this blog! You're doing a great job!
good job
Your website it's very nice, i like your website friend
Muchísimas gracias por brindar a las personas una oportunidad realmente asombrosa para consultar reseñas críticas importantes de esta página web.
No.1 bike transport In Pune and India. We Also Provide Packers And Movers Car Transportation & household services in Pune and pune to all India.Mumbai.
Thanks for the information
great post.. and nice article
It was wondering if I could use this write-up on my other website, I will link it back to your website though.Great Thanks
great post!
Mount abu escort
penyebeb siplis itu apa
I really impressed after read this because of some quality work and informative thoughts. I just wanna say thanks for the writer and wish you all the best for coming!