Skip to content

MDEV-39942: use actual_rec_per_key in loose scan sj optimization#5284

Draft
bsrikanth-mariadb wants to merge 1 commit into
mainfrom
13.1-MDEV-39942-loose-scan-sj-opt-uses-rec_per_key
Draft

MDEV-39942: use actual_rec_per_key in loose scan sj optimization#5284
bsrikanth-mariadb wants to merge 1 commit into
mainfrom
13.1-MDEV-39942-loose-scan-sj-opt-uses-rec_per_key

Conversation

@bsrikanth-mariadb

Copy link
Copy Markdown
Contributor

The filtered out value in the explain plan output should be determined based on the actual records per key value in the index. Instead, rec_per_key[] is used.

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

Copy link
Copy Markdown

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 updates Loose_scan_opt in sql/opt_subselect.h to call actual_rec_per_key() instead of directly accessing the rec_per_key array. The reviewer pointed out a potential precision loss issue because the returned floating-point value is assigned to a ulong variable (rpc), which can lead to inaccurate optimizer estimations. A refactoring was suggested to use a double type and avoid assignment inside the conditional statement.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment thread sql/opt_subselect.h
Comment on lines 246 to 249
ulong rpc;
if ((rpc= s->table->key_info[key].rec_per_key[max_loose_keypart]))
if ((rpc= s->table->key_info[key].actual_rec_per_key(
max_loose_keypart)))
records= records / rpc;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

The variable rpc is declared as ulong, but actual_rec_per_key() returns a floating-point value (rec_per_key_t, which is typically double). Assigning it to a ulong causes truncation and precision loss, which can lead to inaccurate cost/cardinality estimations in the optimizer.

Additionally, avoiding assignment inside the if condition improves readability.

        double rpc = s->table->key_info[key].actual_rec_per_key(max_loose_keypart);
        if (rpc > 0)
          records= records / rpc;

@bsrikanth-mariadb bsrikanth-mariadb marked this pull request as draft June 26, 2026 11:23
@bsrikanth-mariadb bsrikanth-mariadb force-pushed the 13.1-MDEV-39942-loose-scan-sj-opt-uses-rec_per_key branch from feb21a3 to 5d44415 Compare June 26, 2026 12:06
The filtered out value in the explain plan output should be determined
based on the actual records per key value in the index. Instead,
rec_per_key[] is used.
@bsrikanth-mariadb bsrikanth-mariadb force-pushed the 13.1-MDEV-39942-loose-scan-sj-opt-uses-rec_per_key branch from 5d44415 to 6ffa772 Compare June 26, 2026 12:48
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