Skip to content

Commit

Permalink
Merge #61583
Browse files Browse the repository at this point in the history
61583: sql: issue GetRequests from SQL when possible r=jordanlewis a=jordanlewis

Closes #46758.

Previously, SQL unconditionally emitted ScanRequests for every read that
it produced, even if we were completely sure that the scan could contain
only a single key that was fully known at request time.

This was less efficient than necessary, because ScanRequests cause the
KV layer to do more work than GetRequests.

Here are the reasons why GetRequests are more efficient:

- MVCCGet, unlike MVCCScan, is able to use a prefix iterator, which
  allows it to make use of RocksDB/Pebble bloom filters. There may also
  be other wins at the storage layer, like avoiding prefetching
- various data structures have optimizations and fast-paths for
  single-key operations (e.g. timestamp cache, latch manager, refresh
  span tracking)
- various code paths are simpler in the point operation case
- a point operation implies only needing a single key allocation instead
  of a start and end key allocation
- a point operation implies only needing to return up to a single
  result, which means that we can avoid some indirection in various
  places
- A scan over a span that has at most 1 key, but does not know it, needs
  to iterate over all the versions (or eventually do a second expensive
  seek), looking for the non-existent next key in the span. A Get can do
  a single seek, read the key, and be done.

Release note (performance improvement): SQL will now emit GetRequests
when possible to KV instead of always emitting ScanRequests. This
manifests as a modest performance improvement for some workloads.

Co-authored-by: Jordan Lewis <[email protected]>
  • Loading branch information
craig[bot] and jordanlewis committed Apr 14, 2021
2 parents 87ed071 + 907ee18 commit 2e60515
Show file tree
Hide file tree
Showing 41 changed files with 594 additions and 295 deletions.
6 changes: 2 additions & 4 deletions pkg/ccl/logictestccl/testdata/logic_test/multi_region
Original file line number Diff line number Diff line change
Expand Up @@ -1474,7 +1474,7 @@ vectorized: true
table: orders@primary
spans: [/'ap-southeast-2'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727' - /'ap-southeast-2'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727'] [/'us-east-1'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727' - /'us-east-1'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727']
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJykk9tO20AQhu_7FKPhgoM2cnyABEtIRm1Kg9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Oa-o_l0MjjmvBnn35PcvzqvcE5xXnlCHddDf4tjtQaoEeKDdFRlkOC1dBLHHYp_FASYrhrp0d0ytSy8JI2_FXqbGe7KamnfvRYqerMLfqOKOfKkajiTWiCerf-ic6I4uuv66wrFcSgfeRg695zh5oqW6MTJYX9Me8z0jx1p_Kwv4qqUCrSKIa8CMlCDTOANxwCD262833ttIL3gOvRnZQitLD6zaJDxhSOKSWr-tLk1Gp0ZnzZo2nDa4JiHIurbqt8FINaXmEu-Dvf8B-4-CgzVw7yE4eBQcPgAnq1d_AgAA__8yFKQ7

query T nodeidx=3
USE multi_region_test_db; EXPLAIN (DISTSQL) SELECT
Expand Down Expand Up @@ -1511,7 +1511,7 @@ vectorized: true
table: orders@primary
spans: [/'ap-southeast-2'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727' - /'ap-southeast-2'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727'] [/'us-east-1'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727' - /'us-east-1'/'94e4b847-8f2f-4ac5-83f1-641d6e3df727']
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJykk19v2jwUxu_fT2GdXvRdZZq_BRqpkquNbVQMOqi0STNCaXzSegM7c5wtU8XVPtu-15SEtgMN1LILSHx8fjnPeexzB_nXOUTQ-3g5OO8Pyf-v-pOryfvBCzLpDXovr4g2As1MCkoyoxc6P64fq4BMkJxPmpfZd2lvdWFndcL97hERMk90oex6YpNFXo9H75oSObkY9YerImR0_3YsVaoJ44XrBrjKbGIf3vbGvQd55IwcnoYYXnfDTqub-mkrjJOTVjdIvVY79EQbA5F2_M4hUFBa4DBeYA7RJwhhSiEzOsE816YK3dUJfVFC5FKQKitsFZ5SSLRBiO7ASjtHiOAqvp7jGGOBxnGBgkAby3n92UYpy4xcxOYHUJhkscoj4nBgHBwOnJenIeclVn_X3Tecl93Uufj1k_My9QTnpSfUWbXoHHJo7ck5ByRWgnhE21s0QGFU2IgwjzKfshCmSwq6sI8d5ja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9i_1_EwV5xuCH2tsXhXnG0XxztFR9viWer_34HAAD__0Dh_b0=

subtest enum_name_clash

Expand All @@ -1527,5 +1527,3 @@ ALTER DATABASE db_enum_name_clash SET PRIMARY REGION "us-east-1"
statement ok
DROP TYPE db_enum_name_clash.crdb_internal_region;
ALTER DATABASE db_enum_name_clash SET PRIMARY REGION "us-east-1"


28 changes: 14 additions & 14 deletions pkg/ccl/logictestccl/testdata/logic_test/regional_by_row
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
OR message LIKE 'Scan%'
ORDER BY ordinality ASC
----
Scan /Table/73/1/"@"/1{-/#}, /Table/73/1/"\x80"/1{-/#}, /Table/73/1/"\xc0"/1{-/#}
Scan /Table/73/1/"@"/1/0, /Table/73/1/"\x80"/1/0, /Table/73/1/"\xc0"/1/0
fetched: /regional_by_row_table/primary/'ap-southeast-2'/1/pk2/a/b/j -> /1/2/3/'{"a": "b"}'
output row: [1 1 2 3 '{"a": "b"}']

Expand All @@ -716,7 +716,7 @@ vectorized: true
table: regional_by_row_table@primary
spans: [/'ca-central-1'/1 - /'ca-central-1'/1] [/'us-east-1'/1 - /'us-east-1'/1]
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJykkVEL0zAQx9_9FOF8Uclo0ylIQIhoxULdZjtQsGVkzTGLXVKTFDdGv7usRbbJJmw-3l3-l98vOYD72QCH-OsifZvMyLP3Sb7MP6fPSR6n8bsleUE-ZPNPxOKmNlo2q_V-Zc2vlZfrBsmXj3EWk_YHeUMYUNBG4Uxu0QH_BgxKCq01FTpn7LF1GA4kagc8pFDrtvPHdkmhMhaBH8DXvkHgsDxuz1AqtEEIFBR6WTfD2qsgorX1Vto9UMhbqR0nQQFFsXsdFhCwyXkRPCVSK8KI8d_RAoV55zkRjIqIiikVL6l4BWVPwXT-BOi83CBw1tPHJNijEuKPgBjh7wSObgKfODttrEKL6oKx7K8ozczEtEF0KZPW29oTdpMhvOfRMnSt0Q7_Yrm1uaSAaoOjkDOdrXBhTTVcM5bzITc0FDo_TqOxSPQwGn71PMz-Jxz9Mzy9CId92T_5HQAA__-b4SGh
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJykkVFr2zAUhd_3K8R92oaGLWeDIRh4bC41uElqB1qoTVCsS2rqSK4k04Tg_15iP6QpSSHp4z1X5-g70hbscw0covtp8jcek6__42yW3SbfSBYl0b8Z-U6u0skNMbistBL1fLGZG_0yd2JRI7m7jtKINE_kD2FAQWmJY7FCC_wBGBQUGqNLtFabnbTtD8RyDdynUKmmdTu5oFBqg8C34CpXI3CY7dJTFBKN5wMFiU5UdR97FCRsTLUSZgMUskYoy4mXQ56vf_s5eMzziVCSMKLdIxqgMGkdJyGjYUDDEQ1_0vAXFB0F3bo9k3ViicBZRy_jZpdyhwPzmZzBSc49Xqu0kWhQHqAV3ZEmY_1DN15w2CGpVpUj7CSDf85bpWgbrSy-YzmVXFBAucShkNWtKXFqdNlfM4yT3tcLEq0btsEwxKpf9Z_51sw-Yw4-NI8OzH5XdF9eAwAA__86rhxz

statement ok
SET tracing = on,kv,results; SELECT * FROM regional_by_row_table WHERE pk = 1; SET tracing = off
Expand All @@ -728,7 +728,7 @@ SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
OR message LIKE 'Scan%'
ORDER BY ordinality ASC
----
Scan /Table/73/1/"@"/1{-/#}
Scan /Table/73/1/"@"/1/0
fetched: /regional_by_row_table/primary/'ap-southeast-2'/1/pk2/a/b/j -> /1/2/3/'{"a": "b"}'
output row: [1 1 2 3 '{"a": "b"}']

Expand All @@ -743,8 +743,8 @@ SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
OR message LIKE 'Scan%'
ORDER BY ordinality ASC
----
Scan /Table/73/1/"@"/10{-/#}
Scan /Table/73/1/"\x80"/10{-/#}, /Table/73/1/"\xc0"/10{-/#}
Scan /Table/73/1/"@"/10/0
Scan /Table/73/1/"\x80"/10/0, /Table/73/1/"\xc0"/10/0
fetched: /regional_by_row_table/primary/'ca-central-1'/10/pk2/a/b -> /10/11/12
output row: [10 10 11 12 NULL]

Expand Down Expand Up @@ -824,9 +824,9 @@ SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
OR message LIKE 'Scan%'
ORDER BY ordinality ASC
----
Scan /Table/75/1/"@"/10{-/#}, /Table/75/1/"\x80"/10{-/#}, /Table/75/1/"\xc0"/10{-/#}
Scan /Table/75/1/"@"/10/0{-/NULL}, /Table/75/1/"\x80"/10/0{-/NULL}, /Table/75/1/"\xc0"/10/0{-/NULL}
fetched: /child/primary/'ap-southeast-2'/10/c_p_id -> /10
Scan /Table/74/1/"@"/10{-/#}, /Table/74/1/"\x80"/10{-/#}, /Table/74/1/"\xc0"/10{-/#}
Scan /Table/74/1/"@"/10/0, /Table/74/1/"\x80"/10/0, /Table/74/1/"\xc0"/10/0
fetched: /parent/primary/'ap-southeast-2'/10 -> NULL

statement ok
Expand Down Expand Up @@ -862,7 +862,7 @@ vectorized: true
table: child@primary
spans: [/'ca-central-1'/10 - /'ca-central-1'/10] [/'us-east-1'/10 - /'us-east-1'/10]
·
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJy0k1Fr2zAQx9_3KY7bQ5KhEtlxoQgKLqvLXDynSwwrLKZ41tF6SyRPliEl5LuP2GGNwxLSjr35Tve3fv_T3QqrX3MUGNzfRVdhDP3rcJpMv0QDmAZR8DGBD3AzGX-G_KmYS_j6KZgE0I_HCQT3m0Lod8vKzJCy27ryoZBwCfnD5mMwgKv4Gvp5m3T4ABkqLSnOFlSh-IYOpgxLo3OqKm02qVVTEMolCs6wUGVtN-mUYa4NoVihLeycUGCSfZ_ThDJJZsiRoSSbFfPmtw25X5pikZlnZDgtM1UJGM5wNlte8BkOHX7WiYbvIVMSHND2iQwyHNdWgO8w38V0zVDX9oWkstkjoXDW7G20zqm0_h9Sf0t5Gpl7kOwFqFbaSDIkOzDp-i_ssT7T5dDtUkfForDgHGTgr-nOrS7Utjmj7jXJc0kCouAmgas4CeF2HMbIsB26naZFWv-sS_ihCwVaCej7I7gE392OoO_BJSx7Hu8JIXyHc37uDU5r5uiNRrz_ZWSzssvexa4VBste3vF2ojnvNeYmVJVaVbQ3MYfeP2VI8pHasat0bXK6MzpvrmnDcaNrEpIq2566bRCq5qhZsl2x8y9i96h41BHzffHoqNg7LvaOis_3xOn63e8AAAD__2Pcwcc=
Diagram: https://cockroachdb.github.io/distsqlplan/decode.html#eJy0k2Fr2zwQx98_n-K4N0keNCI7LhRBwGF1mYvndIlhhcUUzzpab4nkyTKkhHz3ETvQOCwh7dg763R_3-9_uttg9WuJAoOH-2gSxtC_CefJ_Es0gHkQBR8T-B9uZ9PPkD8XSwlfPwWzAPrxNIHgYZcI_W5amRlSdp9XPhYSxpA_7j4GA5jEN9DP26DDB8hQaUlxtqIKxTd0MGVYGp1TVWmzC22ahFCuUXCGhSpruwunDHNtCMUGbWGXhAKT7PuSZpRJMkOODCXZrFg2v23I_dIUq8y8IMN5malKwHCBi8X6mi9w6PAhh0xJcEDbZzLIcFpbAb7DfBfTLUNd29falc2eCIWzZe_jcy7l8_dsl_G4J3leMWqljSRDsoOQbv9AHOsPuhy6XdaoWBUWnJMM_C09udOF2rdk1C2TvJQkIApuE5jESQh30zBGhu1wHbQq0vpnXcIPXSjQSkDfH8EYfHc_ar4HY1j3PN4TQvgO5_zKG1zWzNE7jXj_yshuNde960MrDNa9vOPtQnPeW8zNqCq1quhoYk69f8qQ5BO1Y1fp2uR0b3TelGmP00bXBCRVtr1120OomqtmtQ7Fzt-I3bPiUUfMj8Wjs2LvvNg7K746Eqfb_34HAAD__-igvDk=

statement ok
SET tracing = on,kv,results; SELECT * FROM child WHERE NOT EXISTS (SELECT * FROM parent WHERE p_id = c_p_id) AND c_id = 10; SET tracing = off
Expand All @@ -874,9 +874,9 @@ SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
OR message LIKE 'Scan%'
ORDER BY ordinality ASC
----
Scan /Table/75/1/"@"/10{-/#}
Scan /Table/75/1/"@"/10/0
fetched: /child/primary/'ap-southeast-2'/10/c_p_id -> /10
Scan /Table/74/1/"@"/10{-/#}
Scan /Table/74/1/"@"/10/0
fetched: /parent/primary/'ap-southeast-2'/10 -> NULL

statement ok
Expand All @@ -890,11 +890,11 @@ SELECT message FROM [SHOW KV TRACE FOR SESSION] WITH ORDINALITY
OR message LIKE 'Scan%'
ORDER BY ordinality ASC
----
Scan /Table/75/1/"@"/20{-/#}
Scan /Table/75/1/"\x80"/20{-/#}, /Table/75/1/"\xc0"/20{-/#}
Scan /Table/75/1/"@"/20/0
Scan /Table/75/1/"\x80"/20/0, /Table/75/1/"\xc0"/20/0
fetched: /child/primary/'ca-central-1'/20/c_p_id -> /20
Scan /Table/74/1/"@"/20{-/#}
Scan /Table/74/1/"\x80"/20{-/#}, /Table/74/1/"\xc0"/20{-/#}
Scan /Table/74/1/"@"/20/0
Scan /Table/74/1/"\x80"/20/0, /Table/74/1/"\xc0"/20/0
fetched: /parent/primary/'ca-central-1'/20 -> NULL

query T
Expand Down
2 changes: 1 addition & 1 deletion pkg/kv/kvclient/kvcoord/dist_sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,7 +613,7 @@ func (ds *DistSender) initAndVerifyBatch(
case *roachpb.ReverseScanRequest:
// Accepted reverse range requests.

case *roachpb.QueryIntentRequest, *roachpb.EndTxnRequest:
case *roachpb.QueryIntentRequest, *roachpb.EndTxnRequest, *roachpb.GetRequest:
// Accepted point requests that can be in batches with limit.

default:
Expand Down
1 change: 1 addition & 0 deletions pkg/kv/kvserver/batcheval/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ go_test(
"cmd_add_sstable_test.go",
"cmd_clear_range_test.go",
"cmd_end_transaction_test.go",
"cmd_get_test.go",
"cmd_lease_test.go",
"cmd_recover_txn_test.go",
"cmd_refresh_range_test.go",
Expand Down
21 changes: 20 additions & 1 deletion pkg/kv/kvserver/batcheval/cmd_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,22 @@ func Get(
h := cArgs.Header
reply := resp.(*roachpb.GetResponse)

val, intent, err := storage.MVCCGet(ctx, reader, args.Key, h.Timestamp, storage.MVCCGetOptions{
if h.MaxSpanRequestKeys < 0 || h.TargetBytes < 0 {
// Receipt of a GetRequest with negative MaxSpanRequestKeys or TargetBytes
// indicates that the request was part of a batch that has already exhausted
// its limit, which means that we should *not* serve the request and return
// a ResumeSpan for this GetRequest.
//
// This mirrors the logic in MVCCScan, though the logic in MVCCScan is
// slightly lower in the stack.
reply.ResumeSpan = &roachpb.Span{Key: args.Key}
reply.ResumeReason = roachpb.RESUME_KEY_LIMIT
return result.Result{}, nil
}
var val *roachpb.Value
var intent *roachpb.Intent
var err error
val, intent, err = storage.MVCCGet(ctx, reader, args.Key, h.Timestamp, storage.MVCCGetOptions{
Inconsistent: h.ReadConsistency != roachpb.CONSISTENT,
Txn: h.Txn,
FailOnMoreRecent: args.KeyLocking != lock.None,
Expand All @@ -41,6 +56,10 @@ func Get(
if err != nil {
return result.Result{}, err
}
if val != nil {
reply.NumKeys = 1
reply.NumBytes = int64(len(val.RawBytes))
}
var intents []roachpb.Intent
if intent != nil {
intents = append(intents, *intent)
Expand Down
96 changes: 96 additions & 0 deletions pkg/kv/kvserver/batcheval/cmd_get_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// Copyright 2021 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.

package batcheval

import (
"context"
"testing"

"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/stretchr/testify/assert"
)

// TestGetResumeSpan tests that a GetRequest with a target bytes or max span
// request keys is properly handled by returning no result with a resume span.
func TestGetResumeSpan(t *testing.T) {
defer leaktest.AfterTest(t)()
ctx := context.Background()
resp := &roachpb.GetResponse{}
key := roachpb.Key([]byte{'a'})
value := roachpb.MakeValueFromString("woohoo")

db := storage.NewDefaultInMemForTesting()
defer db.Close()

_, err := Put(ctx, db, CommandArgs{
Header: roachpb.Header{TargetBytes: -1},
Args: &roachpb.PutRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
Value: value,
},
}, resp)
assert.NoError(t, err)

// Case 1: Check that a negative TargetBytes causes a resume span.
_, err = Get(ctx, db, CommandArgs{
Header: roachpb.Header{TargetBytes: -1},
Args: &roachpb.GetRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
},
}, resp)
assert.NoError(t, err)

assert.NotNil(t, resp.ResumeSpan)
assert.Equal(t, key, resp.ResumeSpan.Key)
assert.Nil(t, resp.ResumeSpan.EndKey)
assert.Nil(t, resp.Value)

resp = &roachpb.GetResponse{}
// Case 2: Check that a negative MaxSpanRequestKeys causes a resume span.
_, err = Get(ctx, db, CommandArgs{
Header: roachpb.Header{MaxSpanRequestKeys: -1},
Args: &roachpb.GetRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
},
}, resp)
assert.NoError(t, err)

assert.NotNil(t, resp.ResumeSpan)
assert.Equal(t, key, resp.ResumeSpan.Key)
assert.Nil(t, resp.ResumeSpan.EndKey)
assert.Nil(t, resp.Value)

resp = &roachpb.GetResponse{}
// Case 3: Check that a positive limit causes a normal return.
_, err = Get(ctx, db, CommandArgs{
Header: roachpb.Header{MaxSpanRequestKeys: 10, TargetBytes: 100},
Args: &roachpb.GetRequest{
RequestHeader: roachpb.RequestHeader{
Key: key,
},
},
}, resp)
assert.NoError(t, err)

assert.Nil(t, resp.ResumeSpan)
assert.NotNil(t, resp.Value)
assert.Equal(t, resp.Value.RawBytes, value.RawBytes)
assert.Equal(t, 1, int(resp.NumKeys))
assert.Equal(t, len(resp.Value.RawBytes), int(resp.NumBytes))
}
Loading

0 comments on commit 2e60515

Please sign in to comment.