From fad4d48b1c05a1c71ddf12ba04a09c2397bec193 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 19 May 2017 17:04:51 -0500 Subject: [PATCH] search: fix order:note, order:comment_bumped to use indexes. --- app/controllers/comments_controller.rb | 2 +- app/logical/post_query_builder.rb | 2 +- ...20170519204506_fix_last_noted_at_index_on_posts.rb | 11 +++++++++++ db/structure.sql | 6 ++++-- 4 files changed, 17 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index 97036688e..9dcd753d9 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -78,7 +78,7 @@ private end def index_by_post - @posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC").paginate(params[:page], :limit => 5, :search_count => params[:search]) + @posts = Post.where("last_comment_bumped_at IS NOT NULL").tag_match(params[:tags]).reorder("last_comment_bumped_at DESC NULLS LAST").paginate(params[:page], :limit => 5, :search_count => params[:search]) @posts.each # hack to force rails to eager load respond_with(@posts) do |format| format.html {render :action => "index_by_post"} diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index 552324017..6c5400321 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -419,7 +419,7 @@ class PostQueryBuilder relation = relation.order("posts.last_comment_bumped_at DESC NULLS LAST") when "comment_bumped_asc" - relation = relation.order("posts.last_comment_bumped_at ASC NULLS LAST") + relation = relation.order("posts.last_comment_bumped_at ASC NULLS FIRST") when "note" relation = relation.order("posts.last_noted_at DESC NULLS LAST") diff --git a/db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb b/db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb new file mode 100644 index 000000000..fb10551e5 --- /dev/null +++ b/db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb @@ -0,0 +1,11 @@ +class FixLastNotedAtIndexOnPosts < ActiveRecord::Migration + def up + Post.without_timeout do + remove_index :posts, column: :last_comment_bumped_at + add_index :posts, :last_comment_bumped_at, order: "DESC NULLS LAST" + + remove_index :posts, column: :last_noted_at + add_index :posts, :last_noted_at, order: "DESC NULLS LAST" + end + end +end diff --git a/db/structure.sql b/db/structure.sql index 9869c8562..1df387bc2 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -6896,14 +6896,14 @@ CREATE INDEX index_posts_on_image_width ON posts USING btree (image_width); -- Name: index_posts_on_last_comment_bumped_at; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_posts_on_last_comment_bumped_at ON posts USING btree (last_comment_bumped_at); +CREATE INDEX index_posts_on_last_comment_bumped_at ON posts USING btree (last_comment_bumped_at DESC NULLS LAST); -- -- Name: index_posts_on_last_noted_at; Type: INDEX; Schema: public; Owner: - -- -CREATE INDEX index_posts_on_last_noted_at ON posts USING btree (last_noted_at); +CREATE INDEX index_posts_on_last_noted_at ON posts USING btree (last_noted_at DESC NULLS LAST); -- @@ -7545,3 +7545,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170512221200'); INSERT INTO schema_migrations (version) VALUES ('20170515235205'); +INSERT INTO schema_migrations (version) VALUES ('20170519204506'); +