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

sql: EXPLAIN ANALYZE (DEBUG) 4-12x slower for queries with large lookup joins #90739

Open
michae2 opened this issue Oct 26, 2022 · 4 comments
Open
Labels
A-sql-explain Issues related to EXPLAIN and EXPLAIN ANALYZE improvements C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team X-nostale Marks an issue/pr that should be ignored by the stale bot

Comments

@michae2
Copy link
Collaborator

michae2 commented Oct 26, 2022

Queries that perform large lookup joins (tens of thousands of rows) are roughly 4-12x slower to EXPLAIN ANALYZE than to execute normally. This seems to be true on 21.2, 22.1, 22.2, and probably earlier releases as well. Here's a demonstration on 22.2.0:

CREATE TABLE t (a PRIMARY KEY, b) AS SELECT i, i FROM generate_series(0, 99999) s(i);
CREATE TABLE u (x PRIMARY KEY, y) AS SELECT * FROM t;
ANALYZE t;
ANALYZE u;
SELECT count(*) FROM t JOIN u@{NO_FULL_SCAN} ON x = b WHERE a >= 50000;
EXPLAIN ANALYZE SELECT count(*) FROM t JOIN u@{NO_FULL_SCAN} ON x = b WHERE a >= 50000;

On a one-node cockroach demo the normal execution takes 274 ms, and the EXPLAIN ANALYZE takes 3.29s:

[email protected]:26257/defaultdb> SELECT count(*) FROM t JOIN u@{NO_FULL_SCAN} ON x = b WHERE a >= 50000;
  count
---------
  50000
(1 row)


Time: 274ms total (execution 274ms / network 0ms)

[email protected]:26257/defaultdb> EXPLAIN ANALYZE SELECT count(*) FROM t JOIN u@{NO_FULL_SCAN} ON x = b WHERE a >= 50000;
                                            info
--------------------------------------------------------------------------------------------
  planning time: 1ms
  execution time: 3.3s
  distribution: full
  vectorized: true
  rows read from KV: 100,000 (2.4 MiB, 5 gRPC calls)
  cumulative time spent in KV: 3.2s
  maximum memory usage: 23 MiB
  network usage: 0 B (0 messages)
  regions: us-east1

  • group (scalar)
  │ nodes: n1
  │ regions: us-east1
  │ actual row count: 1
  │ estimated row count: 1
  │
  └── • lookup join
      │ nodes: n1
      │ regions: us-east1
      │ actual row count: 50,000
      │ KV time: 3.2s
      │ KV contention time: 0µs
      │ KV rows read: 50,000
      │ KV bytes read: 766 KiB
      │ KV gRPC calls: 4
      │ estimated max memory allocated: 22 MiB
      │ estimated row count: 50,067
      │ table: u@u_pkey
      │ equality: (b) = (x)
      │ equality cols are key
      │
      └── • scan
            nodes: n1
            regions: us-east1
            actual row count: 50,000
            KV time: 17ms
            KV contention time: 0µs
            KV rows read: 50,000
            KV bytes read: 1.6 MiB
            KV gRPC calls: 1
            estimated max memory allocated: 1.6 MiB
            estimated row count: 49,896 (50% of the table; stats collected 24 seconds ago)
            table: t@t_pkey
            spans: [/50000 - ]
(44 rows)


Time: 3.290s total (execution 3.289s / network 0.000s)

We record many events in the trace for each lookup of the lookup join, which happens 50k times for this query.

Jira issue: CRDB-20918

@michae2 michae2 added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. A-sql-explain Issues related to EXPLAIN and EXPLAIN ANALYZE improvements labels Oct 26, 2022
@michae2
Copy link
Collaborator Author

michae2 commented Oct 26, 2022

(Note that this is different from #80671 which was fixed in 21.2.)

@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Oct 28, 2022
@DrewKimball
Copy link
Collaborator

5-second cpu and heap profiles from running the explain analyze query repeatedly against a single-node cluster:
cpu-profile.pb.gz
Screen Shot 2022-10-29 at 12 15 09 PM

heap-profile.pb.gz
Screen Shot 2022-10-29 at 12 15 31 PM

@yuzefovich
Copy link
Member

yuzefovich commented Nov 1, 2022

From the team meeting:

@yuzefovich yuzefovich changed the title EXPLAIN ANALYZE 4-12x slower for queries with large lookup joins sql: EXPLAIN ANALYZE (DEBUG) 4-12x slower for queries with large lookup joins Nov 2, 2022
craig bot pushed a commit that referenced this issue Nov 3, 2022
89632: go.mod: bump raft r=nvanbenschoten a=tbg

```
go get go.etcd.io/etcd/raft/v3@d19116e6ee66e52a5fd8cce2e10f9422fb80e42f

go: downloading go.etcd.io/etcd/raft/v3 v3.6.0-alpha.0.0.20221009201006-d19116e6ee66
go: module github.com/golang/protobuf is deprecated: Use the "google.golang.org/protobuf" module instead.
go: upgraded go.etcd.io/etcd/api/v3 v3.5.0 => v3.6.0-alpha.0
go: upgraded go.etcd.io/etcd/raft/v3 v3.0.0-20210320072418-e51c697ec6e8 => v3.6.0-alpha.0.0.20221009201006-d19116e6ee66
```

This picks up

- etcd-io/etcd#14413
- etcd-io/etcd#14538

Compared single-node performance on gceworker via

```bash
#!/bin/bash
set -euxo pipefail
pkill -9 cockroach || true
rm -rf cockroach-data
cr=./cockroach-$1
$cr start-single-node --background --insecure
$cr workload init kv
$cr workload run kv --splits 100 --max-rate 2000 --duration 10m --read-percent 0 --min-block-bytes 10 --max-block-bytes 10 | tee $1.txt
```

```
_elapsed___errors_____ops(total)___ops/sec(cum)__avg(ms)__p50(ms)__p95(ms)__p99(ms)_pMax(ms)__total
  600.0s        0        1199604         1999.3      6.7      7.1     10.0     11.0     75.5  write #master
  600.0s        0        1199614         1999.4      6.8      7.1     10.0     11.0     79.7  write #PR
```

Closes #87264.

- [x] [make it build](#88985 (comment))
- [x] remove the maxIndex param and handling from Task.AckCommittedEntriesBeforeApplication
- [x] check that single node write latencies don't regress

Release note: None


91117: sql: reduce the overhead of EXPLAIN ANALYZE r=yuzefovich a=yuzefovich

In order to propagate the execution stats across the distributed query plan we use the tracing infrastructure, where each stats object is added as "structured metadata" to the trace. Thus, whenever we're collecting the exec stats for a statement, we must enable tracing. Previously, in many cases we would enable it at the highest verbosity level which has non-trivial overhead. In some cases this was an overkill (e.g. in `EXPLAIN ANALYZE` we don't really care about the trace containing all of the gory details - we won't expose it anyway), so this is now fixed by using the less verbose "structured" verbosity level. As a concrete example of the difference: for a stmt that without `EXPLAIN ANALYZE` takes around 190ms, with `EXPLAIN ANALYZE` it would previously run for about 1.8s and now it takes around 210ms.

This required some minor changes to the row-by-row outbox and router
setups to collect thats even if the recording is not verbose.

Addresses: #90739.

Epic: None

Release note (performance improvement): The overhead of running `EXPLAIN ANALYZE` and `EXPLAIN ANALYZE (DISTSQL)` has been significantly reduced. The overhead of `EXPLAIN ANALYZE (DEBUG)` didn't change.

91119: roachprod: improve error in ParallelE r=smg260 a=tbg

Prior to this commit, the error's stack trace did not link back
to the caller of `ParallelE`. Now it does.

Epic: none
Release note: None


91126: dev: allow whitespace separated regexps for testlogic files r=ajwerner a=ajwerner

This was a feature of `make testlogic` and it was liked.

Fixes #91125

Release note: None

Co-authored-by: Tobias Grieger <[email protected]>
Co-authored-by: Yahor Yuzefovich <[email protected]>
Co-authored-by: Andrew Werner <[email protected]>
@mgartner mgartner moved this to Backlog (DO NOT ADD NEW ISSUES) in SQL Queries Jul 24, 2023
@mgartner mgartner moved this from Backlog (DO NOT ADD NEW ISSUES) to New Backlog in SQL Queries Apr 23, 2024

This comment was marked as resolved.

@michae2 michae2 added X-nostale Marks an issue/pr that should be ignored by the stale bot and removed no-issue-activity labels Apr 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-explain Issues related to EXPLAIN and EXPLAIN ANALYZE improvements C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. T-sql-queries SQL Queries Team X-nostale Marks an issue/pr that should be ignored by the stale bot
Projects
Status: Backlog
Development

No branches or pull requests

3 participants