简体   繁体   中英

Rails Impressionist making expensive queries

I'm using charlotte-ruby/impressionist to track impressions within my rails app.

I have a very simple implementation, similar to what's shown in the quick start guide. Effectively: - Controller: impressionist actions: [:show, :index] - View: @post.impressionist_count - Model: is_impressionable

I ran into some DB queuing issues today, and found the following when checking out my expensive queries within Heroku Postgres:在此处输入图片说明

I immediately removed impressionist from my controllers/views (at 14:30), and you can see how it impacted performance:在此处输入图片说明

Has anyone run into a similar issue with the impressionist gem? Any ideas of why it's so expensive from a DB perspective?

EDIT:

Here are the indexes that were added:

  add_index "impressions", ["controller_name", "action_name", "ip_address"], name: "controlleraction_ip_index", using: :btree
  add_index "impressions", ["controller_name", "action_name", "request_hash"], name: "controlleraction_request_index", using: :btree
  add_index "impressions", ["controller_name", "action_name", "session_hash"], name: "controlleraction_session_index", using: :btree
  add_index "impressions", ["impressionable_type", "impressionable_id", "ip_address"], name: "poly_ip_index", using: :btree
  add_index "impressions", ["impressionable_type", "impressionable_id", "request_hash"], name: "poly_request_index", using: :btree
  add_index "impressions", ["impressionable_type", "impressionable_id", "session_hash"], name: "poly_session_index", using: :btree
  add_index "impressions", ["impressionable_type", "message", "impressionable_id"], name: "impressionable_type_message_index", using: :btree
  add_index "impressions", ["user_id"], name: "index_impressions_on_user_id", using: :btree

@Ken Hampson - thanks for the recommendation. Looking for the long running SELECT queries identified yet another index that was needed. Once added, POOF, all was better. Very odd that this issue just showed up after having the site live for a few months. Impressions must have hit a size that broke the camel's back. Appreciate the help!

For anyone that runs into similar issues with degrading performance due to DB queries (on Heroku), going to https://postgres.heroku.com/databases/your-database-name and looking at the "Expensive Queries" table is a great way to figure out where you may have missed an index.

The best solution for a large project (thousands/millions of users) is punching bag gem , go through the doc for integration : punching_bag

The major problem arises when with each request a new record is created which puts huge load on database. Gem Impressionist fails here. Punching bag too creates records but it has a rake task which comes to rescue :

Schedule a cron job to this rake task once a month/week to decrease (groups the records) the number of records without affecting the count of page views which can be found by simply - @post.hits . Here is the rake task : rake punching_bag:combine[by_hour_after,by_day_after,by_month_after,by_year_after]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM