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
Show file tree
Hide file tree
Changes from 2 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(start time.Time, n uint64, inte
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
87 changes: 68 additions & 19 deletions stores/sql_test.go
ChrisSchinnerl marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -332,20 +332,27 @@ type sqliteQueryPlan struct {
Detail string `json:"detail"`
}

func (p sqliteQueryPlan) usesIndex() bool {
func (p sqliteQueryPlan) usesIndex(index string) bool {
d := strings.ToLower(p.Detail)
return strings.Contains(d, "using index") || strings.Contains(d, "using covering index")
if index == "" {
return strings.Contains(d, "using index") || strings.Contains(d, "using covering index")
}
return strings.Contains(d, fmt.Sprintf("using index %s", index))
}

//nolint:tagliatelle
type mysqlQueryPlan struct {
Extra string `json:"Extra"`
PossibleKeys string `json:"possible_keys"`
Key string `json:"key"`
}

func (p mysqlQueryPlan) usesIndex() bool {
d := strings.ToLower(p.Extra)
return strings.Contains(d, "using index") || strings.Contains(p.PossibleKeys, "idx_")
func (p mysqlQueryPlan) usesIndex(index string) bool {
if index == "" {
d := strings.ToLower(p.Extra)
return strings.Contains(d, "using index") || strings.Contains(p.PossibleKeys, "idx_")
}
return p.Key == index
}

func TestQueryPlan(t *testing.T) {
Expand Down Expand Up @@ -381,24 +388,66 @@ func TestQueryPlan(t *testing.T) {
}

for _, query := range queries {
if isSQLite(ss.db) {
var explain sqliteQueryPlan
if err := ss.db.Raw(fmt.Sprintf("EXPLAIN QUERY PLAN %s;", query)).Scan(&explain).Error; err != nil {
t.Fatal(err)
} else if !explain.usesIndex() {
t.Fatalf("query '%s' should use an index, instead the plan was %+v", query, explain)
}
} else {
var explain mysqlQueryPlan
if err := ss.db.Raw(fmt.Sprintf("EXPLAIN %s;", query)).Scan(&explain).Error; err != nil {
t.Fatal(err)
} else if !explain.usesIndex() {
t.Fatalf("query '%s' should use an index, instead the plan was %+v", query, explain)
}
plan := queryPlan(ss.db)
if err := explainQuery(ss.db, query, plan); err != nil {
t.Fatal(err)
} else if !plan.usesIndex("") {
t.Fatalf("query '%s' should use an index, instead the plan was %+v", query, plan)
}
}
}

func TestContractMetricsQueryPlan(t *testing.T) {
ss := newTestSQLStore(t, defaultTestSQLStoreConfig)
defer ss.Close()
db := ss.dbMetrics

query := "SELECT * FROM contracts c WHERE c.timestamp >= 1 AND c.timestamp < 2 AND c.fcid = '<binary>' LIMIT 1"
plan := queryPlan(db)
if err := explainQuery(db, query, plan); err != nil {
t.Fatal(err)
}

if isSQLite(db) {
// SQLite uses the index by default
if !plan.usesIndex("idx_contracts_fcid_timestamp") {
t.Fatalf("unexpected query plan %+v", plan)
}
} else {
// MySQL uses an index, but not 'idx_contracts_fcid_timestamp'
if !plan.usesIndex("") || plan.usesIndex("idx_contracts_fcid_timestamp") {
t.Fatalf("unexpected query plan %+v", plan)
}

// redo the query with hint
queryWithHint := strings.Replace(query, "WHERE", "USE INDEX (idx_contracts_fcid_timestamp) WHERE", 1)
if err := explainQuery(db, queryWithHint, plan); err != nil {
t.Fatal(err)
}

// assert it uses 'idx_contracts_fcid_timestamp' now
if !plan.usesIndex("idx_contracts_fcid_timestamp") {
t.Fatalf("unexpected query plan %+v", plan)
}
}
}

func queryPlan(db *gorm.DB) interface{ usesIndex(index string) bool } {
if isSQLite(db) {
return &sqliteQueryPlan{}
}
return &mysqlQueryPlan{}
}

func explainQuery(db *gorm.DB, query string, res interface{}) (err error) {
if isSQLite(db) {
err = db.Raw(fmt.Sprintf("EXPLAIN QUERY PLAN %s;", query)).Scan(&res).Error
} else {
err = db.Raw(fmt.Sprintf("EXPLAIN %s;", query)).Scan(&res).Error
}
return
}

func TestApplyUpdatesErr(t *testing.T) {
ss := newTestSQLStore(t, defaultTestSQLStoreConfig)
defer ss.Close()
Expand Down
Loading