Skip to content

Commit

Permalink
add indexes for common queries on ENT
Browse files Browse the repository at this point in the history
Signed-off-by: pxp928 <parth.psu@gmail.com>
  • Loading branch information
pxp928 committed Jul 16, 2024
1 parent a4c36b1 commit 8620418
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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));
3 changes: 2 additions & 1 deletion pkg/assembler/backends/ent/migrate/migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -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=
42 changes: 42 additions & 0 deletions pkg/assembler/backends/ent/migrate/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/assembler/backends/ent/schema/billofmaterials.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
1 change: 1 addition & 0 deletions pkg/assembler/backends/ent/schema/certifylegal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
3 changes: 2 additions & 1 deletion pkg/assembler/backends/ent/schema/dependency.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
2 changes: 2 additions & 0 deletions pkg/assembler/backends/ent/schema/occurrence.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

0 comments on commit 8620418

Please sign in to comment.