-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Performance Fixes for Vitess 18 #14383
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
62e840f
vtgate: implement OLTP as an endtoend benchmark
vmg 91f7b00
evalengine: reorder comparison checks
vmg ba7b9ca
ordered_aggregate: optimize distinct without aggregations
vmg c2b9797
engine: more efficient sorting
vmg 92e684e
engine: fix streaming cases
vmg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package endtoend | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"math/rand" | ||
"sync" | ||
"testing" | ||
|
||
"vitess.io/vitess/go/mysql" | ||
) | ||
|
||
// 10 groups, 119 characters | ||
const cValueTemplate = "###########-###########-###########-" + | ||
"###########-###########-###########-" + | ||
"###########-###########-###########-" + | ||
"###########" | ||
|
||
// 5 groups, 59 characters | ||
const padValueTemplate = "###########-###########-###########-" + | ||
"###########-###########" | ||
|
||
func sysbenchRandom(rng *rand.Rand, template string) []byte { | ||
out := make([]byte, 0, len(template)) | ||
for i := range template { | ||
switch template[i] { | ||
case '#': | ||
out = append(out, '0'+byte(rng.Intn(10))) | ||
default: | ||
out = append(out, template[i]) | ||
} | ||
} | ||
return out | ||
} | ||
|
||
var oltpInitOnce sync.Once | ||
|
||
func BenchmarkOLTP(b *testing.B) { | ||
const MaxRows = 10000 | ||
const RangeSize = 100 | ||
|
||
rng := rand.New(rand.NewSource(1234)) | ||
|
||
ctx := context.Background() | ||
conn, err := mysql.Connect(ctx, &vtParams) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
defer conn.Close() | ||
|
||
var query bytes.Buffer | ||
|
||
oltpInitOnce.Do(func() { | ||
b.Logf("seeding database for benchmark...") | ||
|
||
var rows int = 1 | ||
for i := 0; i < MaxRows/10; i++ { | ||
query.Reset() | ||
query.WriteString("insert into oltp_test(id, k, c, pad) values ") | ||
for j := 0; j < 10; j++ { | ||
if j > 0 { | ||
query.WriteString(", ") | ||
} | ||
_, _ = fmt.Fprintf(&query, "(%d, %d, '%s', '%s')", rows, rng.Int31n(0xFFFF), sysbenchRandom(rng, cValueTemplate), sysbenchRandom(rng, padValueTemplate)) | ||
rows++ | ||
} | ||
|
||
_, err = conn.ExecuteFetch(query.String(), -1, false) | ||
if err != nil { | ||
b.Fatal(err) | ||
} | ||
} | ||
b.Logf("finshed (inserted %d rows)", rows) | ||
}) | ||
|
||
b.Run("SimpleRanges", func(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
id := rng.Intn(MaxRows) | ||
|
||
query.Reset() | ||
_, _ = fmt.Fprintf(&query, "SELECT c FROM oltp_test WHERE id BETWEEN %d AND %d", id, id+rng.Intn(RangeSize)-1) | ||
_, err := conn.ExecuteFetch(query.String(), 1000, false) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
} | ||
}) | ||
|
||
b.Run("SumRanges", func(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
id := rng.Intn(MaxRows) | ||
|
||
query.Reset() | ||
_, _ = fmt.Fprintf(&query, "SELECT SUM(k) FROM oltp_test WHERE id BETWEEN %d AND %d", id, id+rng.Intn(RangeSize)-1) | ||
_, err := conn.ExecuteFetch(query.String(), 1000, false) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
} | ||
}) | ||
|
||
b.Run("OrderRanges", func(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
id := rng.Intn(MaxRows) | ||
|
||
query.Reset() | ||
_, _ = fmt.Fprintf(&query, "SELECT c FROM oltp_test WHERE id BETWEEN %d AND %d ORDER BY c", id, id+rng.Intn(RangeSize)-1) | ||
_, err := conn.ExecuteFetch(query.String(), 1000, false) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
} | ||
}) | ||
|
||
b.Run("DistinctRanges", func(b *testing.B) { | ||
b.ResetTimer() | ||
for i := 0; i < b.N; i++ { | ||
id := rng.Intn(MaxRows) | ||
|
||
query.Reset() | ||
_, _ = fmt.Fprintf(&query, "SELECT DISTINCT c FROM oltp_test WHERE id BETWEEN %d AND %d ORDER BY c", id, id+rng.Intn(RangeSize)-1) | ||
_, err := conn.ExecuteFetch(query.String(), 1000, false) | ||
if err != nil { | ||
b.Error(err) | ||
} | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -114,6 +114,35 @@ func (oa *OrderedAggregate) TryExecute(ctx context.Context, vcursor VCursor, bin | |
return qr.Truncate(oa.TruncateColumnCount), nil | ||
} | ||
|
||
func (oa *OrderedAggregate) executeGroupBy(result *sqltypes.Result) (*sqltypes.Result, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice 🌟 |
||
if len(result.Rows) < 1 { | ||
return result, nil | ||
} | ||
|
||
out := &sqltypes.Result{ | ||
Fields: result.Fields, | ||
Rows: result.Rows[:0], | ||
} | ||
|
||
var currentKey []sqltypes.Value | ||
var lastRow sqltypes.Row | ||
var err error | ||
for _, row := range result.Rows { | ||
var nextGroup bool | ||
|
||
currentKey, nextGroup, err = oa.nextGroupBy(currentKey, row) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if nextGroup { | ||
out.Rows = append(out.Rows, lastRow) | ||
} | ||
lastRow = row | ||
} | ||
out.Rows = append(out.Rows, lastRow) | ||
return out, nil | ||
} | ||
|
||
func (oa *OrderedAggregate) execute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable) (*sqltypes.Result, error) { | ||
result, err := vcursor.ExecutePrimitive( | ||
ctx, | ||
|
@@ -124,6 +153,10 @@ func (oa *OrderedAggregate) execute(ctx context.Context, vcursor VCursor, bindVa | |
if err != nil { | ||
return nil, err | ||
} | ||
if len(oa.Aggregates) == 0 { | ||
return oa.executeGroupBy(result) | ||
} | ||
|
||
agg, fields, err := newAggregation(result.Fields, oa.Aggregates) | ||
if err != nil { | ||
return nil, err | ||
|
@@ -160,8 +193,63 @@ func (oa *OrderedAggregate) execute(ctx context.Context, vcursor VCursor, bindVa | |
return out, nil | ||
} | ||
|
||
func (oa *OrderedAggregate) executeStreamGroupBy(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, callback func(*sqltypes.Result) error) error { | ||
cb := func(qr *sqltypes.Result) error { | ||
return callback(qr.Truncate(oa.TruncateColumnCount)) | ||
} | ||
|
||
var fields []*querypb.Field | ||
var currentKey []sqltypes.Value | ||
var lastRow sqltypes.Row | ||
|
||
visitor := func(qr *sqltypes.Result) error { | ||
var err error | ||
if fields == nil && len(qr.Fields) > 0 { | ||
fields = qr.Fields | ||
if err = cb(&sqltypes.Result{Fields: fields}); err != nil { | ||
return err | ||
} | ||
} | ||
for _, row := range qr.Rows { | ||
var nextGroup bool | ||
|
||
currentKey, nextGroup, err = oa.nextGroupBy(currentKey, row) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if nextGroup { | ||
// this is a new grouping. let's yield the old one, and start a new | ||
if err := cb(&sqltypes.Result{Rows: []sqltypes.Row{lastRow}}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
lastRow = row | ||
} | ||
return nil | ||
} | ||
|
||
/* we need the input fields types to correctly calculate the output types */ | ||
err := vcursor.StreamExecutePrimitive(ctx, oa.Input, bindVars, true, visitor) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if lastRow != nil { | ||
if err := cb(&sqltypes.Result{Rows: [][]sqltypes.Value{lastRow}}); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
// TryStreamExecute is a Primitive function. | ||
func (oa *OrderedAggregate) TryStreamExecute(ctx context.Context, vcursor VCursor, bindVars map[string]*querypb.BindVariable, _ bool, callback func(*sqltypes.Result) error) error { | ||
if len(oa.Aggregates) == 0 { | ||
return oa.executeStreamGroupBy(ctx, vcursor, bindVars, callback) | ||
} | ||
|
||
cb := func(qr *sqltypes.Result) error { | ||
return callback(qr.Truncate(oa.TruncateColumnCount)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Header is missing here