Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use index hint for idx_contracts_fcid_timestamp, MySQL does not select it otherwise #1059

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion stores/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -555,11 +555,16 @@ func (s *SQLStore) findAggregatedContractPeriods(ctx context.Context, start time
return fmt.Errorf("failed to fetch distinct contract ids: %w", err)
}

var indexHint string
if !isSQLite(tx) {
indexHint = "USE INDEX (idx_contracts_fcid_timestamp)"
}

for intervalStart := start; intervalStart.Before(end); intervalStart = intervalStart.Add(interval) {
intervalEnd := intervalStart.Add(interval)
for _, fcid := range fcids {
var metrics []dbContractMetric
err := tx.Raw("SELECT * FROM contracts WHERE contracts.timestamp >= ? AND contracts.timestamp < ? AND contracts.fcid = ? LIMIT 1", unixTimeMS(intervalStart), unixTimeMS(intervalEnd), fileContractID(fcid)).
err := tx.Raw(fmt.Sprintf("SELECT * FROM contracts %s WHERE contracts.timestamp >= ? AND contracts.timestamp < ? AND contracts.fcid = ? LIMIT 1", indexHint), unixTimeMS(intervalStart), unixTimeMS(intervalEnd), fileContractID(fcid)).
Scan(&metrics).Error
if err != nil {
return fmt.Errorf("failed to fetch contract metrics: %w", err)
Expand Down
Loading