简体   繁体   English

Rails 印象派进行昂贵的查询

[英]Rails Impressionist making expensive queries

I'm using charlotte-ruby/impressionist to track impressions within my rails app.我正在使用 charlotte-ruby/impressionist 来跟踪我的 rails 应用程序中的印象。

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有效地: - 控制器:印象派动作:[:show, :index] - 视图:@post.impressionist_count - 模型:is_impressionable

I ran into some DB queuing issues today, and found the following when checking out my expensive queries within Heroku Postgres:我今天遇到了一些数据库排队问题,在检查我在 Heroku Postgres 中的昂贵查询时发现了以下问题:在此处输入图片说明

I immediately removed impressionist from my controllers/views (at 14:30), and you can see how it impacted performance:我立即从我的控制器/视图中删除了印象派(在 14:30),您可以看到它如何影响性能:在此处输入图片说明

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. @Ken Hampson - 感谢您的推荐。 Looking for the long running SELECT queries identified yet another index that was needed.查找长时间运行的 SELECT 查询确定了另一个需要的索引。 Once added, POOF, all was better.一旦添加,POOF,一切都更好。 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.对于因数据库查询(在 Heroku 上)而遇到类似性能下降问题的任何人,转到https://postgres.heroku.com/databases/your-database-name并查看“昂贵的查询”表是一个很好的选择找出您可能错过索引的位置的方法。

The best solution for a large project (thousands/millions of users) is punching bag gem , go through the doc for integration : punching_bag大型项目(数千/数百万用户)的最佳解决方案是出气筒 gem ,通过文档进行集成: 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 .每月/每周为这个 rake 任务安排一个 cron 作业,以减少(分组记录)记录数量,而不会影响页面浏览次数,这可以通过简单的 @post.hits 找到。 Here is the rake task : rake punching_bag:combine[by_hour_after,by_day_after,by_month_after,by_year_after]这是耙子任务:耙子punching_bag:combine[by_hour_after,by_day_after,by_month_after,by_year_after]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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