Skip to content

Commit 2946f19

Browse files
Optimize run_by query to avoid full scan of experiment_crates table
The query plan was previously: QUERY PLAN |--SCAN TABLE experiment_crates `--SEARCH TABLE experiments USING INDEX sqlite_autoindex_experiments_1 (name=?) and now is: QUERY PLAN |--SEARCH TABLE experiments USING INDEX sqlite_autoindex_experiments_1 (name=?) `--LIST SUBQUERY 2 |--SEARCH TABLE experiment_crates USING INDEX experiment_crates__experiment_skipped (experiment=? AND skipped=?) `--LIST SUBQUERY 1 `--SCAN TABLE experiments While the new query does search more, it does so in a way that is significantly more efficient, as it can avoid scanning the whole experiment_crates table (which is quite large).
1 parent e3af9ea commit 2946f19

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/experiments.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -282,11 +282,11 @@ impl Experiment {
282282

283283
pub fn run_by(db: &Database, assignee: &Assignee) -> Fallible<Option<Experiment>> {
284284
let record = db.get_row(
285-
"SELECT * FROM experiments \
286-
INNER JOIN experiment_crates ON experiment_crates.experiment \
287-
= experiments.name WHERE experiment_crates.assigned_to = ?1 \
288-
AND experiment_crates.status = ?2 AND experiments.status = ?2 \
289-
AND experiment_crates.skipped = 0 LIMIT 1",
285+
"select * from experiments where name in ( \
286+
select experiment from experiment_crates \
287+
where status = ?2 and skipped = 0 and assigned_to = ?1 and \
288+
experiment in (select name from experiments where status = ?2)) \
289+
limit 1",
290290
&[&assignee.to_string(), Status::Running.to_str()],
291291
|r| ExperimentDBRecord::from_row(r),
292292
)?;

0 commit comments

Comments
 (0)