Should I denormalize has_many has_many?

I have it:

class User < ActiveRecord::Base has_many :serials has_many :sites, :through => :series end class Serial < ActiveRecord::Base belongs_to :user belongs_to :site has_many :episodes end class Site < ActiveRecord::Base has_many :serials has_many :users, :through => :serials end class Episode < ActiveRecord::Base belongs_to :serial end 

I would like to do some operations with User.serials.episodes, but I know that this will mean all kinds of smart tricks. I could theoretically just put all the episode data into serial (denormalize) and then to group_by if necessary.

If I have many episodes that I need to request, will this be a bad idea?

thanks

+4
source share
2 answers

I would not interfere with denormalization.

If you need to look at the calculations, you can check counter_cache on the relation to save the request for this.

Do you have the correct indexes for your foreign keys? If so, pulling data from one additional connection should not be as large as it happens, but you may need to reduce it to SQL to get all the results in a single query without repeating .serials:

 User.serials.collect { |s| s.episodes }.uniq # ack! this could be bad 
0
source

It really depends on the scale you need from this application. If the application does not need to service tons and tons of people, then go for it. If you benefit greatly from active record associations, then keep using them. As the scale of application of your application can be replaced by specific examples of using the association with a more direct approach to processing traffic, though.

0
source

Source: https://habr.com/ru/post/1307688/


All Articles