Cap metric ordering of ML experiment runs at 20,000 runs

What does this MR do and why?

Behind the ml_candidate_metric_order_cap feature flag, experiments with more than 20,000 runs no longer sort their runs by a metric. MLflow runs/search rejects a metric order_by with 400 INVALID_PARAMETER_VALUE, and the experiment page uses the default order (newest first) with a notice. A metric sort looks up the latest metric value of every run in the experiment, so its cost grows with the experiment size: on the largest experiment on GitLab.com it reads 5.5 M buffers, 4.9 s warm and over a minute cold.

The 400 is a breaking API change for self-managed when the flag is removed. That MR needs the breaking change label and a Breaking Change Exception, like https://gitlab.com/gitlab-com/Product/-/work_items/14590.

Context for reviewers and agents

Behavior:

Flag off (today) Flag on
Experiment with 20,000 runs or fewer LATERAL metric order LATERAL metric order
20,001 to 50,000 runs LATERAL metric order MLflow: 400. Experiment page (HTML and CSV): default order (created_at DESC, id DESC), notice on the HTML page
More than 50,000 runs DISTINCT ON metric order MLflow: 400. Experiment page: default order, notice
  • Affected callers of Projects::Ml::CandidateFinder: the experiment page (Projects::Ml::ExperimentsController#show, HTML and CSV) and MLflow POST /api/v4/projects/:id/ml/mlflow/api/2.0/mlflow/runs/search with order_by=metrics.<name>. No GraphQL field uses the finder. A capped MLflow request returns the 400 before the query and before the search_mlflow_runs internal event. The message is "Ordering by a metric is not supported for experiments with more than 20000 runs".
  • Why 20,000: the lateral query reads about 6 buffers per run with the (candidate_id, name, id DESC) index from !272094 (merged), and about 16 without it. At 15,200 runs it takes 90 ms warm (91k buffers) with the index and 157 ms warm (249k buffers) without it. No experiment on GitLab.com has between 15,201 and 20,000 runs, so the 20,000 figures are extrapolated: about 120k buffers and 120 ms warm with the index, 330k buffers and 210 ms without it. At the current 50,000 limit it would be about 300k buffers and 300 ms warm with the index, and the first run would be above the statement timeout (7.0 s cold at 15,200 runs on Database Lab). On GitLab.com, the only experiment above 20,000 runs today is also the only one above 50,000, so 20,000 removes metric ordering from no experiment that has it today.
  • The cap counts every run of the experiment, also with a name filter. The LIKE name filter reads every run of the experiment too, so a filtered request on a large experiment is not cheaper.
  • The cap check is Ml::Experiment#more_candidates_than? (OFFSET 20000 LIMIT 1, an index-only scan). It runs once per metric-ordered request (strong_memoize_attr). more_candidates_than? stays after the flag is removed, because the cap uses it.
  • With the flag on, lateral_metric_order? is always true, so the DISTINCT ON branch, the lateral: keyword and LATERAL_METRIC_ORDER_MAX_CANDIDATES are reachable only with the flag off. They are removed with the flag (cleanup steps in the rollout issue).
  • The default order on the largest experiment still reads every run of the experiment (917k buffers, 267-393 ms warm, on page 1 and on a deep keyset page), because no index on ml_candidates covers (experiment_id, created_at). That is the existing cost of opening that experiment's page. It is 6 times fewer buffers than the lateral metric order. In a Database Lab clone, an (experiment_id, created_at DESC, id DESC) index brings both pages to 34-37 buffers and 1.4 ms. Follow-up: https://gitlab.com/gitlab-org/gitlab/-/work_items/641292.
  • A CSV request above the cap lists every run of the experiment, also runs that do not log the metric. A metric-ordered CSV lists only the runs that log the metric. Like every CSV, the rows are in id order, because CsvBuilder writes them in id batches. The CSV loads every run to build its header and value maps, and TARGET_FILESIZE limits only the output; a CSV without a sort of the same experiment does the same today. Before this MR, a metric-ordered CSV of the largest experiment ran the DISTINCT ON query with no LIMIT, which also returns every run, or it ran past the statement timeout. Follow-up for a bounded export: https://gitlab.com/gitlab-org/gitlab/-/work_items/641293.
  • Merge order: this MR merges first. The sibling !272094 (merged) adds the index that the lateral query uses, and it merges only after ml_candidate_metric_order_cap is at 100% on GitLab.com, as the rollout issue https://gitlab.com/gitlab-org/gitlab/-/work_items/640912 says. With that index, the flag-off DISTINCT ON query on the largest experiment scans the whole new index: 16.5 s instead of 6.3 s on the second run, above the 15 s statement timeout. Until the sibling merges, the flag-on lateral query runs without the index, which is the query that runs with the flag off today for experiments up to 50,000 runs.

Response above the cap and the breaking change

Caller Above the cap Breaking change?
MLflow runs/search with order_by=metrics.<name> 400 INVALID_PARAMETER_VALUE with a message Yes, when the flag is removed. The API style guide lists "Changing any status code other than 500", and experiments above 20,000 runs can return 200 today, mostly on self-managed. On GitLab.com one experiment is affected, and on Database Lab its metric sort runs past the statement timeout (500 to 400 is allowed).
Experiment page (HTML and CSV) Default order, with a notice on the HTML page Not an API.
  • With the flag, the change reaches only GitLab.com. Removing the flag (or enabling it by default) brings the 400 to self-managed, so that MR needs the breaking change label and an approved Breaking Change Exception, like https://gitlab.com/gitlab-com/Product/-/work_items/14590. The rollout issue https://gitlab.com/gitlab-org/gitlab/-/work_items/640912 says the same.
  • A 400 tells an MLflow client that asks for the best runs by a metric that the order is not available. The default order would return other runs without an error.
  • The MLflow client documentation and the experiment page documentation describe the cap in the flag removal MR.
  • The page's sort dropdown still shows the metric when the cap applies; the notice says that the runs are sorted by creation date.
Sources
  • doc/development/api_styleguide.md, "What is a breaking change" and "What is not a breaking change".
  • lib/api/ml/mlflow/runs.rb (search endpoint) and invalid_parameter! in lib/api/ml/mlflow/api_helpers.rb.

References

Screenshots or screen recordings

Experiment page sorted by the Accuracy metric, with the flag off (before) and on (after), for an experiment above the cap.

Before After
ml-metric-order-cap-before ml-metric-order-cap-after

How to set up and validate locally

  1. In app/finders/projects/ml/candidate_finder.rb, set METRIC_ORDER_MAX_CANDIDATES = 1 (development reloads the file).
  2. In the rails console, run Feature.enable(:ml_candidate_metric_order_cap).
  3. Open an experiment with at least two runs that log the same metric, for example http://gdk.test:3000/<group>/<project>/-/ml/experiments/<iid>, and sort by that metric. Expect: the notice, and the runs in newest-first order.
  4. Run glab api -X POST "projects/<project_id>/ml/mlflow/api/2.0/mlflow/runs/search" -f "experiment_ids[]=<iid>" -f "order_by=metrics.<name> DESC". Expect: 400 with {"error_code":"INVALID_PARAMETER_VALUE","message":"Ordering by a metric is not supported for experiments with more than 1 runs"}.
  5. Run Feature.disable(:ml_candidate_metric_order_cap) and repeat steps 3 and 4. Expect: no notice, 200, and the runs in metric order.

Feature flag and rollback

ml_candidate_metric_order_cap (gitlab_com_derisk, default off). Rollout: https://gitlab.com/gitlab-org/gitlab/-/work_items/640912. Rollback: disable the flag. No changelog entry: the change is behind a default-off flag.

Database

Query (Database Lab, gitlab-production-main) First run Second run
Cap check, largest experiment (911,000 runs) 6.1 ms, 394 buffers 4.5 ms, 394
Cap check, 15,200-run experiment 3.4 ms, 105 3.5 ms, 105
Default order after the cap, largest experiment 430 ms, 917k 393 ms, 917k
Default order after the cap, largest experiment, deep keyset page (row 455,000) 289 ms, 917k 267 ms, 917k
Default order, largest experiment, with an (experiment_id, created_at DESC, id DESC) index (clone only, follow-up), page 1 and the deep page 1.6-1.8 ms, 37 1.4 ms, 34-37
Lateral metric order at 15,200 runs, with the index from the sibling MR 7.0 s, 91k 92 ms, 91k
Lateral metric order at 15,200 runs, without the index (the state when this MR merges) 3.8 s, 249k 157 ms, 249k
Lateral metric order, largest experiment, with the index (what the cap avoids) 1.17 min, 5.47 M 4.9 s, 5.47 M
DISTINCT ON metric order (flag off), largest experiment, without the index 27.1 s, 4.41 M 6.3 s, 4.41 M
DISTINCT ON metric order (flag off), largest experiment, with the index from the sibling MR 34.3 s, 4.84 M 16.5 s, 4.84 M

Literals are replaced with placeholders. Planning time is 0.6 to 5.6 ms for every query, except one 19.7 ms run of the DISTINCT ON query with the index. ASC and DESC give the same buffers for the lateral query. The second run without the index at 15,200 runs is from 2026-11-19; the cold 3.8 s is from 2026-11-18, because the clone cache was already warm on 2026-11-19. Database Lab gave no plan links for these runs. Full plans of the lateral queries on the largest experiment are in the sibling MR.

Cap check, largest experiment (first run)
SELECT 1 AS one FROM ml_candidates WHERE ml_candidates.experiment_id = <experiment_id> LIMIT 1 OFFSET 20000
 Limit  (cost=553.31..553.34 rows=1 width=4) (actual time=5.349..5.350 rows=1 loops=1)
   Buffers: shared hit=393 read=1
   I/O Timings: read=0.898 write=0.000
   ->  Index Only Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..25000.89 rows=904369 width=4) (actual time=1.020..4.533 rows=20001 loops=1)
         Index Cond: (ml_candidates.experiment_id = <experiment_c_id>)
         Heap Fetches: 269
         Buffers: shared hit=393 read=1
         I/O Timings: read=0.898 write=0.000
Cap check, largest experiment (second run)
SELECT 1 AS one FROM ml_candidates WHERE ml_candidates.experiment_id = <experiment_id> LIMIT 1 OFFSET 20000
 Limit  (cost=553.31..553.34 rows=1 width=4) (actual time=3.827..3.828 rows=1 loops=1)
   Buffers: shared hit=394
   I/O Timings: read=0.000 write=0.000
   ->  Index Only Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..25000.89 rows=904369 width=4) (actual time=0.092..3.109 rows=20001 loops=1)
         Index Cond: (ml_candidates.experiment_id = <experiment_c_id>)
         Heap Fetches: 269
         Buffers: shared hit=394
         I/O Timings: read=0.000 write=0.000
Cap check, 15,200-run experiment
SELECT 1 AS one FROM ml_candidates WHERE ml_candidates.experiment_id = <experiment_id> LIMIT 1 OFFSET 20000
 Limit  (cost=420.46..420.48 rows=1 width=4) (actual time=2.696..2.696 rows=0 loops=1)
   Buffers: shared hit=105
   I/O Timings: read=0.000 write=0.000
   ->  Index Only Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..420.46 rows=15173 width=4) (actual time=0.072..2.106 rows=15200 loops=1)
         Index Cond: (ml_candidates.experiment_id = <experiment_b_id>)
         Heap Fetches: 5
         Buffers: shared hit=105
         I/O Timings: read=0.000 write=0.000
Default order after the cap, largest experiment
SELECT ml_candidates.* FROM ml_candidates WHERE ml_candidates.experiment_id = <experiment_c_id>
ORDER BY ml_candidates.created_at DESC, ml_candidates.id DESC LIMIT 31
 Limit  (cost=84763.99..84767.60 rows=31 width=141) (actual time=372.204..392.080 rows=31 loops=1)
   Buffers: shared hit=916761
   I/O Timings: read=0.000 write=0.000
   ->  Gather Merge  (cost=84763.99..172694.79 rows=753640 width=141) (actual time=372.202..392.075 rows=31 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         Buffers: shared hit=916761
         I/O Timings: read=0.000 write=0.000
         ->  Sort  (cost=83763.96..84706.01 rows=376820 width=141) (actual time=369.054..369.059 rows=25 loops=3)
               Sort Key: ml_candidates.created_at DESC, ml_candidates.id DESC
               Sort Method: top-N heapsort  Memory: 39kB
               Buffers: shared hit=916761
               I/O Timings: read=0.000 write=0.000
               ->  Parallel Index Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..72545.66 rows=376820 width=141) (actual time=0.048..270.860 rows=303684 loops=3)
                     Index Cond: (ml_candidates.experiment_id = <experiment_c_id>)
                     Buffers: shared hit=916725
                     I/O Timings: read=0.000 write=0.000
Default order after the cap, largest experiment, deep keyset page (second run)
SELECT ml_candidates.* FROM ml_candidates
WHERE ml_candidates.experiment_id = <experiment_c_id>
  AND ((ml_candidates.created_at, ml_candidates.id) < ('<cursor_created_at>', <cursor_id>))
ORDER BY ml_candidates.created_at DESC, ml_candidates.id DESC LIMIT 31
 Limit  (cost=81560.38..81564.00 rows=31 width=141) (actual time=257.411..265.928 rows=31 loops=1)
   Buffers: shared hit=916758
   I/O Timings: read=0.000 write=0.000
   ->  Gather Merge  (cost=81560.38..127682.17 rows=395302 width=141) (actual time=257.410..265.924 rows=31 loops=1)
         Workers Planned: 2
         Workers Launched: 2
         Buffers: shared hit=916758
         I/O Timings: read=0.000 write=0.000
         ->  Sort  (cost=80560.36..81054.49 rows=197651 width=141) (actual time=254.335..254.340 rows=25 loops=3)
               Sort Key: ml_candidates.created_at DESC, ml_candidates.id DESC
               Sort Method: top-N heapsort  Memory: 39kB
               Buffers: shared hit=916758
               I/O Timings: read=0.000 write=0.000
               ->  Parallel Index Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..74676.09 rows=197651 width=141) (actual time=0.064..207.580 rows=152017 loops=3)
                     Index Cond: (ml_candidates.experiment_id = <experiment_c_id>)
                     Filter: (ROW(ml_candidates.created_at, ml_candidates.id) < ROW('<cursor_created_at>'::timestamp with time zone, <cursor_id>))
                     Rows Removed by Filter: 151667
                     Buffers: shared hit=916722
                     I/O Timings: read=0.000 write=0.000
Default order, deep keyset page, with an (experiment_id, created_at DESC, id DESC) index (clone only, second run)
CREATE INDEX tmp_idx_ml_candidates_experiment_created_id ON ml_candidates (experiment_id, created_at DESC, id DESC);
-- then the deep keyset page query above
 Limit  (cost=0.43..3.70 rows=31 width=141) (actual time=0.028..0.101 rows=31 loops=1)
   Buffers: shared hit=34
   I/O Timings: read=0.000 write=0.000
   ->  Index Scan using tmp_idx_ml_candidates_experiment_created_id on public.ml_candidates  (cost=0.43..50433.67 rows=477533 width=141) (actual time=0.027..0.097 rows=31 loops=1)
         Index Cond: ((ml_candidates.experiment_id = <experiment_c_id>) AND (ROW(ml_candidates.created_at, ml_candidates.id) < ROW('<cursor_created_at>'::timestamp with time zone, <cursor_id>)))
         Buffers: shared hit=34
         I/O Timings: read=0.000 write=0.000
Lateral metric order at 15,200 runs, without the index (second run)
SELECT ml_candidates.*, latest.value AS metric_value
FROM ml_candidates
INNER JOIN LATERAL (
  SELECT ml_candidate_metrics.candidate_id, ml_candidate_metrics.value
  FROM ml_candidate_metrics
  WHERE ml_candidate_metrics.name = '<metric>'
    AND ml_candidate_metrics.candidate_id = ml_candidates.id
  ORDER BY ml_candidate_metrics.id DESC
  LIMIT 1
) latest ON latest.candidate_id = ml_candidates.id
WHERE ml_candidates.experiment_id = <experiment_id>
ORDER BY latest.value DESC NULLS LAST, ml_candidates.id DESC
LIMIT 31
 Limit  (cost=1449064.30..1449064.38 rows=31 width=149) (actual time=155.976..155.982 rows=31 loops=1)
   Buffers: shared hit=249034
   I/O Timings: read=0.000 write=0.000
   ->  Sort  (cost=1449064.30..1449064.49 rows=75 width=149) (actual time=155.975..155.979 rows=31 loops=1)
         Sort Key: latest.value DESC NULLS LAST, ml_candidates.id DESC
         Sort Method: top-N heapsort  Memory: 37kB
         Buffers: shared hit=249034
         I/O Timings: read=0.000 write=0.000
         ->  Nested Loop  (cost=95.93..1449062.07 rows=75 width=149) (actual time=0.132..150.140 rows=15199 loops=1)
               Buffers: shared hit=249028
               I/O Timings: read=0.000 write=0.000
               ->  Index Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..10440.11 rows=15060 width=141) (actual time=0.035..11.355 rows=15200 loops=1)
                     Index Cond: (ml_candidates.experiment_id = <experiment_b_id>)
                     Buffers: shared hit=15274
                     I/O Timings: read=0.000 write=0.000
               ->  Subquery Scan on latest  (cost=95.50..95.52 rows=1 width=16) (actual time=0.009..0.009 rows=1 loops=15200)
                     Filter: (ml_candidates.id = latest.candidate_id)
                     Buffers: shared hit=233754
                     I/O Timings: read=0.000 write=0.000
                     ->  Limit  (cost=95.50..95.50 rows=1 width=24) (actual time=0.009..0.009 rows=1 loops=15200)
                           Buffers: shared hit=233754
                           I/O Timings: read=0.000 write=0.000
                           ->  Sort  (cost=95.50..95.50 rows=1 width=24) (actual time=0.008..0.008 rows=1 loops=15200)
                                 Sort Key: ml_candidate_metrics.id DESC
                                 Sort Method: quicksort  Memory: 25kB
                                 Buffers: shared hit=233754
                                 I/O Timings: read=0.000 write=0.000
                                 ->  Index Scan using index_ml_candidate_metrics_on_candidate_id on public.ml_candidate_metrics  (cost=0.44..95.49 rows=1 width=24) (actual time=0.005..0.008 rows=1 loops=15200)
                                       Index Cond: (ml_candidate_metrics.candidate_id = ml_candidates.id)
                                       Filter: (ml_candidate_metrics.name = '<metric>'::text)
                                       Rows Removed by Filter: 12
                                       Buffers: shared hit=233754
                                       I/O Timings: read=0.000 write=0.000
Lateral metric order at 15,200 runs, with the index (warm)
SELECT ml_candidates.*, latest.value AS metric_value
FROM ml_candidates
INNER JOIN LATERAL (
  SELECT ml_candidate_metrics.candidate_id, ml_candidate_metrics.value
  FROM ml_candidate_metrics
  WHERE ml_candidate_metrics.name = '<metric>'
    AND ml_candidate_metrics.candidate_id = ml_candidates.id
  ORDER BY ml_candidate_metrics.id DESC
  LIMIT 1
) latest ON latest.candidate_id = ml_candidates.id
WHERE ml_candidates.experiment_id = <experiment_id>
ORDER BY latest.value DESC NULLS LAST, ml_candidates.id DESC
LIMIT 31
 Limit  (cost=65251.22..65251.29 rows=31 width=149) (actual time=89.933..89.940 rows=31 loops=1)
   Buffers: shared hit=91279
   I/O Timings: read=0.000 write=0.000
   ->  Sort  (cost=65251.22..65251.41 rows=76 width=149) (actual time=89.933..89.936 rows=31 loops=1)
         Sort Key: latest.value DESC NULLS LAST, ml_candidates.id DESC
         Sort Method: top-N heapsort  Memory: 37kB
         Buffers: shared hit=91279
         I/O Timings: read=0.000 write=0.000
         ->  Nested Loop  (cost=0.99..65248.95 rows=76 width=149) (actual time=0.075..84.101 rows=15199 loops=1)
               Buffers: shared hit=91273
               I/O Timings: read=0.000 write=0.000
               ->  Index Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..10512.36 rows=15173 width=141) (actual time=0.033..11.942 rows=15200 loops=1)
                     Index Cond: (ml_candidates.experiment_id = <experiment_b_id>)
                     Buffers: shared hit=15274
                     I/O Timings: read=0.000 write=0.000
               ->  Subquery Scan on latest  (cost=0.56..3.60 rows=1 width=16) (actual time=0.004..0.004 rows=1 loops=15200)
                     Filter: (ml_candidates.id = latest.candidate_id)
                     Buffers: shared hit=75999
                     I/O Timings: read=0.000 write=0.000
                     ->  Limit  (cost=0.56..3.58 rows=1 width=24) (actual time=0.004..0.004 rows=1 loops=15200)
                           Buffers: shared hit=75999
                           I/O Timings: read=0.000 write=0.000
                           ->  Index Scan using index_ml_candidate_metrics_on_candidate_id_name_id_desc on public.ml_candidate_metrics  (cost=0.56..3.58 rows=1 width=24) (actual time=0.004..0.004 rows=1 loops=15200)
                                 Index Cond: ((ml_candidate_metrics.candidate_id = ml_candidates.id) AND (ml_candidate_metrics.name = '<metric>'::text))
                                 Buffers: shared hit=75999
                                 I/O Timings: read=0.000 write=0.000
Lateral metric order, largest experiment, with the index (second run, LIMIT 21)
SELECT ml_candidates.*, latest.value AS metric_value
FROM ml_candidates
INNER JOIN LATERAL (
  SELECT ml_candidate_metrics.candidate_id, ml_candidate_metrics.value
  FROM ml_candidate_metrics
  WHERE ml_candidate_metrics.name = '<metric>'
    AND ml_candidate_metrics.candidate_id = ml_candidates.id
  ORDER BY ml_candidate_metrics.id DESC
  LIMIT 1
) latest ON latest.candidate_id = ml_candidates.id
WHERE ml_candidates.experiment_id = <experiment_id>
ORDER BY latest.value DESC NULLS LAST, ml_candidates.id DESC
LIMIT 21
 Limit  (cost=1357330.90..1357330.96 rows=21 width=149) (actual time=4874.804..4874.809 rows=21 loops=1)
   Buffers: shared hit=5471844
   I/O Timings: read=0.000 write=0.000
   ->  Sort  (cost=1357330.90..1357342.30 rows=4558 width=149) (actual time=4874.803..4874.806 rows=21 loops=1)
         Sort Key: latest.value DESC NULLS LAST, ml_candidates.id DESC
         Sort Method: top-N heapsort  Memory: 35kB
         Buffers: shared hit=5471844
         I/O Timings: read=0.000 write=0.000
         ->  Nested Loop  (cost=0.99..1357208.01 rows=4558 width=149) (actual time=0.039..4524.843 rows=910901 loops=1)
               Buffers: shared hit=5471838
               I/O Timings: read=0.000 write=0.000
               ->  Index Scan using index_ml_candidates_on_experiment_id_and_eid on public.ml_candidates  (cost=0.43..77942.18 rows=911542 width=141) (actual time=0.023..950.270 rows=911053 loops=1)
                     Index Cond: (ml_candidates.experiment_id = <experiment_c_id>)
                     Buffers: shared hit=916725
                     I/O Timings: read=0.000 write=0.000
               ->  Subquery Scan on latest  (cost=0.56..1.39 rows=1 width=16) (actual time=0.004..0.004 rows=1 loops=911053)
                     Filter: (ml_candidates.id = latest.candidate_id)
                     Buffers: shared hit=4555113
                     I/O Timings: read=0.000 write=0.000
                     ->  Limit  (cost=0.56..1.38 rows=1 width=24) (actual time=0.003..0.003 rows=1 loops=911053)
                           Buffers: shared hit=4555113
                           I/O Timings: read=0.000 write=0.000
                           ->  Index Scan using index_ml_candidate_metrics_on_candidate_id_name_id_desc on public.ml_candidate_metrics  (cost=0.56..15.25 rows=18 width=24) (actual time=0.003..0.003 rows=1 loops=911053)
                                 Index Cond: ((ml_candidate_metrics.candidate_id = ml_candidates.id) AND (ml_candidate_metrics.name = '<metric>'::text))
                                 Buffers: shared hit=4555113
                                 I/O Timings: read=0.000 write=0.000
DISTINCT ON metric order (flag off), largest experiment, without the index (second run)
SELECT ml_candidates.*, latest.value AS metric_value
FROM ml_candidates
INNER JOIN (
  SELECT DISTINCT ON (candidate_id, name) *
  FROM ml_candidate_metrics
  WHERE ml_candidate_metrics.name = '<metric>'
  ORDER BY candidate_id, name, id DESC
) latest ON latest.candidate_id = ml_candidates.id
WHERE ml_candidates.experiment_id = <experiment_id>
ORDER BY latest.value DESC NULLS LAST, ml_candidates.id DESC
LIMIT 31
 Limit  (cost=3549711.91..3549711.99 rows=31 width=149) (actual time=6329.279..6331.257 rows=31 loops=1)
   Buffers: shared hit=3659532 read=751882
   I/O Timings: read=7498.628 write=0.000
   ->  Sort  (cost=3549711.91..3549809.28 rows=38947 width=149) (actual time=6329.278..6331.253 rows=31 loops=1)
         Sort Key: ml_candidate_metrics.value DESC NULLS LAST, ml_candidates.id DESC
         Sort Method: top-N heapsort  Memory: 41kB
         Buffers: shared hit=3659532 read=751882
         I/O Timings: read=7498.628 write=0.000
         ->  Nested Loop  (cost=3372985.79..3548552.42 rows=38947 width=149) (actual time=4154.713..6042.312 rows=910901 loops=1)
               Buffers: shared hit=3659529 read=751882
               I/O Timings: read=7498.628 write=0.000
               ->  Unique  (cost=3372985.36..3482131.52 rows=52061 width=114) (actual time=4154.642..4572.101 rows=910901 loops=1)
                     Buffers: shared hit=15925 read=751882
                     I/O Timings: read=7498.628 write=0.000
                     ->  Gather Merge  (cost=3372985.36..3479837.88 rows=917453 width=114) (actual time=4154.640..4373.058 rows=910951 loops=1)
                           Workers Planned: 2
                           Workers Launched: 2
                           Buffers: shared hit=15925 read=751882
                           I/O Timings: read=7498.628 write=0.000
                           ->  Sort  (cost=3371985.34..3372941.02 rows=382272 width=114) (actual time=4150.920..4180.452 rows=303650 loops=3)
                                 Sort Key: ml_candidate_metrics.candidate_id, ml_candidate_metrics.id DESC
                                 Sort Method: quicksort  Memory: 36449kB
                                 Buffers: shared hit=15925 read=751882
                                 I/O Timings: read=7498.628 write=0.000
                                 ->  Parallel Seq Scan on public.ml_candidate_metrics  (cost=0.00..3336540.62 rows=382272 width=114) (actual time=107.249..4049.079 rows=303650 loops=3)
                                       Filter: (ml_candidate_metrics.name = '<metric>'::text)
                                       Rows Removed by Filter: 16698479
                                       Buffers: shared hit=15825 read=751882
                                       I/O Timings: read=7498.628 write=0.000
               ->  Index Scan using ml_candidates_pkey on public.ml_candidates  (cost=0.43..1.27 rows=1 width=141) (actual time=0.001..0.001 rows=1 loops=910901)
                     Index Cond: (ml_candidates.id = ml_candidate_metrics.candidate_id)
                     Filter: (ml_candidates.experiment_id = <experiment_c_id>)
                     Buffers: shared hit=3643604
                     I/O Timings: read=0.000 write=0.000
DISTINCT ON metric order (flag off), largest experiment, with the index from the sibling MR (second run)
SELECT ml_candidates.*, latest.value AS metric_value
FROM ml_candidates
INNER JOIN (
  SELECT DISTINCT ON (candidate_id, name) *
  FROM ml_candidate_metrics
  WHERE ml_candidate_metrics.name = '<metric>'
  ORDER BY candidate_id, name, id DESC
) latest ON latest.candidate_id = ml_candidates.id
WHERE ml_candidates.experiment_id = <experiment_id>
ORDER BY latest.value DESC NULLS LAST, ml_candidates.id DESC
LIMIT 31
 Limit  (cost=1442584.62..1442584.70 rows=31 width=149) (actual time=16450.053..16450.060 rows=31 loops=1)
   Buffers: shared hit=4370883 read=472476
   I/O Timings: read=11540.481 write=0.000
   ->  Sort  (cost=1442584.62..1442682.83 rows=39284 width=149) (actual time=16450.052..16450.056 rows=31 loops=1)
         Sort Key: ml_candidate_metrics.value DESC NULLS LAST, ml_candidates.id DESC
         Sort Method: top-N heapsort  Memory: 41kB
         Buffers: shared hit=4370883 read=472476
         I/O Timings: read=11540.481 write=0.000
         ->  Nested Loop  (cost=0.99..1441415.10 rows=39284 width=149) (actual time=2000.504..16114.778 rows=910901 loops=1)
               Buffers: shared hit=4370877 read=472476
               I/O Timings: read=11540.481 write=0.000
               ->  Unique  (cost=0.56..1374803.87 rows=52477 width=114) (actual time=1997.509..8012.488 rows=910901 loops=1)
                     Buffers: shared hit=750809 read=448940
                     I/O Timings: read=5178.560 write=0.000
                     ->  Index Scan using index_ml_candidate_metrics_on_candidate_id_name_id_desc on public.ml_candidate_metrics  (cost=0.56..1372565.75 rows=895250 width=114) (actual time=1997.507..7771.168 rows=910951 loops=1)
                           Index Cond: (ml_candidate_metrics.name = '<metric>'::text)
                           Buffers: shared hit=750809 read=448940
                           I/O Timings: read=5178.560 write=0.000
               ->  Index Scan using ml_candidates_pkey on public.ml_candidates  (cost=0.43..1.26 rows=1 width=141) (actual time=0.009..0.009 rows=1 loops=910901)
                     Index Cond: (ml_candidates.id = ml_candidate_metrics.candidate_id)
                     Filter: (ml_candidates.experiment_id = <experiment_c_id>)
                     Buffers: shared hit=3620068 read=23536
                     I/O Timings: read=6361.921 write=0.000

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Eduardo Bonet

Merge request reports

Loading
Loading