Skip to content
代码片段 群组 项目
提交 6a82a693 编辑于 作者: fdegier's avatar fdegier 提交者: Fred de Gier
浏览文件

Model: Improve performance of with_version_count scope

Changelog: performance
上级 ab91dc15
No related branches found
No related tags found
2 合并请求!3031Merge per-main-jh to main-jh by luzhiyuan,!3030Merge per-main-jh to main-jh
......@@ -30,9 +30,12 @@ class Model < ApplicationRecord
scope :including_latest_version, -> { includes(:latest_version) }
scope :including_project, -> { includes(:project) }
scope :with_version_count, -> {
left_outer_joins(:versions)
.select("ml_models.*, count(ml_model_versions.id) as version_count")
.group(:id)
version_counts = Ml::ModelVersion
.select(:model_id, 'COUNT(*) as count')
.group(:model_id)
joins("LEFT OUTER JOIN (#{version_counts.to_sql}) as version_counts ON version_counts.model_id = ml_models.id")
.select('ml_models.*, COALESCE(version_counts.count, 0) as version_count')
}
scope :by_name, ->(name) { where("ml_models.name LIKE ?", "%#{sanitize_sql_like(name)}%") } # rubocop:disable GitlabSecurity/SqlInjection
scope :by_project, ->(project) { where(project_id: project.id) }
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册