satellite/accounting: fix full table scan GetProjectObjectsSegments

New SQL queries for GetProjectObjectsSegments turns out to introduce
full table scan. This is fix for this problem.

Change-Id: Ieac22aafeb780168523a97e27c9283c9ac6a24c8
This commit is contained in:
Michal Niewrzal 2023-04-17 10:24:10 +02:00
parent 2193392e6b
commit 3f543163c7
2 changed files with 9 additions and 2 deletions

View File

@ -451,9 +451,10 @@ func (db *ProjectAccounting) GetProjectObjectsSegments(ctx context.Context, proj
var latestDate time.Time
latestDateRow := db.db.QueryRowContext(ctx, db.db.Rebind(`
SELECT interval_start FROM bucket_storage_tallies
SELECT interval_start FROM bucket_storage_tallies bst
WHERE
EXISTS (SELECT 1 FROM bucket_metainfos WHERE project_id = ?)
project_id = ?
AND EXISTS (SELECT 1 FROM bucket_metainfos bm WHERE bm.project_id = bst.project_id)
ORDER BY interval_start DESC
LIMIT 1
`), projectID[:])

View File

@ -426,6 +426,12 @@ func Test_GetProjectObjectsSegments(t *testing.T) {
err = planet.Uplinks[0].Upload(ctx, planet.Satellites[0], "testbucket", "object", []byte("testdata"))
require.NoError(t, err)
// bucket exists but no entry in bucket tallies yet
projectStats, err = planet.Satellites[0].DB.ProjectAccounting().GetProjectObjectsSegments(ctx, projectID)
require.NoError(t, err)
require.Zero(t, projectStats.ObjectCount)
require.Zero(t, projectStats.SegmentCount)
planet.Satellites[0].Accounting.Tally.Loop.TriggerWait()
projectStats, err = planet.Satellites[0].DB.ProjectAccounting().GetProjectObjectsSegments(ctx, projectID)