From 8620418e66ed669b4f2648347b2f3cb175b07d5d Mon Sep 17 00:00:00 2001 From: pxp928 Date: Tue, 16 Jul 2024 14:24:07 -0400 Subject: [PATCH] add indexes for common queries on ENT Signed-off-by: pxp928 --- .../migrations/20240716182144_ent_diff.sql | 12 ++++++ .../backends/ent/migrate/migrations/atlas.sum | 3 +- pkg/assembler/backends/ent/migrate/schema.go | 42 +++++++++++++++++++ .../backends/ent/schema/billofmaterials.go | 2 + .../backends/ent/schema/certifylegal.go | 1 + .../backends/ent/schema/dependency.go | 3 +- .../backends/ent/schema/occurrence.go | 2 + 7 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 pkg/assembler/backends/ent/migrate/migrations/20240716182144_ent_diff.sql diff --git a/pkg/assembler/backends/ent/migrate/migrations/20240716182144_ent_diff.sql b/pkg/assembler/backends/ent/migrate/migrations/20240716182144_ent_diff.sql new file mode 100644 index 00000000000..7abb3b9f464 --- /dev/null +++ b/pkg/assembler/backends/ent/migrate/migrations/20240716182144_ent_diff.sql @@ -0,0 +1,12 @@ +-- Create index "billofmaterials_artifact_id" to table: "bill_of_materials" +CREATE INDEX "billofmaterials_artifact_id" ON "bill_of_materials" ("artifact_id") WHERE ((package_id IS NULL) AND (artifact_id IS NOT NULL)); +-- Create index "billofmaterials_package_id" to table: "bill_of_materials" +CREATE INDEX "billofmaterials_package_id" ON "bill_of_materials" ("package_id") WHERE ((package_id IS NOT NULL) AND (artifact_id IS NULL)); +-- Create index "certifylegal_package_id" to table: "certify_legals" +CREATE INDEX "certifylegal_package_id" ON "certify_legals" ("package_id") WHERE ((package_id IS NOT NULL) AND (source_id IS NULL)); +-- Create index "dependency_dependent_package_version_id" to table: "dependencies" +CREATE INDEX "dependency_dependent_package_version_id" ON "dependencies" ("dependent_package_version_id"); +-- Create index "occurrence_artifact_id" to table: "occurrences" +CREATE INDEX "occurrence_artifact_id" ON "occurrences" ("artifact_id"); +-- Create index "query_occurrence_package_id" to table: "occurrences" +CREATE INDEX "query_occurrence_package_id" ON "occurrences" ("package_id") WHERE ((package_id IS NOT NULL) AND (source_id IS NULL)); diff --git a/pkg/assembler/backends/ent/migrate/migrations/atlas.sum b/pkg/assembler/backends/ent/migrate/migrations/atlas.sum index d14875cae10..ad1b1f964f8 100644 --- a/pkg/assembler/backends/ent/migrate/migrations/atlas.sum +++ b/pkg/assembler/backends/ent/migrate/migrations/atlas.sum @@ -1,5 +1,6 @@ -h1:LXg/nYCsid8/s+ZoQXWKCibm+F9bkUi1BIhyu2GEPb8= +h1:TKSr9lC5BbUWVZejxSWM7ok/sdxy/sTwD/QZYeCDRFQ= 20240503123155_baseline.sql h1:oZtbKI8sJj3xQq7ibfvfhFoVl+Oa67CWP7DFrsVLVds= 20240626153721_ent_diff.sql h1:FvV1xELikdPbtJk7kxIZn9MhvVVoFLF/2/iT/wM5RkA= 20240702195630_ent_diff.sql h1:y8TgeUg35krYVORmC7cN4O96HqOc3mVO9IQ2lYzIzwg= 20240712193834_ent_diff.sql h1:gBSeZwJhD3fL9E97DJIGevIwsbe64Lc0YQtxvwpd/Ss= +20240716182144_ent_diff.sql h1:QaeD0+aY7RYQSvpoxLMfUrcwKIM9ZG++0cn8F5PkIRo= diff --git a/pkg/assembler/backends/ent/migrate/schema.go b/pkg/assembler/backends/ent/migrate/schema.go index 4b5a54eb926..451f7a9606a 100644 --- a/pkg/assembler/backends/ent/migrate/schema.go +++ b/pkg/assembler/backends/ent/migrate/schema.go @@ -82,6 +82,22 @@ var ( Where: "package_id IS NULL AND artifact_id IS NOT NULL", }, }, + { + Name: "billofmaterials_package_id", + Unique: false, + Columns: []*schema.Column{BillOfMaterialsColumns[13]}, + Annotation: &entsql.IndexAnnotation{ + Where: "package_id IS NOT NULL AND artifact_id IS NULL", + }, + }, + { + Name: "billofmaterials_artifact_id", + Unique: false, + Columns: []*schema.Column{BillOfMaterialsColumns[14]}, + Annotation: &entsql.IndexAnnotation{ + Where: "package_id IS NULL AND artifact_id IS NOT NULL", + }, + }, }, } // BuildersColumns holds the columns for the "builders" table. @@ -234,6 +250,14 @@ var ( Where: "package_id IS NOT NULL AND source_id IS NULL", }, }, + { + Name: "certifylegal_package_id", + Unique: false, + Columns: []*schema.Column{CertifyLegalsColumns[11]}, + Annotation: &entsql.IndexAnnotation{ + Where: "package_id IS NOT NULL AND source_id IS NULL", + }, + }, }, } // CertifyScorecardsColumns holds the columns for the "certify_scorecards" table. @@ -436,6 +460,11 @@ var ( Unique: false, Columns: []*schema.Column{DependenciesColumns[7]}, }, + { + Name: "dependency_dependent_package_version_id", + Unique: false, + Columns: []*schema.Column{DependenciesColumns[9]}, + }, }, } // HasMetadataColumns holds the columns for the "has_metadata" table. @@ -686,6 +715,19 @@ var ( Where: "package_id IS NULL AND source_id IS NOT NULL", }, }, + { + Name: "query_occurrence_package_id", + Unique: false, + Columns: []*schema.Column{OccurrencesColumns[6]}, + Annotation: &entsql.IndexAnnotation{ + Where: "package_id IS NOT NULL AND source_id IS NULL", + }, + }, + { + Name: "occurrence_artifact_id", + Unique: false, + Columns: []*schema.Column{OccurrencesColumns[5]}, + }, }, } // PackageNamesColumns holds the columns for the "package_names" table. diff --git a/pkg/assembler/backends/ent/schema/billofmaterials.go b/pkg/assembler/backends/ent/schema/billofmaterials.go index f23141047a3..9f34e22a374 100644 --- a/pkg/assembler/backends/ent/schema/billofmaterials.go +++ b/pkg/assembler/backends/ent/schema/billofmaterials.go @@ -74,5 +74,7 @@ func (BillOfMaterials) Indexes() []ent.Index { index.Fields("algorithm", "digest", "uri", "download_location", "known_since", "included_packages_hash", "included_artifacts_hash", "included_dependencies_hash", "included_occurrences_hash", "origin", "collector", "document_ref").Edges("artifact").Unique(). Annotations(entsql.IndexWhere("package_id IS NULL AND artifact_id IS NOT NULL")).StorageKey("sbom_artifact_id"), + index.Fields("package_id").Annotations(entsql.IndexWhere("package_id IS NOT NULL AND artifact_id IS NULL")), // query when subject is package ID + index.Fields("artifact_id").Annotations(entsql.IndexWhere("package_id IS NULL AND artifact_id IS NOT NULL")), // query when subject is artifact ID } } diff --git a/pkg/assembler/backends/ent/schema/certifylegal.go b/pkg/assembler/backends/ent/schema/certifylegal.go index e0249eb16d1..782b6b191db 100644 --- a/pkg/assembler/backends/ent/schema/certifylegal.go +++ b/pkg/assembler/backends/ent/schema/certifylegal.go @@ -71,5 +71,6 @@ func (CertifyLegal) Indexes() []ent.Index { "origin", "collector", "document_ref", "declared_licenses_hash", "discovered_licenses_hash"). Unique(). Annotations(entsql.IndexWhere("package_id IS NOT NULL AND source_id IS NULL")), + index.Fields("package_id").Annotations(entsql.IndexWhere("package_id IS NOT NULL AND source_id IS NULL")), // query when subject is package ID } } diff --git a/pkg/assembler/backends/ent/schema/dependency.go b/pkg/assembler/backends/ent/schema/dependency.go index db0931527fa..3a6c10a2964 100644 --- a/pkg/assembler/backends/ent/schema/dependency.go +++ b/pkg/assembler/backends/ent/schema/dependency.go @@ -87,6 +87,7 @@ func (Dependency) Indexes() []ent.Index { Unique(). Annotations(entsql.IndexWhere("dependent_package_name_id IS NULL AND dependent_package_version_id IS NOT NULL")). StorageKey("dep_package_version_id"), - index.Fields("package_id"), // speed up frequently run queries to check for deps with a certain package ID + index.Fields("package_id"), // speed up frequently run queries to check for deps with a certain package ID + index.Fields("dependent_package_version_id"), // query via the dependent package ID } } diff --git a/pkg/assembler/backends/ent/schema/occurrence.go b/pkg/assembler/backends/ent/schema/occurrence.go index b49fa727f39..ba0afd7ef7f 100644 --- a/pkg/assembler/backends/ent/schema/occurrence.go +++ b/pkg/assembler/backends/ent/schema/occurrence.go @@ -69,5 +69,7 @@ func (Occurrence) Indexes() []ent.Index { Annotations(entsql.IndexWhere("package_id IS NOT NULL AND source_id IS NULL")).StorageKey("occurrence_package_id"), index.Fields("justification", "origin", "collector", "document_ref").Edges("artifact", "source").Unique(). Annotations(entsql.IndexWhere("package_id IS NULL AND source_id IS NOT NULL")).StorageKey("occurrence_source_id"), + index.Fields("package_id").Annotations(entsql.IndexWhere("package_id IS NOT NULL AND source_id IS NULL")).StorageKey("query_occurrence_package_id"), //querying subject - package ID + index.Fields("artifact_id"), //querying object - artifact ID } }