Skip to content

Adds candidate count to experiments

Eduardo Bonet requested to merge mlops/add-candidate-per-exp-count into master

What does this MR do and why?

This is a preparation to improve the UI on Experiments page, which includes showing the candidate count of experiments #387509 (closed)

Database

SELECT
    ml_experiments.*,
    count(ml_candidates.id) AS candidate_count
FROM
    "ml_experiments"
    LEFT OUTER JOIN "ml_candidates" ON "ml_candidates"."experiment_id" = "ml_experiments"."id"
GROUP BY
    "ml_experiments"."id"

Execution Plan https://postgres.ai/console/gitlab/gitlab-production-tunnel-pg12/sessions/14866/commands/51924

 HashAggregate  (cost=8.66..8.72 rows=6 width=96) (actual time=5.521..5.527 rows=6 loops=1)
   Group Key: ml_experiments.id
   Buffers: shared read=2
   I/O Timings: read=5.419 write=0.000
   ->  Hash Right Join  (cost=4.13..8.51 rows=29 width=96) (actual time=5.481..5.502 rows=30 loops=1)
         Hash Cond: (ml_candidates.experiment_id = ml_experiments.id)
         Buffers: shared read=2
         I/O Timings: read=5.419 write=0.000
         ->  Seq Scan on public.ml_candidates  (cost=0.00..4.29 rows=29 width=16) (actual time=4.729..4.735 rows=29 loops=1)
               Buffers: shared read=1
               I/O Timings: read=4.712 write=0.000
         ->  Hash  (cost=4.06..4.06 rows=6 width=88) (actual time=0.735..0.736 rows=6 loops=1)
               Buckets: 1024  Batches: 1  Memory Usage: 9kB
               Buffers: shared read=1
               I/O Timings: read=0.707 write=0.000
               ->  Seq Scan on public.ml_experiments  (cost=0.00..4.06 rows=6 width=88) (actual time=0.722..0.723 rows=6 loops=1)
                     Buffers: shared read=1
                     I/O Timings: read=0.707 write=0.000

How to set up and validate locally

  1. Create data

    # Added variables
    project_id = 1
    
    exp1 = Ml::Experiment.create!(name: "Awesome Gitlab Experiment", user_id: nil, project_id: project_id)
    exp2 = Ml::Experiment.create!(name: "Awesome Gitlab Experiment2", user_id: nil, project_id: project_id)
    exp1.candidates.create!(user_id: nil, start_time: 0, name: "candidate_1") }
    exp1.candidates.create!(user_id: nil, start_time: 0, name: "candidate_2") }
  2. Fetch

    exps = Ml::Experiments.where(project_id: project_id).with_candidate_count
    exps[0].candidate_count # should be 2
    exps[1].candidate_count # should be 0

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Eduardo Bonet

Merge request reports