From 18d032ad91b941079eb9e08a555554097adf6419 Mon Sep 17 00:00:00 2001 From: Guo Xiang Tan Date: Sat, 15 Oct 2016 01:13:58 +0800 Subject: [PATCH] PERF: Remove ordering by username. * Ordering by username results in a very expensive query for very little upside UX wise. --- app/controllers/directory_items_controller.rb | 3 +-- .../20161014171034_add_directory_items_indexes.rb | 11 +++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20161014171034_add_directory_items_indexes.rb diff --git a/app/controllers/directory_items_controller.rb b/app/controllers/directory_items_controller.rb index 70a729147c5..6158534b400 100644 --- a/app/controllers/directory_items_controller.rb +++ b/app/controllers/directory_items_controller.rb @@ -44,8 +44,7 @@ class DirectoryItemsController < ApplicationController end end - result = result.order('users.username') - result_count = result.dup.count + result_count = result.count result = result.limit(PAGE_SIZE).offset(PAGE_SIZE * page).to_a more_params = params.slice(:period, :order, :asc) diff --git a/db/migrate/20161014171034_add_directory_items_indexes.rb b/db/migrate/20161014171034_add_directory_items_indexes.rb new file mode 100644 index 00000000000..191aed0378f --- /dev/null +++ b/db/migrate/20161014171034_add_directory_items_indexes.rb @@ -0,0 +1,11 @@ +class AddDirectoryItemsIndexes < ActiveRecord::Migration + def change + add_index :directory_items, :likes_received + add_index :directory_items, :likes_given + add_index :directory_items, :topics_entered + add_index :directory_items, :topic_count + add_index :directory_items, :post_count + add_index :directory_items, :posts_read + add_index :directory_items, :days_visited + end +end