storagenode/bandwidthdb: Use existing indexes (#3949)

* storagenode/bandwidthdb: Use existing indexes
This commit is contained in:
littleskunk 2020-10-20 22:48:40 +02:00 committed by GitHub
parent 334ae5b164
commit 77d54ff0ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -40,7 +40,7 @@ func (db *bandwidthDB) Add(ctx context.Context, satelliteID storj.NodeID, action
_, err = db.ExecContext(ctx, `
INSERT INTO
bandwidth_usage(satellite_id, action, amount, created_at)
VALUES(?, ?, ?, ?)`, satelliteID, action, amount, created.UTC())
VALUES(?, ?, ?, datetime(?))`, satelliteID, action, amount, created.UTC())
if err == nil {
db.usedMu.Lock()
defer db.usedMu.Unlock()
@ -133,18 +133,18 @@ func (db *bandwidthDB) getSummary(ctx context.Context, from, to time.Time, filte
usage := &bandwidth.Usage{}
from = from.UTC()
to = to.UTC()
from, to = from.UTC(), to.UTC()
rows, err := db.QueryContext(ctx, `
SELECT action, sum(a) amount from(
SELECT action, sum(amount) a
FROM bandwidth_usage
WHERE datetime(?) <= datetime(created_at) AND datetime(created_at) <= datetime(?)
WHERE datetime(?) <= created_at AND created_at <= datetime(?)
GROUP BY action
UNION ALL
SELECT action, sum(amount) a
FROM bandwidth_usage_rollups
WHERE datetime(?) <= datetime(interval_start) AND datetime(interval_start) <= datetime(?)
WHERE datetime(?) <= interval_start AND interval_start <= datetime(?)
GROUP BY action
) GROUP BY action;
`, from, to, from, to)
@ -201,13 +201,13 @@ func (db *bandwidthDB) getSatelliteSummary(ctx context.Context, satelliteID stor
query := `SELECT action, sum(a) amount from(
SELECT action, sum(amount) a
FROM bandwidth_usage
WHERE datetime(?) <= datetime(created_at) AND datetime(created_at) <= datetime(?)
WHERE datetime(?) <= created_at AND created_at <= datetime(?)
AND satellite_id = ?
GROUP BY action
UNION ALL
SELECT action, sum(amount) a
FROM bandwidth_usage_rollups
WHERE datetime(?) <= datetime(interval_start) AND datetime(interval_start) <= datetime(?)
WHERE datetime(?) <= interval_start AND interval_start <= datetime(?)
AND satellite_id = ?
GROUP BY action
) GROUP BY action;`
@ -242,20 +242,20 @@ func (db *bandwidthDB) SummaryBySatellite(ctx context.Context, from, to time.Tim
entries := map[storj.NodeID]*bandwidth.Usage{}
from = from.UTC()
to = to.UTC()
from, to = from.UTC(), to.UTC()
rows, err := db.QueryContext(ctx, `
SELECT satellite_id, action, sum(a) amount from(
SELECT satellite_id, action, sum(amount) a
SELECT satellite_id, action, sum(amount) a
FROM bandwidth_usage
WHERE datetime(?) <= datetime(created_at) AND datetime(created_at) <= datetime(?)
WHERE datetime(?) <= created_at AND created_at <= datetime(?)
GROUP BY satellite_id, action
UNION ALL
SELECT satellite_id, action, sum(amount) a
UNION ALL
SELECT satellite_id, action, sum(amount) a
FROM bandwidth_usage_rollups
WHERE datetime(?) <= datetime(interval_start) AND datetime(interval_start) <= datetime(?)
WHERE datetime(?) <= interval_start AND interval_start <= datetime(?)
GROUP BY satellite_id, action
) GROUP BY satellite_id, action;
) GROUP BY satellite_id, action;
`, from, to, from, to)
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
@ -313,12 +313,12 @@ func (db *bandwidthDB) Rollup(ctx context.Context) (err error) {
INSERT INTO bandwidth_usage_rollups (interval_start, satellite_id, action, amount)
SELECT datetime(strftime('%Y-%m-%dT%H:00:00', created_at)) created_hr, satellite_id, action, SUM(amount)
FROM bandwidth_usage
WHERE datetime(created_at) < datetime(?)
WHERE created_at < datetime(?)
GROUP BY created_hr, satellite_id, action
ON CONFLICT(interval_start, satellite_id, action)
DO UPDATE SET amount = bandwidth_usage_rollups.amount + excluded.amount;
DELETE FROM bandwidth_usage WHERE datetime(created_at) < datetime(?);
DELETE FROM bandwidth_usage WHERE created_at < datetime(?);
`, hour, hour)
if err != nil {
return ErrBandwidth.Wrap(err)
@ -341,7 +341,7 @@ func (db *bandwidthDB) GetDailyRollups(ctx context.Context, from, to time.Time)
_, before := date.DayBoundary(to.UTC())
return db.getDailyUsageRollups(ctx,
"WHERE DATETIME(?) <= DATETIME(interval_start) AND DATETIME(interval_start) <= DATETIME(?)",
"WHERE datetime(?) <= interval_start AND interval_start <= datetime(?)",
since, before)
}
@ -354,7 +354,7 @@ func (db *bandwidthDB) GetDailySatelliteRollups(ctx context.Context, satelliteID
_, before := date.DayBoundary(to.UTC())
return db.getDailyUsageRollups(ctx,
"WHERE satellite_id = ? AND DATETIME(?) <= DATETIME(interval_start) AND DATETIME(interval_start) <= DATETIME(?)",
"WHERE satellite_id = ? AND datetime(?) <= interval_start AND interval_start <= datetime(?)",
satelliteID, since, before)
}