-
Notifications
You must be signed in to change notification settings - Fork 43
fix(migration): add missing FK indexes for query performance #2276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
imurphy-rh
wants to merge
1
commit into
guacsec:main
Choose a base branch
from
imurphy-rh:fix/perf-fk-indexes
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| use sea_orm_migration::prelude::*; | ||
|
|
||
| #[derive(DeriveMigrationName)] | ||
| pub struct Migration; | ||
|
|
||
| #[async_trait::async_trait] | ||
| #[allow(deprecated)] | ||
| impl MigrationTrait for Migration { | ||
| async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| // product_version.sbom_id — used in analysis graph loading: | ||
| // LEFT JOIN product_version ON sbom.sbom_id = product_version.sbom_id | ||
| // Without this index, every SBOM graph load triggers a sequential scan | ||
| // of the entire product_version table. | ||
| manager | ||
| .create_index( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .table(ProductVersion::Table) | ||
| .name(Indexes::ProductVersionSbomIdIdx.to_string()) | ||
| .col(ProductVersion::SbomId) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| // product_version.product_id — used in analysis graph loading: | ||
| // LEFT JOIN product ON product_version.product_id = product.id | ||
| manager | ||
| .create_index( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .table(ProductVersion::Table) | ||
| .name(Indexes::ProductVersionProductIdIdx.to_string()) | ||
| .col(ProductVersion::ProductId) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| // package_relates_to_package (sbom_id, relationship) — used in CPE | ||
| // context filter SQL and SBOM advisory queries: | ||
| // WHERE sbom_id = $1 AND relationship = 13 | ||
| // The existing PK (sbom_id, left_node_id, relationship, right_node_id) | ||
| // has left_node_id between sbom_id and relationship, forcing a scan of | ||
| // all left_node_id values per SBOM. | ||
| manager | ||
| .create_index( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .table(PackageRelatesToPackage::Table) | ||
| .name(Indexes::PackageRelatesToPackageSbomRelIdx.to_string()) | ||
| .col(PackageRelatesToPackage::SbomId) | ||
| .col(PackageRelatesToPackage::Relationship) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| // purl_status.version_range_id — used in vulnerability analysis: | ||
| // INNER JOIN version_range ON purl_status.version_range_id = version_range.id | ||
| manager | ||
| .create_index( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .table(PurlStatus::Table) | ||
| .name(Indexes::PurlStatusVersionRangeIdIdx.to_string()) | ||
| .col(PurlStatus::VersionRangeId) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| // cpe (vendor, product, version) — used in the generalized CPE lookup | ||
| // within product_advisory_info_sql(): | ||
| // WHERE (vendor, product, version) IN (SELECT ...) | ||
| manager | ||
| .create_index( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .table(Cpe::Table) | ||
| .name(Indexes::CpeVendorProductVersionIdx.to_string()) | ||
| .col(Cpe::Vendor) | ||
| .col(Cpe::Product) | ||
| .col(Cpe::Version) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| // advisory.issuer_id — used in advisory listing/detail queries: | ||
| // LEFT JOIN organization ON advisory.issuer_id = organization.id | ||
| manager | ||
| .create_index( | ||
| Index::create() | ||
| .if_not_exists() | ||
| .table(Advisory::Table) | ||
| .name(Indexes::AdvisoryIssuerIdIdx.to_string()) | ||
| .col(Advisory::IssuerId) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
| manager | ||
| .drop_index( | ||
| Index::drop() | ||
| .if_exists() | ||
| .table(Advisory::Table) | ||
| .name(Indexes::AdvisoryIssuerIdIdx.to_string()) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| manager | ||
| .drop_index( | ||
| Index::drop() | ||
| .if_exists() | ||
| .table(Cpe::Table) | ||
| .name(Indexes::CpeVendorProductVersionIdx.to_string()) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| manager | ||
| .drop_index( | ||
| Index::drop() | ||
| .if_exists() | ||
| .table(PurlStatus::Table) | ||
| .name(Indexes::PurlStatusVersionRangeIdIdx.to_string()) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| manager | ||
| .drop_index( | ||
| Index::drop() | ||
| .if_exists() | ||
| .table(PackageRelatesToPackage::Table) | ||
| .name(Indexes::PackageRelatesToPackageSbomRelIdx.to_string()) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| manager | ||
| .drop_index( | ||
| Index::drop() | ||
| .if_exists() | ||
| .table(ProductVersion::Table) | ||
| .name(Indexes::ProductVersionProductIdIdx.to_string()) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| manager | ||
| .drop_index( | ||
| Index::drop() | ||
| .if_exists() | ||
| .table(ProductVersion::Table) | ||
| .name(Indexes::ProductVersionSbomIdIdx.to_string()) | ||
| .to_owned(), | ||
| ) | ||
| .await?; | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| #[allow(clippy::enum_variant_names)] | ||
| #[derive(DeriveIden)] | ||
| pub enum Indexes { | ||
| ProductVersionSbomIdIdx, | ||
| ProductVersionProductIdIdx, | ||
| PackageRelatesToPackageSbomRelIdx, | ||
| PurlStatusVersionRangeIdIdx, | ||
| CpeVendorProductVersionIdx, | ||
| AdvisoryIssuerIdIdx, | ||
| } | ||
|
|
||
| #[derive(DeriveIden)] | ||
| pub enum ProductVersion { | ||
| Table, | ||
| SbomId, | ||
| ProductId, | ||
| } | ||
|
|
||
| #[derive(DeriveIden)] | ||
| pub enum PackageRelatesToPackage { | ||
| Table, | ||
| SbomId, | ||
| Relationship, | ||
| } | ||
|
|
||
| #[derive(DeriveIden)] | ||
| pub enum PurlStatus { | ||
| Table, | ||
| VersionRangeId, | ||
| } | ||
|
|
||
| #[derive(DeriveIden)] | ||
| pub enum Cpe { | ||
| Table, | ||
| Vendor, | ||
| Product, | ||
| Version, | ||
| } | ||
|
|
||
| #[derive(DeriveIden)] | ||
| pub enum Advisory { | ||
| Table, | ||
| IssuerId, | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
issue (performance): Consider the impact of non-concurrent index creation on large, hot tables.
Plain
CREATE INDEXwill take an exclusive lock on the table for the duration of the build, which can be disruptive on large, hot tables likeproduct_version,package_relates_to_package, orcpe. If this runs on a live system, consider usingCREATE INDEX CONCURRENTLY(via raw SQL or a special migration) or scheduling the migration during a maintenance window to avoid impacting production traffic.