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 f96544f74..6c5400321 100644 --- a/app/logical/post_query_builder.rb +++ b/app/logical/post_query_builder.rb @@ -392,34 +392,34 @@ 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") 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..3b6bbf841 --- /dev/null +++ b/db/migrate/20170519204506_fix_last_noted_at_index_on_posts.rb @@ -0,0 +1,13 @@ +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" + + execute "analyze posts" + end + end +end diff --git a/db/structure.sql b/db/structure.sql index b9adfc198..1df387bc2 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: - -- @@ -6889,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); -- @@ -7536,3 +7543,7 @@ INSERT INTO schema_migrations (version) VALUES ('20170428220448'); INSERT INTO schema_migrations (version) VALUES ('20170512221200'); +INSERT INTO schema_migrations (version) VALUES ('20170515235205'); + +INSERT INTO schema_migrations (version) VALUES ('20170519204506'); +