Skip to content

Commit 28eed86

Browse files
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 <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Ethan Lee <[email protected]> kokoro-CI: kokoro <[email protected]>
1 parent c7fc077 commit 28eed86

File tree

3 files changed

+4
-6
lines changed

3 files changed

+4
-6
lines changed

internal/postgres/insert_module.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ func (pdb *DB) insertUnits(ctx context.Context, tx *database.DB,
409409
pq.Array(licenseTypes),
410410
pq.Array(licensePaths),
411411
pdb.bypassLicenseCheck || u.IsRedistributable,
412+
len(u.Imports),
412413
)
413414
if u.Readme != nil {
414415
pathToReadme[u.Path] = u.Readme
@@ -481,6 +482,7 @@ func insertUnits(ctx context.Context, db *database.DB, unitValues []any) (pathID
481482
"license_types",
482483
"license_paths",
483484
"redistributable",
485+
"num_imports",
484486
}
485487
uniqueUnitCols := []string{"path_id", "module_id"}
486488
returningUnitCols := []string{"id", "path_id"}

internal/postgres/insert_module_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ func checkModule(ctx context.Context, t *testing.T, db *DB, want *internal.Modul
124124
}
125125
}
126126
wantu.Subdirectories = subdirectories
127+
wantu.NumImports = len(wantu.Imports)
127128
opts := cmp.Options{
128129
cmpopts.EquateEmpty(),
129130
cmpopts.IgnoreFields(licenses.Metadata{}, "Coverage"),

internal/postgres/unit.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -474,12 +474,7 @@ func (db *DB) getUnitWithAllFields(ctx context.Context, um *internal.UnitMeta, b
474474
r.contents,
475475
d.synopsis,
476476
d.source,
477-
COALESCE((
478-
SELECT COUNT(unit_id)
479-
FROM imports
480-
WHERE unit_id = u.id
481-
GROUP BY unit_id
482-
), 0) AS num_imports,
477+
u.num_imports,
483478
COALESCE((
484479
SELECT imported_by_count
485480
FROM search_documents

0 commit comments

Comments
 (0)