diff options
author | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-06-22 12:48:49 +0300 |
---|---|---|
committer | Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com> | 2015-06-22 12:48:49 +0300 |
commit | 29b6d465a7491e9660c91e324df7a7b7d9d03868 (patch) | |
tree | 5abebae07d20871c8eacec306fc803be63c05d92 /app/models/user.rb | |
parent | ea9dda9541caffe59714ff427918f674bec0a6f2 (diff) | |
parent | e17020b9079d6e4f349a1a01e5d43393b6b49f18 (diff) |
Merge branch 'rs-dev-issue-2355' into 'master'
MergeRequest#show performance improvements
This is a first pass on improving the performance of the `MergeRequests#show` page. Notable changes:
- The "Commits" tab is loaded lazily, so the initial page load should be much faster for MRs with many commits.
- Relative timestamps via `timeago` are only initialized once per load instead of `O(n^2)`. This greatly improves frontend rendering times for a large number of commits.
- Refactored `User.find_for_commit` to use a single ARel-generated SQL query instead of the old method which resulted in one query, and could result in up to three.
See merge request !838
Diffstat (limited to 'app/models/user.rb')
-rw-r--r-- | app/models/user.rb | 24 |
1 files changed, 20 insertions, 4 deletions
diff --git a/app/models/user.rb b/app/models/user.rb index a2e2d220b3a..29f43051464 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -223,10 +223,26 @@ class User < ActiveRecord::Base end def find_for_commit(email, name) - # Prefer email match over name match - User.where(email: email).first || - User.joins(:emails).where(emails: { email: email }).first || - User.where(name: name).first + user_table = arel_table + email_table = Email.arel_table + + # Use ARel to build a query: + query = user_table. + # SELECT "users".* FROM "users" + project(user_table[Arel.star]). + # LEFT OUTER JOIN "emails" + join(email_table, Arel::Nodes::OuterJoin). + # ON "users"."id" = "emails"."user_id" + on(user_table[:id].eq(email_table[:user_id])). + # WHERE ("user"."email" = '<email>' OR "user"."name" = '<name>') + # OR "emails"."email" = '<email>' + where( + user_table[:email].eq(email). + or(user_table[:name].eq(name)). + or(email_table[:email].eq(email)) + ) + + find_by_sql(query.to_sql).first end def filter(filter_name) |