Skip to content

Commit

Permalink
Merge pull request #7497 from influxdata/js-6704-first-last-optimization
Browse files Browse the repository at this point in the history
Optimize first/last when no group by interval is present
  • Loading branch information
jsternberg authored Oct 25, 2016
2 parents 5840cc4 + a515aed commit 17eb8cb
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ The following configuration changes in the `[data]` section may need to changed
- [#7480](https://github.com/influxdata/influxdb/pull/7480): Improve compaction planning performance by caching tsm file stats.
- [#7320](https://github.com/influxdata/influxdb/issues/7320): Update defaults in config for latest best practices
- [#7495](https://github.com/influxdata/influxdb/pull/7495): Rewrite regexes of the form host = /^server-a$/ to host = 'server-a', to take advantage of the tsdb index.
- [#6704](https://github.com/influxdata/influxdb/issues/6704): Optimize first/last when no group by interval is present.

### Bugfixes

Expand Down
17 changes: 16 additions & 1 deletion tsdb/engine/tsm1/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,22 @@ func (e *Engine) CreateIterator(opt influxql.IteratorOptions) (influxql.Iterator
if call, ok := opt.Expr.(*influxql.Call); ok {
refOpt := opt
refOpt.Expr = call.Args[0].(*influxql.VarRef)
inputs, err := e.createVarRefIterator(refOpt, true)

aggregate := true
if opt.Interval.IsZero() {
switch call.Name {
case "first":
aggregate = false
refOpt.Limit = 1
refOpt.Ascending = true
case "last":
aggregate = false
refOpt.Limit = 1
refOpt.Ascending = false
}
}

inputs, err := e.createVarRefIterator(refOpt, aggregate)
if err != nil {
return nil, err
} else if len(inputs) == 0 {
Expand Down
42 changes: 42 additions & 0 deletions tsdb/engine/tsm1/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,48 @@ func benchmarkEngineCreateIteratorCount(b *testing.B, pointN int) {
}, pointN)
}

func BenchmarkEngine_CreateIterator_First_1K(b *testing.B) {
benchmarkEngineCreateIteratorFirst(b, 1000)
}
func BenchmarkEngine_CreateIterator_First_100K(b *testing.B) {
benchmarkEngineCreateIteratorFirst(b, 100000)
}
func BenchmarkEngine_CreateIterator_First_1M(b *testing.B) {
benchmarkEngineCreateIteratorFirst(b, 1000000)
}

func benchmarkEngineCreateIteratorFirst(b *testing.B, pointN int) {
benchmarkIterator(b, influxql.IteratorOptions{
Expr: influxql.MustParseExpr("first(value)"),
Sources: []influxql.Source{&influxql.Measurement{Name: "cpu"}},
Dimensions: []string{"host"},
Ascending: true,
StartTime: influxql.MinTime,
EndTime: influxql.MaxTime,
}, pointN)
}

func BenchmarkEngine_CreateIterator_Last_1K(b *testing.B) {
benchmarkEngineCreateIteratorLast(b, 1000)
}
func BenchmarkEngine_CreateIterator_Last_100K(b *testing.B) {
benchmarkEngineCreateIteratorLast(b, 100000)
}
func BenchmarkEngine_CreateIterator_Last_1M(b *testing.B) {
benchmarkEngineCreateIteratorLast(b, 1000000)
}

func benchmarkEngineCreateIteratorLast(b *testing.B, pointN int) {
benchmarkIterator(b, influxql.IteratorOptions{
Expr: influxql.MustParseExpr("last(value)"),
Sources: []influxql.Source{&influxql.Measurement{Name: "cpu"}},
Dimensions: []string{"host"},
Ascending: true,
StartTime: influxql.MinTime,
EndTime: influxql.MaxTime,
}, pointN)
}

func BenchmarkEngine_CreateIterator_Limit_1K(b *testing.B) {
benchmarkEngineCreateIteratorLimit(b, 1000)
}
Expand Down

0 comments on commit 17eb8cb

Please sign in to comment.