From e98cc4b9605987862ff5ae90df7bdfd62a577baa Mon Sep 17 00:00:00 2001 From: Raphael Sofaer Date: Thu, 7 Jul 2011 15:14:29 -0700 Subject: [PATCH] Sort on and index taggings, ftw --- app/controllers/tags_controller.rb | 4 ++-- db/migrate/20110707221112_index_taggings_created_at.rb | 8 ++++++++ db/schema.rb | 3 ++- 3 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20110707221112_index_taggings_created_at.rb diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 9aa9616d9..efde15a51 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -59,12 +59,12 @@ class TagsController < ApplicationController @tag = ActsAsTaggableOn::Tag.where(:name => params[:name]).first if @tag @posts = @posts.joins("LEFT OUTER JOIN comments ON comments.post_id = posts.id"). - joins("INNER JOIN taggings ON (taggings.tag_id = #{@tag.id} AND + joins("INNER JOIN taggings ON (taggings.tag_id = #{@tag.id} AND ((taggable_id = posts.id AND taggable_type = 'Post') OR (taggings.taggable_type = 'Comment' AND taggings.taggable_id = comments.id)))") max_time = params[:max_time] ? Time.at(params[:max_time].to_i) : Time.now @posts = @posts.where(StatusMessage.arel_table[:created_at].lt(max_time)) - @posts = @posts.includes({:comments => {:author => :profile}}, :photos).order('posts.created_at DESC').limit(15) + @posts = @posts.includes({:comments => {:author => :profile}}, :photos).order('taggings.created_at DESC').limit(15) else @posts = [] end diff --git a/db/migrate/20110707221112_index_taggings_created_at.rb b/db/migrate/20110707221112_index_taggings_created_at.rb new file mode 100644 index 000000000..136d72d44 --- /dev/null +++ b/db/migrate/20110707221112_index_taggings_created_at.rb @@ -0,0 +1,8 @@ +class IndexTaggingsCreatedAt < ActiveRecord::Migration + def self.up + add_index :taggings, :created_at + end + + def self.down + end +end diff --git a/db/schema.rb b/db/schema.rb index a6d58dfb0..536e3167e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20110705003445) do +ActiveRecord::Schema.define(:version => 20110707221112) do create_table "aspect_memberships", :force => true do |t| t.integer "aspect_id", :null => false @@ -343,6 +343,7 @@ ActiveRecord::Schema.define(:version => 20110705003445) do t.datetime "created_at" end + add_index "taggings", ["created_at"], :name => "index_taggings_on_created_at" add_index "taggings", ["tag_id"], :name => "index_taggings_on_tag_id" add_index "taggings", ["taggable_id", "taggable_type", "context"], :name => "index_taggings_on_taggable_id_and_taggable_type_and_context" add_index "taggings", ["taggable_id", "taggable_type", "tag_id"], :name => "index_taggings_uniquely", :unique => true