Skip to content

MDEV-39538: Different costs when same range is read twice#5128

Open
bsrikanth-mariadb wants to merge 1 commit into
bb-12.3-MDEV-39368-test-replayfrom
13.1-MDEV-39538-different-cost-when-same-range-is-read-twice
Open

MDEV-39538: Different costs when same range is read twice#5128
bsrikanth-mariadb wants to merge 1 commit into
bb-12.3-MDEV-39368-test-replayfrom
13.1-MDEV-39538-different-cost-when-same-range-is-read-twice

Conversation

@bsrikanth-mariadb
Copy link
Copy Markdown
Contributor

When same range is used as a filter, once in the outer query block, and the second inside a sub query such as: -

select * from t1
where year(a) = 2010 and c < (select count(*) from t1 where year(a) = 2010);

The Optimizer Context had two records for multi_range_read_info_const() call, but had different cost vector members.
The cause is that the first table considered index-only scan on the range, while the second considered non-index only scan.

However, when replaying the context, the same range got matched twice, and the costs corresponding to that got returned twice. Hence the costs in the explain plan output differed as well.

Solution

Include a new field called "call_number", while recording range contexts into the overall context. This way, we could even match the call_number and return the appropriate cost during replay.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces a tracking mechanism for multi-range read (MRR) calls using a counter (call_number) to resolve issues where different costs are calculated when the same range is read multiple times. Feedback on the changes highlights a critical null-pointer dereference risk in sql/opt_context_store_replay.cc where call_number is assigned before verifying if range_ctx is null. Additionally, an optimization is suggested to defer sequence iterator initialization until after verifying the call_number matches the current counter.

Comment thread sql/opt_context_store_replay.cc
Comment thread sql/opt_context_store_replay.cc Outdated
When same range is used as a filter, once in the outer query block, and
the second inside a sub query such as: -

select * from t1
  where year(a) = 2010 and c < (select count(*) from t1 where year(a) = 2010);

The Optimizer Context had two records for multi_range_read_info_const() call,
but had different cost vector members.
The cause is that the first table considered index-only scan on the range,
while the second considered non-index only scan.

However, when replaying the context, the same range got matched
twice, and the costs corresponding to that got returned twice.
Hence the costs in the explain plan output differed as well.

Solution
========
Include a new field called "call_number", while recording range contexts
into the overall context. This way, we could even match the call_number
and return the appropriate cost during replay.
@bsrikanth-mariadb bsrikanth-mariadb force-pushed the 13.1-MDEV-39538-different-cost-when-same-range-is-read-twice branch from 1d52c21 to c2c3b5e Compare May 26, 2026 09:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

1 participant