From c099bff477bb24921d7077eba0be369d10dedd3e Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Fri, 29 Sep 2023 12:25:11 +0200 Subject: [PATCH 1/3] stores: speed up ObjectsStats --- stores/metadata.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 4a7d7b235..2c2feeff3 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -560,21 +560,27 @@ func (s *SQLStore) ObjectsStats(ctx context.Context) (api.ObjectsStatsResponse, resp.NumObjects = objInfo.NumObjects resp.TotalObjectsSize = objInfo.TotalObjectsSize - // Size of sectors - var sectorSizes struct { - SectorsSize uint64 - UploadedSize uint64 + var sectorsSize int64 + err = tx.Model(&dbSector{}). + Raw("SELECT COUNT(*) FROM (SELECT 1 FROM contract_sectors cs GROUP BY cs.db_sector_id)"). + Scan(§orsSize). + Error + if err != nil { + return err } - err = tx. - Model(&dbContractSector{}). - Select("COUNT(DISTINCT db_sector_id) * ? as sectors_size, COUNT(*) * ? as uploaded_size", rhpv2.SectorSize, rhpv2.SectorSize). - Scan(§orSizes). + sectorsSize *= int64(rhpv2.SectorSize) + + var uploadedSize int64 + err = tx.Model(&dbContractSector{}). + Count(&uploadedSize). Error if err != nil { return err } - resp.TotalSectorsSize = sectorSizes.SectorsSize - resp.TotalUploadedSize = sectorSizes.UploadedSize + uploadedSize *= int64(rhpv2.SectorSize) + + resp.TotalSectorsSize = uint64(sectorsSize) + resp.TotalUploadedSize = uint64(uploadedSize) return nil }) } From b8f9297238c0d3d6687e80bd3f890be5d2e0f30b Mon Sep 17 00:00:00 2001 From: PJ Date: Fri, 29 Sep 2023 12:31:13 +0200 Subject: [PATCH 2/3] stores: implement MR remarks --- stores/metadata.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 2c2feeff3..007e65205 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -560,27 +560,25 @@ func (s *SQLStore) ObjectsStats(ctx context.Context) (api.ObjectsStatsResponse, resp.NumObjects = objInfo.NumObjects resp.TotalObjectsSize = objInfo.TotalObjectsSize - var sectorsSize int64 + var totalSectors uint64 err = tx.Model(&dbSector{}). Raw("SELECT COUNT(*) FROM (SELECT 1 FROM contract_sectors cs GROUP BY cs.db_sector_id)"). - Scan(§orsSize). + Scan(&totalSectors). Error if err != nil { return err } - sectorsSize *= int64(rhpv2.SectorSize) - var uploadedSize int64 + var totalUploaded int64 err = tx.Model(&dbContractSector{}). - Count(&uploadedSize). + Count(&totalUploaded). Error if err != nil { return err } - uploadedSize *= int64(rhpv2.SectorSize) - resp.TotalSectorsSize = uint64(sectorsSize) - resp.TotalUploadedSize = uint64(uploadedSize) + resp.TotalSectorsSize = totalSectors * rhpv2.SectorSize + resp.TotalUploadedSize = uint64(totalUploaded) * rhpv2.SectorSize return nil }) } From 2c2a38bb0cbc0e0f02af85a9c0a6d9bdda45dddf Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Fri, 29 Sep 2023 14:16:51 +0200 Subject: [PATCH 3/3] stors: fix query for mysql --- stores/metadata.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stores/metadata.go b/stores/metadata.go index 007e65205..6d7c25b04 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -562,7 +562,7 @@ func (s *SQLStore) ObjectsStats(ctx context.Context) (api.ObjectsStatsResponse, var totalSectors uint64 err = tx.Model(&dbSector{}). - Raw("SELECT COUNT(*) FROM (SELECT 1 FROM contract_sectors cs GROUP BY cs.db_sector_id)"). + Raw("SELECT COUNT(*) FROM (SELECT 1 FROM contract_sectors cs GROUP BY cs.db_sector_id) sectors"). Scan(&totalSectors). Error if err != nil {