Skip to content

Commit

Permalink
Merge pull request #629 from SiaFoundation/chris/speedup-object-stats
Browse files Browse the repository at this point in the history
Speed up ObjectsStats by splitting up query
  • Loading branch information
ChrisSchinnerl authored Sep 29, 2023
2 parents 9d8016a + 2c2a38b commit 23f2918
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions stores/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,21 +560,25 @@ 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 totalSectors uint64
err = tx.Model(&dbSector{}).
Raw("SELECT COUNT(*) FROM (SELECT 1 FROM contract_sectors cs GROUP BY cs.db_sector_id) sectors").
Scan(&totalSectors).
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(&sectorSizes).

var totalUploaded int64
err = tx.Model(&dbContractSector{}).
Count(&totalUploaded).
Error
if err != nil {
return err
}
resp.TotalSectorsSize = sectorSizes.SectorsSize
resp.TotalUploadedSize = sectorSizes.UploadedSize

resp.TotalSectorsSize = totalSectors * rhpv2.SectorSize
resp.TotalUploadedSize = uint64(totalUploaded) * rhpv2.SectorSize
return nil
})
}
Expand Down

0 comments on commit 23f2918

Please sign in to comment.