diff options
| author | Ethan Lee <ethanalee@google.com> | 2025-11-13 14:51:08 -0500 |
|---|---|---|
| committer | Ethan Lee <ethanalee@google.com> | 2025-11-13 13:36:12 -0800 |
| commit | 28eed86815232e48f5e757569fcd061f858142d4 (patch) | |
| tree | fadd2816682ff4651058d980b489c186b3acf154 | |
| parent | c7fc077fc0e70e6198224f86ef70901c94919a4f (diff) | |
| download | go-x-pkgsite-28eed86815232e48f5e757569fcd061f858142d4.tar.xz | |
internal/postgres: utilize num_imports column in insert_module
- Avoid recomputing number of imports for every query in getUnitWithAllFields.
For golang/go#76284
Change-Id: I7f7d683efb1b2ff49e33681802896a65b1354d25
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/720261
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Robert Findley <rfindley@google.com>
Auto-Submit: Ethan Lee <ethanalee@google.com>
kokoro-CI: kokoro <noreply+kokoro@google.com>
| -rw-r--r-- | internal/postgres/insert_module.go | 2 | ||||
| -rw-r--r-- | internal/postgres/insert_module_test.go | 1 | ||||
| -rw-r--r-- | internal/postgres/unit.go | 7 |
3 files changed, 4 insertions, 6 deletions
diff --git a/internal/postgres/insert_module.go b/internal/postgres/insert_module.go index b348ae5f..39e8688d 100644 --- a/internal/postgres/insert_module.go +++ b/internal/postgres/insert_module.go @@ -409,6 +409,7 @@ func (pdb *DB) insertUnits(ctx context.Context, tx *database.DB, pq.Array(licenseTypes), pq.Array(licensePaths), pdb.bypassLicenseCheck || u.IsRedistributable, + len(u.Imports), ) if u.Readme != nil { pathToReadme[u.Path] = u.Readme @@ -481,6 +482,7 @@ func insertUnits(ctx context.Context, db *database.DB, unitValues []any) (pathID "license_types", "license_paths", "redistributable", + "num_imports", } uniqueUnitCols := []string{"path_id", "module_id"} returningUnitCols := []string{"id", "path_id"} diff --git a/internal/postgres/insert_module_test.go b/internal/postgres/insert_module_test.go index 37dd8b7a..2788546e 100644 --- a/internal/postgres/insert_module_test.go +++ b/internal/postgres/insert_module_test.go @@ -124,6 +124,7 @@ func checkModule(ctx context.Context, t *testing.T, db *DB, want *internal.Modul } } wantu.Subdirectories = subdirectories + wantu.NumImports = len(wantu.Imports) opts := cmp.Options{ cmpopts.EquateEmpty(), cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"), diff --git a/internal/postgres/unit.go b/internal/postgres/unit.go index 33c58401..dda306ea 100644 --- a/internal/postgres/unit.go +++ b/internal/postgres/unit.go @@ -474,12 +474,7 @@ func (db *DB) getUnitWithAllFields(ctx context.Context, um *internal.UnitMeta, b r.contents, d.synopsis, d.source, - COALESCE(( - SELECT COUNT(unit_id) - FROM imports - WHERE unit_id = u.id - GROUP BY unit_id - ), 0) AS num_imports, + u.num_imports, COALESCE(( SELECT imported_by_count FROM search_documents |
