From 27498a912bf182f6840b50d5b92c0bfbf2cfe287 Mon Sep 17 00:00:00 2001 From: Neil Lalonde Date: Wed, 31 Jul 2013 14:57:31 -0400 Subject: [PATCH] Add indexes to help IncomingLinksReport, but not much. Refactor to limit result set size, but it doesn't help perf much either. SELECT COUNT(DISTINCT ...) is still slow. --- app/models/incoming_links_report.rb | 22 ++++++++++--------- ...3035_add_report_index_to_incoming_links.rb | 6 +++++ 2 files changed, 18 insertions(+), 10 deletions(-) create mode 100644 db/migrate/20130731163035_add_report_index_to_incoming_links.rb diff --git a/app/models/incoming_links_report.rb b/app/models/incoming_links_report.rb index 34a984af2..77129ec89 100644 --- a/app/models/incoming_links_report.rb +++ b/app/models/incoming_links_report.rb @@ -62,8 +62,8 @@ class IncomingLinksReport report.y_titles[:num_users] = I18n.t("reports.#{report.type}.num_users") num_clicks = link_count_per_domain - num_topics = topic_count_per_domain - num_users = user_count_per_domain + num_topics = topic_count_per_domain(num_clicks.keys) + num_users = user_count_per_domain(num_clicks.keys) report.data = [] num_clicks.keys.each do |domain| report.data << {domain: domain, num_clicks: num_clicks[domain], num_topics: num_topics[domain], num_users: num_users[domain]} @@ -71,20 +71,22 @@ class IncomingLinksReport report.data = report.data.sort_by {|x| x[:num_clicks]}.reverse[0,10] end - def self.per_domain - @per_domain_query ||= IncomingLink.where('created_at > ? AND domain IS NOT NULL', 30.days.ago).group('domain') + def self.link_count_per_domain(limit=10) + IncomingLink.where('created_at > ? AND domain IS NOT NULL', 30.days.ago).group('domain').order('count_all DESC').limit(limit).count end - def self.link_count_per_domain - per_domain.count + def self.per_domain(domains) + IncomingLink.where('created_at > ? AND domain IN (?)', 30.days.ago, domains).group('domain') end - def self.topic_count_per_domain - per_domain.count('topic_id', distinct: true) + def self.topic_count_per_domain(domains) + # COUNT(DISTINCT) is slow + per_domain(domains).count('topic_id', distinct: true) end - def self.user_count_per_domain - per_domain.count('user_id', distinct: true) + def self.user_count_per_domain(domains) + # COUNT(DISTINCT) is slow + per_domain(domains).count('user_id', distinct: true) end diff --git a/db/migrate/20130731163035_add_report_index_to_incoming_links.rb b/db/migrate/20130731163035_add_report_index_to_incoming_links.rb new file mode 100644 index 000000000..7d0254169 --- /dev/null +++ b/db/migrate/20130731163035_add_report_index_to_incoming_links.rb @@ -0,0 +1,6 @@ +class AddReportIndexToIncomingLinks < ActiveRecord::Migration + def change + add_index :incoming_links, [:created_at, :user_id] + add_index :incoming_links, [:created_at, :domain] + end +end