Skip to content

Commit 27561a0

Browse files
committed
Optimize a SQL request (comments in footer)
1 parent d77f164 commit 27561a0

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

app/models/comment.rb

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ class Comment < ActiveRecord::Base
3131
scope :under, ->(path) { where("materialized_path LIKE ?", "#{path}_%") }
3232
scope :published, -> { where(state: 'published') }
3333
scope :on_dashboard, -> { published.order(created_at: :desc) }
34-
scope :footer, -> { published.joins(:node).merge(Node.visible).order(created_at: :desc).limit(12).select([:id, :node_id, :title]) }
34+
scope :footer, -> {
35+
# MariaDB tries to scan nodes first, which is a very slow, so we add an index hint
36+
from("comments USE INDEX (index_comments_on_state_and_created_at)").published.
37+
joins(:node).merge(Node.visible).
38+
order(created_at: :desc).
39+
limit(12).
40+
select([:id, :node_id, :title])
41+
}
3542

3643
validates :title, presence: { message: "Le titre est obligatoire" },
3744
length: { maximum: 100, message: "Le titre est trop long" }

0 commit comments

Comments
 (0)