From 2a60f4dea86c9aa052d88de9c772e91750885a9a Mon Sep 17 00:00:00 2001
From: Phil Hughes <me@iamphill.com>
Date: Wed, 15 Feb 2017 10:42:54 +0000
Subject: [PATCH] Reduce query count for snippet search

Recudes the number of queries within the snippet search from approx. 50
to approx. 14 by preloading the authors

Part of #27392
---
 app/views/search/results/_snippet_blob.html.haml      | 2 +-
 app/views/search/results/_snippet_title.html.haml     | 2 +-
 changelogs/unreleased/snippets-search-performance.yml | 4 ++++
 lib/gitlab/snippet_search_results.rb                  | 4 ++--
 4 files changed, 8 insertions(+), 4 deletions(-)
 create mode 100644 changelogs/unreleased/snippets-search-performance.yml

diff --git a/app/views/search/results/_snippet_blob.html.haml b/app/views/search/results/_snippet_blob.html.haml
index f7808ea6aff..e977c1f1698 100644
--- a/app/views/search/results/_snippet_blob.html.haml
+++ b/app/views/search/results/_snippet_blob.html.haml
@@ -7,7 +7,7 @@
     = snippet.title
     by
     = link_to user_snippets_path(snippet.author) do
-      = image_tag avatar_icon(snippet.author_email), class: "avatar avatar-inline s16", alt: ''
+      = image_tag avatar_icon(snippet.author), class: "avatar avatar-inline s16", alt: ''
       = snippet.author_name
     %span.light= time_ago_with_tooltip(snippet.created_at)
   %h4.snippet-title
diff --git a/app/views/search/results/_snippet_title.html.haml b/app/views/search/results/_snippet_title.html.haml
index 704d1d01a81..026f404ce07 100644
--- a/app/views/search/results/_snippet_title.html.haml
+++ b/app/views/search/results/_snippet_title.html.haml
@@ -18,6 +18,6 @@
     %span
       by
       = link_to user_snippets_path(snippet_title.author) do
-        = image_tag avatar_icon(snippet_title.author_email), class: "avatar avatar-inline s16", alt: ''
+        = image_tag avatar_icon(snippet_title.author), class: "avatar avatar-inline s16", alt: ''
         = snippet_title.author_name
       %span.light= time_ago_with_tooltip(snippet_title.created_at)
diff --git a/changelogs/unreleased/snippets-search-performance.yml b/changelogs/unreleased/snippets-search-performance.yml
new file mode 100644
index 00000000000..2895478abfd
--- /dev/null
+++ b/changelogs/unreleased/snippets-search-performance.yml
@@ -0,0 +1,4 @@
+---
+title: Reduced query count for snippet search
+merge_request:
+author:
diff --git a/lib/gitlab/snippet_search_results.rb b/lib/gitlab/snippet_search_results.rb
index 9e01f02029c..b85f70e450e 100644
--- a/lib/gitlab/snippet_search_results.rb
+++ b/lib/gitlab/snippet_search_results.rb
@@ -31,11 +31,11 @@ module Gitlab
     private
 
     def snippet_titles
-      limit_snippets.search(query).order('updated_at DESC')
+      limit_snippets.search(query).order('updated_at DESC').includes(:author)
     end
 
     def snippet_blobs
-      limit_snippets.search_code(query).order('updated_at DESC')
+      limit_snippets.search_code(query).order('updated_at DESC').includes(:author)
     end
 
     def default_scope
-- 
GitLab