From a8c1b0bc624a106f0c49cf863bb40057d3c172ab Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 19 May 2017 17:07:15 -0500 Subject: [PATCH 1/4] structure.sql: add forum topics (is_sticky, updated_at) index. --- db/structure.sql | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/db/structure.sql b/db/structure.sql index b9adfc198..9869c8562 100644 --- a/db/structure.sql +++ b/db/structure.sql @@ -6598,6 +6598,13 @@ CREATE INDEX index_forum_topic_visits_on_user_id ON forum_topic_visits USING btr CREATE INDEX index_forum_topics_on_creator_id ON forum_topics USING btree (creator_id); +-- +-- Name: index_forum_topics_on_is_sticky_and_updated_at; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX index_forum_topics_on_is_sticky_and_updated_at ON forum_topics USING btree (is_sticky, updated_at); + + -- -- Name: index_forum_topics_on_text_index; Type: INDEX; Schema: public; Owner: - -- @@ -7536,3 +7543,5 @@ INSERT INTO schema_migrations (version) VALUES ('20170428220448'); INSERT INTO schema_migrations (version) VALUES ('20170512221200'); +INSERT INTO schema_migrations (version) VALUES ('20170515235205'); + From d74eda2b96e49119c9a71d39b33843a413dc518a Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 19 May 2017 16:40:36 -0500 Subject: [PATCH 2/4] search: readd posts.id secondary sort to order: metatags. --- app/logical/post_query_builder.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/app/logical/post_query_builder.rb b/app/logical/post_query_builder.rb index f96544f74..552324017 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -392,28 +392,28 @@ class PostQueryBuilder relation = relation.order("posts.id DESC") when "score", "score_desc" - relation = relation.order("posts.score DESC") + relation = relation.order("posts.score DESC, posts.id DESC") when "score_asc" - relation = relation.order("posts.score ASC") + relation = relation.order("posts.score ASC, posts.id ASC") when "favcount" - relation = relation.order("posts.fav_count DESC") + relation = relation.order("posts.fav_count DESC, posts.id DESC") when "favcount_asc" - relation = relation.order("posts.fav_count ASC") + relation = relation.order("posts.fav_count ASC, posts.id ASC") when "change", "change_desc" - relation = relation.order("posts.updated_at DESC") + relation = relation.order("posts.updated_at DESC, posts.id DESC") when "change_asc" - relation = relation.order("posts.updated_at ASC") + relation = relation.order("posts.updated_at ASC, posts.id ASC") when "comment", "comm" - relation = relation.order("posts.last_commented_at DESC NULLS LAST") + relation = relation.order("posts.last_commented_at DESC NULLS LAST, posts.id DESC") when "comment_asc", "comm_asc" - relation = relation.order("posts.last_commented_at ASC NULLS LAST") + relation = relation.order("posts.last_commented_at ASC NULLS LAST, posts.id ASC") when "comment_bumped" relation = relation.order("posts.last_comment_bumped_at DESC NULLS LAST") From fad4d48b1c05a1c71ddf12ba04a09c2397bec193 Mon Sep 17 00:00:00 2001 From: evazion Date: Fri, 19 May 2017 17:04:51 -0500 Subject: [PATCH 3/4] 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'); + From 9bc4e1a5dfb51bfecb52bd5b97aab6e4cb951850 Mon Sep 17 00:00:00 2001 From: Albert Yi Date: Mon, 22 May 2017 11:38:37 -0700 Subject: [PATCH 4/4] Update 20170519204506_fix_last_noted_at_index_on_posts.rb --- db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb | 2 ++ 1 file changed, 2 insertions(+) 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 index fb10551e5..3b6bbf841 100644 --- a/db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb +++ b/db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb @@ -6,6 +6,8 @@ class FixLastNotedAtIndexOnPosts < ActiveRecord::Migration remove_index :posts, column: :last_noted_at add_index :posts, :last_noted_at, order: "DESC NULLS LAST" + + execute "analyze posts" end end end