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

kvstreamer: optimize singleRangeBatch.Less #82382

Merged
merged 2 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
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
69 changes: 69 additions & 0 deletions pkg/bench/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,6 +1200,75 @@ func BenchmarkIndexJoinColumnFamilies(b *testing.B) {
})
}

// BenchmarkLookupJoinEqColsAreKeyNoOrdering measures a lookup-join with 1000
// rows when equality columns are key and ordering doesn't have to be
// maintained.
func BenchmarkLookupJoinEqColsAreKeyNoOrdering(b *testing.B) {
defer log.Scope(b).Close(b)
ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
db.Exec(b, `CREATE TABLE t1 (a INT)`)
db.Exec(b, `INSERT INTO t1 SELECT generate_series(1, 1000)`)
db.Exec(b, `CREATE TABLE t2 (a INT PRIMARY KEY, b INT)`)
db.Exec(b, `INSERT INTO t2 SELECT generate_series(1, 1000), (random()*1000)::INT`)
b.ResetTimer()

for i := 0; i < b.N; i++ {
db.Exec(b, `SELECT * FROM t1 INNER LOOKUP JOIN t2 ON t1.a = t2.a`)
}
})
}

// BenchmarkLookupJoinEqColsAreKeyOrdering measures a lookup-join with 1000 rows
// when equality columns are key and ordering needs to be maintained.
func BenchmarkLookupJoinEqColsAreKeyOrdering(b *testing.B) {
defer log.Scope(b).Close(b)
ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
db.Exec(b, `CREATE TABLE t1 (a INT PRIMARY KEY)`)
db.Exec(b, `INSERT INTO t1 SELECT generate_series(1, 1000)`)
db.Exec(b, `CREATE TABLE t2 (a INT PRIMARY KEY, b INT)`)
db.Exec(b, `INSERT INTO t2 SELECT generate_series(1, 1000), (random()*1000)::INT`)
b.ResetTimer()

for i := 0; i < b.N; i++ {
db.Exec(b, `SELECT * FROM t1 INNER LOOKUP JOIN t2 ON t1.a = t2.a ORDER BY t1.a`)
}
})
}

// BenchmarkLookupJoinNoOrdering measures a lookup-join with 1000 rows and
// ordering doesn't have to be maintained.
func BenchmarkLookupJoinNoOrdering(b *testing.B) {
defer log.Scope(b).Close(b)
ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
db.Exec(b, `CREATE TABLE t1 (a INT)`)
db.Exec(b, `INSERT INTO t1 SELECT generate_series(1, 1000)`)
db.Exec(b, `CREATE TABLE t2 (a INT, INDEX (a))`)
db.Exec(b, `INSERT INTO t2 SELECT generate_series(1, 1000)`)
b.ResetTimer()

for i := 0; i < b.N; i++ {
db.Exec(b, `SELECT * FROM t1 INNER LOOKUP JOIN t2 ON t1.a = t2.a`)
}
})
}

// BenchmarkLookupJoinOrdering measures a lookup-join with 1000 rows when
// ordering needs to be maintained.
func BenchmarkLookupJoinOrdering(b *testing.B) {
defer log.Scope(b).Close(b)
ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
db.Exec(b, `CREATE TABLE t1 (a INT PRIMARY KEY)`)
db.Exec(b, `INSERT INTO t1 SELECT generate_series(1, 1000)`)
db.Exec(b, `CREATE TABLE t2 (a INT, INDEX (a))`)
db.Exec(b, `INSERT INTO t2 SELECT generate_series(1, 1000)`)
b.ResetTimer()

for i := 0; i < b.N; i++ {
db.Exec(b, `SELECT * FROM t1 INNER LOOKUP JOIN t2 ON t1.a = t2.a ORDER BY t1.a`)
}
})
}

func BenchmarkSortJoinAggregation(b *testing.B) {
defer log.Scope(b).Close(b)
ForEachDB(b, func(b *testing.B, db *sqlutils.SQLRunner) {
Expand Down
13 changes: 10 additions & 3 deletions pkg/kv/kvclient/kvstreamer/requests_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ import (
// singleRangeBatch.
type singleRangeBatch struct {
reqs []roachpb.RequestUnion
// reqsKeys stores the start key of the corresponding request in reqs. It is
// only set prior to sorting reqs within this object (currently, this is
// done only in the OutOfOrder mode for the original requests - resume
// requests don't set this), and the reference is niled out right after
// sorting is complete. Thus, this slice doesn't have to be accounted for.
reqsKeys []roachpb.Key
// positions is a 1-to-1 mapping with reqs to indicate which ordinal among
// the originally enqueued requests a particular reqs[i] corresponds to. In
// other words, if reqs[i] is (or a part of) enqueuedReqs[j], then
Expand Down Expand Up @@ -90,6 +96,9 @@ func (r *singleRangeBatch) Len() int {

func (r *singleRangeBatch) Swap(i, j int) {
r.reqs[i], r.reqs[j] = r.reqs[j], r.reqs[i]
if r.reqsKeys != nil {
r.reqsKeys[i], r.reqsKeys[j] = r.reqsKeys[j], r.reqsKeys[i]
}
r.positions[i], r.positions[j] = r.positions[j], r.positions[i]
if r.subRequestIdx != nil {
r.subRequestIdx[i], r.subRequestIdx[j] = r.subRequestIdx[j], r.subRequestIdx[i]
Expand All @@ -98,9 +107,7 @@ func (r *singleRangeBatch) Swap(i, j int) {

// Less returns true if r.reqs[i]'s key comes before r.reqs[j]'s key.
func (r *singleRangeBatch) Less(i, j int) bool {
// TODO(yuzefovich): figure out whether it's worth extracting the keys when
// constructing singleRangeBatch object.
return r.reqs[i].GetInner().Header().Key.Compare(r.reqs[j].GetInner().Header().Key) < 0
return r.reqsKeys[i].Compare(r.reqsKeys[j]) < 0
}

// priority returns the priority value of this batch.
Expand Down
7 changes: 7 additions & 0 deletions pkg/kv/kvclient/kvstreamer/streamer.go
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,7 @@ func (s *Streamer) Enqueue(ctx context.Context, reqs []roachpb.RequestUnion) (re
s.mu.Unlock()
}
}()
var reqsKeysScratch []roachpb.Key
for ; ri.Valid(); ri.Seek(ctx, seekKey, scanDir) {
// Truncate the request span to the current range.
singleRangeSpan, err := rs.Intersect(ri.Token().Desc())
Expand Down Expand Up @@ -516,7 +517,13 @@ func (s *Streamer) Enqueue(ctx context.Context, reqs []roachpb.RequestUnion) (re
// can become head-of-the-line in the future. We probably will need
// to introduce a way to "restore" the original order within
// singleRangeBatch if it is sorted and issued with headOfLine=true.
r.reqsKeys = reqsKeysScratch[:0]
for i := range r.reqs {
r.reqsKeys = append(r.reqsKeys, r.reqs[i].GetInner().Header().Key)
}
sort.Sort(&r)
reqsKeysScratch = r.reqsKeys
r.reqsKeys = nil
}

requestsToServe = append(requestsToServe, r)
Expand Down