Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
李少辉-开发者
gitlab-foss
提交
a0ba6c6c
G
gitlab-foss
项目概览
李少辉-开发者
/
gitlab-foss
通知
15
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
gitlab-foss
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a0ba6c6c
编写于
10月 30, 2015
作者:
Y
Yorick Peterse
浏览文件
操作
浏览文件
下载
差异文件
Merge branch 'optimize-user-find-by-any-email' into 'master'
Improve performance of User.find_by_any_email See merge request !1698
上级
49a73b6e
6d3068be
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
37 addition
and
15 deletion
+37
-15
CHANGELOG
CHANGELOG
+1
-0
app/models/user.rb
app/models/user.rb
+10
-15
spec/benchmarks/models/user_spec.rb
spec/benchmarks/models/user_spec.rb
+26
-0
未找到文件。
CHANGELOG
浏览文件 @
a0ba6c6c
...
...
@@ -2,6 +2,7 @@ Please view this file on the master branch, on stable branches it's out of date.
v 8.2.0 (unreleased)
- Force update refs/merge-requests/X/head upon a push to the source branch of a merge request (Stan Hu)
- Improved performance of finding users by one of their Email addresses
- Improved performance of replacing references in comments
- Show last project commit to default branch on project home page
- Highlight comment based on anchor in URL
...
...
app/models/user.rb
浏览文件 @
a0ba6c6c
...
...
@@ -235,21 +235,16 @@ class User < ActiveRecord::Base
# Find a User by their primary email or any associated secondary email
def
find_by_any_email
(
email
)
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 "emails"."email" = '<email>')
where
(
user_table
[
:email
].
eq
(
email
).
or
(
email_table
[
:email
].
eq
(
email
)))
find_by_sql
(
query
.
to_sql
).
first
sql
=
'SELECT *
FROM users
WHERE id IN (
SELECT id FROM users WHERE email = :email
UNION
SELECT emails.user_id FROM emails WHERE email = :email
)
LIMIT 1;'
User
.
find_by_sql
([
sql
,
{
email:
email
}]).
first
end
def
filter
(
filter_name
)
...
...
spec/benchmarks/models/user_spec.rb
浏览文件 @
a0ba6c6c
...
...
@@ -39,4 +39,30 @@ describe User, benchmark: true do
it
{
is_expected
.
to
iterate_per_second
(
iterations
)
}
end
end
describe
'.find_by_any_email'
do
let
(
:user
)
{
create
(
:user
)
}
describe
'using a user with only a single Email address'
do
let
(
:email
)
{
user
.
email
}
benchmark_subject
{
User
.
find_by_any_email
(
email
)
}
it
{
is_expected
.
to
iterate_per_second
(
1000
)
}
end
describe
'using a user with multiple Email addresses'
do
let
(
:email
)
{
user
.
emails
.
first
.
email
}
benchmark_subject
{
User
.
find_by_any_email
(
email
)
}
before
do
10
.
times
do
user
.
emails
.
create
(
email:
FFaker
::
Internet
.
email
)
end
end
it
{
is_expected
.
to
iterate_per_second
(
1000
)
}
end
end
end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录