I have a query that spans several tables that at the end use Active Serializers to display data. Currently, most of the time is spent in serializers, as I have to request some data from the serializer itself. I want to find a way to speed this up, and it may not be using AMS (this is normal).
My data model is as follows:
Location -> Images -> Recent Images -> Days Images Image -> User
recent_images and days_images same as images , but with the scope of where to filter by day and limit to 6.
Currently, this entire process takes about 15 seconds locally and 2-4 seconds in production. I feel like I can accomplish this much faster, but not quite sure how I can change my code.
Request to retrieve Location :
ids = @company .locations .active .has_image_taken .order(last_image_taken: :desc) .page(page) .per(per_page) .pluck(:id) Location.fetch_multi(ids)
fetch_multi from identity_cache gem. Then these results get into the serializer, which:
class V1::RecentLocationSerializer < ActiveModel::Serializer has_many :recent_images, serializer: V1::RecentImageSerializer do if scope[:view_user_photos] object.fetch_recent_images.take(6) else ids = object.recent_images.where(user_id: scope[:current_user].id).limit(6).pluck(:id) Image.fetch_multi(ids) end end has_many :days_images do if scope[:view_user_photos] object.fetch_days_images else ids = object.days_images.where(user_id: scope[:current_user].id).pluck(:id) Image.fetch_multi(ids) end end end
Areas for last and days of images:
scope :days_images, -> { includes(:user).active.where('date_uploaded > ?', DateTime.now.beginning_of_day).ordered_desc_by_date } scope :recent_images, -> { includes(:user).active.ordered_desc_by_date }
My question is, if you think I need to disable AMS, so I donβt need to query the serializer, and if so, how would you recommend it?