sam
(Sam Saffron)
2017 年2 月 14 日 21:45
1
Certain plugins need the ability to “preload” data in the topic list.
Some specific examples:
Discourse solved adds an icon on the topic list when a topic is solved
Discourse assign will display the username the topic is assigned to on the topic list
To achieve that we have 2 mechanisms:
Preloading a custom field
If you have any custom fields you need for rendering of the topic list use:
TopicList.preloaded_custom_fields << "my_custom_field"
This will force the system to ensure that the Topic object model has the custom field loaded when serializing.
If you do not do that and try to access a custom field an error will be raised.
###Preloading custom data
In some cases you may need to preload custom data that goes beyond a custom field. To do so:
TopicList.on_preload do |topics, topic_list|
# your custom code to load up all topic data in bulk goes here
end
This is useful when you need to reach out and preload other models.
By carefully using both these methods you can ensure your plugin does not cause N+1 queries.
9 个赞
I believe Feature Voting plugin currently has n+1 queries and perhaps for the same reasons?
sam
(Sam Saffron)
2017 年2 月 14 日 22:01
3
very possible, I only added the ability to preload custom data today.
Falco
(Falco)
2017 年2 月 14 日 22:03
4
This will make a lot of plugins use way less code, like my signatures plugin.
1 个赞
sam
(Sam Saffron)
2017 年2 月 14 日 22:04
5
Does signature show up in topic list? scary …
Falco
(Falco)
2017 年2 月 14 日 22:06
6
Oh no, misread as post list.
Will there be an analogous feature to the post stream?
4 个赞
sam
(Sam Saffron)
2017 年2 月 14 日 22:07
7
I would be happy to add an analogous feature, makes sense there as well.
4 个赞
joebuhlig
(Joe Buhlig)
2017 年2 月 15 日 02:04
8
It used to. I think I’ve cleaned most of those up. I don’t know of any others.
committed 02:44PM - 20 Oct 16 UTC
I had to preload the custom fields due to errors if the data was blank. It was tricky to debug at first because I had existing data so I couldn’t replicate the errors reported. Then someone mentioned this process in Babble. Once I implemented it, the errors were gone. I never looked into it further than that.
3 个赞
angus
(Angus McLeod)
2025 年1 月 17 日 15:36
9
Just for anyone reading this in 2025 I’ve written an updated version on the same topic
When working with the Discourse rails application, whether building a plugin, or making a pull request to discourse/discourse there are a number of contexts where you’ll encounter N+1 query problems. This topic explains how to handle data preloading to address such problems and keep Discourse performant.
N+1 Query Problems in Rails
First, if you’re not already familiar with N+1 query problems, and how Rails addresses them, check out this section of the guides.
Topics
The N+1 issues to look ou…
2 个赞