Skip to content

Commit

Permalink
use an index instead
Browse files Browse the repository at this point in the history
  • Loading branch information
hawkw committed Jul 29, 2024
1 parent cc88786 commit 161874d
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 18 deletions.
25 changes: 10 additions & 15 deletions dev-tools/omdb/src/bin/omdb/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4247,21 +4247,16 @@ async fn cmd_db_migrations_list(
deleted: Option<chrono::DateTime<Utc>>,
}

let migrations = datastore
.pool_connection_for_tests()
.await?
.transaction_async(|conn| async move {
// Selecting all migration records requires a full table scan
conn.batch_execute_async(ALLOW_FULL_TABLE_SCAN_SQL).await?;
query
.limit(i64::from(u32::from(fetch_opts.fetch_limit)))
.order_by(dsl::time_created)
.select(Migration::as_select())
.load_async(&conn)
.await
.context("listing migrations")
})
.await?;
let migrations = query
.limit(i64::from(u32::from(fetch_opts.fetch_limit)))
.order_by(dsl::time_created)
// This is just to prove to CRDB that it can use the
// migrations-by-time-created index, it doesn't actually do anything.
.filter(dsl::time_created.gt(chrono::DateTime::UNIX_EPOCH))
.select(Migration::as_select())
.load_async(&*datastore.pool_connection_for_tests().await?)
.await
.context("listing migrations")?;

check_limit(&migrations, fetch_opts.fetch_limit, || "listing migrations");

Expand Down
5 changes: 3 additions & 2 deletions nexus/db-model/src/schema_versions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use std::collections::BTreeMap;
///
/// This must be updated when you change the database schema. Refer to
/// schema/crdb/README.adoc in the root of this repository for details.
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(84, 0, 0);
pub const SCHEMA_VERSION: SemverVersion = SemverVersion::new(85, 0, 0);

/// List of all past database schema versions, in *reverse* order
///
Expand All @@ -29,7 +29,8 @@ static KNOWN_VERSIONS: Lazy<Vec<KnownVersion>> = Lazy::new(|| {
// | leaving the first copy as an example for the next person.
// v
// KnownVersion::new(next_int, "unique-dirname-with-the-sql-files"),
KnownVersion::new(84, "region-read-only"),
KnownVersion::new(85, "region-read-only"),
KnownVersion::new(84, "add-migrations-by-time-created-index"),
KnownVersion::new(83, "dataset-address-optional"),
KnownVersion::new(82, "region-port"),
KnownVersion::new(81, "add-nullable-filesystem-pool"),
Expand Down
9 changes: 9 additions & 0 deletions schema/crdb/add-migrations-by-time-created-index/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

/* Migrations by time created.
*
* Currently, this is only used by OMDB for ordering the `omdb migration list`
* output, but it may be used by other UIs in the future...
*/
CREATE INDEX IF NOT EXISTS migrations_by_time_created ON omicron.public.migration (
time_created
);
11 changes: 10 additions & 1 deletion schema/crdb/dbinit.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4131,6 +4131,15 @@ CREATE INDEX IF NOT EXISTS lookup_migrations_by_instance_id ON omicron.public.mi
instance_id
);

/* Migrations by time created.
*
* Currently, this is only used by OMDB for ordering the `omdb migration list`
* output, but it may be used by other UIs in the future...
*/
CREATE INDEX IF NOT EXISTS migrations_by_time_created ON omicron.public.migration (
time_created
);

/* Lookup region snapshot by snapshot id */
CREATE INDEX IF NOT EXISTS lookup_region_snapshot_by_snapshot_id on omicron.public.region_snapshot (
snapshot_id
Expand All @@ -4147,7 +4156,7 @@ INSERT INTO omicron.public.db_metadata (
version,
target_version
) VALUES
(TRUE, NOW(), NOW(), '84.0.0', NULL)
(TRUE, NOW(), NOW(), '85.0.0', NULL)
ON CONFLICT DO NOTHING;

COMMIT;

0 comments on commit 161874d

Please sign in to comment.