Speed ​​up querying and rendering a large data array using AMS

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?

+5
source share
1 answer

Perhaps I skipped this point - which part is slow? What do magazines look like? Are you missing the db index? I don't see many connections there, so maybe you just need an index on date_uploaded (and maybe user_id). I don't see anything there, which does a bunch of serialization.

You can easily speed up sql in many ways - for example, with a trigger (in ruby ​​or sql) that updates the image with user_active boolean so you can reset this connection. (you must include this in the index)

OR create a cached table with only identifiers in it. Like an inverted index (I also do this in redis, but it's me), which has a row for each user / location, which is updated when the image is loaded.

Direct your work to downloading the image, and not where it is now by looking at the list.

+1
source

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


All Articles