Bug #119541 Performance regression due to record buffer in index range scan

Submitted: 11 Dec 2025 12:32 Modified: 1 Dec 16:41
Reporter: Martin Hansson (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0, 8.4, 9.5 OS:Any
Assigned to: CPU Architecture:Any
Tags: index scan, record buffer, regression

[11 Dec 2025 12:32] Martin Hansson
Description:
Regression caused by a follow up fix for Bug#36775910: Record buffer not set in index range scans (SHA 2969af5)

Prior to this commit, the executioner consulted the storage engine for how many rows should be in the record buffer (handler::ha_is_record_buffer_wanted()). The line that caps the number of rows in the record buffer according to this number fell away in the bespoke commit, likely by mistake.

How to repeat:
CREATE TABLE t1 (
  id INT NOT NULL AUTO_INCREMENT,
  a INT,
  b INT,
  PRIMARY KEY (id),
  KEY i1 (b, a)
);

INSERT INTO t1(a, b) VALUES (1, 1);
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;
INSERT INTO t1(a, b) SELECT a, b FROM t1;

mysqlslap --socket=... --user=... --concurrency=1 --query="SELECT b, a FROM ps10232.t1 FORCE KEY(i1) WHERE b = 1 ORDER BY a DESC LIMIT 1" --number-of-queries=100000 --iterations=1

This benchmark show more than three times as long execution time prior to bespoke commit. (19s vs. 7s) The suggested fix brings execution time back to what it was.

Suggested fix:
Put the cap back on the execution time.
[11 Dec 2025 12:34] Martin Hansson
`git show` of patch merged to percona-server.

(*) I confirm the code being submitted is offered under the terms of the OCA, and that I am authorized to contribute it.

Contribution: submission.patch (application/octet-stream, text), 1.18 KiB.

[12 Dec 2025 8:19] Chaithra Marsur Gopala Reddy
Hi Martin,

Thank you for the test case and the patch. Verified as described.
[13 Aug 14:48] MySQL Admin
Posted by developer: Bug status updated to 'Patch pending'
[21 Oct 11:35] Knut Anders Hatlen
Bug#118144, bug#119052 and bug#119066 have been marked as duplicates of this bug. I've verified that Martin's patch improves the performance of the test cases in those bug reports too.
[21 Oct 12:06] Knut Anders Hatlen
Thanks for the patch, Martin! We plan to take it largely as is, but there is some background worth explaining.

Removing the storage engine's recommended 100-row cap on the record buffer was intentional. Buffer sizing should take the optimizer's estimates into account, and larger buffers gave a modest performance improvement for large scans.

However, LIMIT-aware buffer sizing had been lost in an earlier change for WL#14071. For your query, that meant filling a 100-row buffer even though only one row was needed. The change you identified for Bug#36775910 amplified this problem: in my tests, the buffer grew to 1596 rows.

Your patch restores the 100-row cap and substantially improves performance. For this reproducer, though, it still means reading 99 unnecessary rows.

The fuller solution is to size the buffer using the number of rows we expect to read before reaching LIMIT, accounting for filtering and joins. For your query, that would avoid allocating a record buffer altogether.

I have a work-in-progress patch for that, but it is a larger change and would require more care to backport. My plan is therefore to accept your smaller patch and consider backports to affected branches, then address LIMIT-aware sizing separately in trunk.
[26 Oct 12:13] Knut Anders Hatlen
Bug#121609 tracks the work on making the record buffer size LIMIT-aware.
[28 Oct 9:48] MySQL Admin
Posted by developer: Bug status updated to 'Patch approved'
[30 Oct 15:47] MySQL Admin
Posted by developer: Bug status updated to 'Documenting'
[1 Dec 16:41] Edward Gilmore
Posted by developer:
 
Added the following note to the MySQL Server 8.4.14, 9.7.5, and 27.1.0 release notes:

Index scans could suffer a performance regression, particularly for
queries with a small LIMIT, because record buffers exceeded the storage
engine's recommended row limit. That limit, which is 100 rows for InnoDB,
is now respected.

Our thanks to Martin Hansson and the team from Percona for
the contribution.

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.063 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.