Open michae2 opened 12 months ago
We don't remove the scan with a normal SQL statement, either:
root@localhost:26257/system/defaultdb> explain (opt, verbose) SELECT NULL FROM a WHERE a = NULL FOR UPDATE;
info
-------------------------------------------------------------
project
├── columns: "?column?":5
├── cardinality: [0 - 0]
├── volatile
├── stats: [rows=0]
├── cost: 1058.25
├── fd: ()-->(5)
├── prune: (5)
├── select
│ ├── cardinality: [0 - 0]
│ ├── volatile
│ ├── stats: [rows=0]
│ ├── cost: 1058.24
│ ├── scan a
│ │ ├── locking: for-update
│ │ ├── volatile
│ │ ├── stats: [rows=1000]
│ │ └── cost: 1048.22
│ └── filters
│ └── false [constraints=(contradiction; tight)]
└── projections
└── NULL [as="?column?":5]
(22 rows)
Time: 11ms total (execution 11ms / network 0ms)
We don't fire the rule that removes a zero-cardinality group when the expression isn't leakproof. The scan isn't leakproof here because of the locking. I'm not sure what was removing the scan in 22.2 that's different - it must have been some other rule that doesn't check this.
We probably shouldn't just remove locking (or any database state changes, like mutations), but in this case the SELECT FOR UPDATE
only applies to rows that pass the filter, which is none. So this is potentially an instance of https://github.com/cockroachdb/cockroach/issues/75457.
Also, we should probably be removing that lookup join too even though it's locking, since it should be guaranteed not to do any lookups.
Also, possible dupe of https://github.com/cockroachdb/cockroach/issues/73074
The lack of optimization of SFU does seem like a dupe of https://github.com/cockroachdb/cockroach/issues/73074, which is improved in 23.2 when using the new SFU implementation, so the only remaining question here is why we were optimizing at all in 22.2. I think there is some other minor difference due to the prepared statement. I"m going to keep this open because of that.
This exact combination of SELECT FOR UPDATE, EXISTS, and NULL parameter using a prepared statement is able to be optimized to a constant
false
in 22.2.16 but becomes a full table scan in 23.1.11:Here's v22.2.16:
Here's v23.1.11:
In v23.2.0-alpha.6 it's better if we use the new SELECT FOR UPDATE behavior (
optimizer_use_lock_op_for_serializable
):Jira issue: CRDB-33431