Type:
Bug
Priority:
Critical
Resolution:
Fixed
Affects Version/s:
None
How to reproduce:
CREATE TABLE `x`(
`i` INT,
`i2` INT,
PRIMARY KEY(`i`),
KEY (`i2`)
);
INSERT INTO
`x` (
SELECT
seq,
FLOOR(0 + RAND() * (1000000 +1))
FROM
seq_1_to_10000000
);
DELIMITER $$
CREATE PROCEDURE `testSp`()
MODIFIES SQL DATA
BEGIN
DECLARE `indexId` INT DEFAULT 71;
SELECT
`i2`
FROM
(
SELECT
`x`.*,
ROW_NUMBER() OVER (
PARTITION BY `i2`
ORDER BY `i` ASC
) AS `rowNumber`
FROM
`x`
) AS `iq`
WHERE
`iq`.`i2` = `indexId`
LIMIT 1;
END$$
DELIMITER ;
If you run this procedure, it takes long time and performs a table scan instead of using the pushed down condition:
MariaDB [tflt]> call testSp();
+------+
| i2 |
+------+
| 71 |
+------+
1 row in set (43.560 sec)
This is also visible using SHOW EXPLAIN:
MariaDB [(none)]> show explain for 16;
+------+-------------+------------+-------+---------------+------+---------+------+---------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+------------+-------+---------------+------+---------+------+---------+------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 9983371 | Using where |
| 2 | DERIVED | x | index | NULL | i2 | 5 | NULL | 9983371 | Using index; Using temporary |
+------+-------------+------------+-------+---------------+------+---------+------+---------+------------------------------+
2 rows in set, 1 warning (0.001 sec)
If you run the query outside the stored procedure, condition pushdown works as expected, and query returns almost immediately.
Explain from outside:
+------+-------------+------------+------+---------------+------+---------+-------+------+------------------------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+------+-------------+------------+------+---------------+------+---------+-------+------+------------------------------+
| 1 | PRIMARY | <derived2> | ALL | NULL | NULL | NULL | NULL | 16 | Using where |
| 2 | DERIVED | x | ref | i2 | i2 | 5 | const | 16 | Using index; Using temporary |
+------+-------------+------------+------+---------------+------+---------+-------+------+------------------------------+
2 rows in set (0.005 sec)
Sample execution:
SELECT
`i2`
FROM
(
SELECT
`x`.*,
ROW_NUMBER() OVER (
PARTITION BY `i2`
ORDER BY `i` ASC
) AS `rowNumber`
FROM
`x`
) AS `iq`
WHERE
`iq`.`i2` = 71 LIMIT 1;
+------+
| i2 |
+------+
| 71 |
+------+
1 row in set (0.001 sec)
It has been noted that the problem seems tied to the use of a local variable in the stored procedure. Using a constant or a session variable does not exhibit the problem.
{"report":{"fcp":1044.9000000059605,"ttfb":311.29999999701977,"pageVisibility":"visible","entityId":128978,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"434f394f-0b11-4820-9ede-0a84979700c0","navigationType":0,"readyForUser":1143.2000000029802,"redirectCount":0,"resourceLoadedEnd":770.0999999940395,"resourceLoadedStart":321.70000000298023,"resourceTiming":[{"duration":136.09999999403954,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":321.70000000298023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":321.70000000298023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":457.79999999701977,"responseStart":0,"secureConnectionStart":0},{"duration":136.09999999403954,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/download/contextbatch/css/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":322,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":322,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":458.09999999403954,"responseStart":0,"secureConnectionStart":0},{"duration":319.09999999403954,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":322.20000000298023,"connectEnd":322.20000000298023,"connectStart":322.20000000298023,"domainLookupEnd":322.20000000298023,"domainLookupStart":322.20000000298023,"fetchStart":322.20000000298023,"redirectEnd":0,"redirectStart":0,"requestStart":460.90000000596046,"responseEnd":641.2999999970198,"responseStart":488.40000000596046,"secureConnectionStart":322.20000000298023},{"duration":446.69999998807907,"initiatorType":"script","name":"https://jira.mariadb.org/s/c32eb0da7ad9831253f8397e6cc26afd-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/download/contextbatch/js/jira.browse.project,project.issue.navigator,jira.view.issue,jira.general,jira.global,atl.general,-_super/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":323.40000000596046,"connectEnd":323.40000000596046,"connectStart":323.40000000596046,"domainLookupEnd":323.40000000596046,"domainLookupStart":323.40000000596046,"fetchStart":323.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":461.70000000298023,"responseEnd":770.0999999940395,"responseStart":511.5,"secureConnectionStart":323.40000000596046},{"duration":171.09999999403954,"initiatorType":"script","name":"https://jira.mariadb.org/s/bc0bcb146314416123c992714ee00ff7-CDN/lu2bv2/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":323.70000000298023,"connectEnd":323.70000000298023,"connectStart":323.70000000298023,"domainLookupEnd":323.70000000298023,"domainLookupStart":323.70000000298023,"fetchStart":323.70000000298023,"redirectEnd":0,"redirectStart":0,"requestStart":464.40000000596046,"responseEnd":494.79999999701977,"responseStart":492.90000000596046,"secureConnectionStart":323.70000000298023},{"duration":171.09999999403954,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":323.90000000596046,"connectEnd":323.90000000596046,"connectStart":323.90000000596046,"domainLookupEnd":323.90000000596046,"domainLookupStart":323.90000000596046,"fetchStart":323.90000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":465.20000000298023,"responseEnd":495,"responseStart":493.79999999701977,"secureConnectionStart":323.90000000596046},{"duration":176.20000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":324.09999999403954,"connectEnd":324.09999999403954,"connectStart":324.09999999403954,"domainLookupEnd":324.09999999403954,"domainLookupStart":324.09999999403954,"fetchStart":324.09999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":465.40000000596046,"responseEnd":500.29999999701977,"responseStart":497.29999999701977,"secureConnectionStart":324.09999999403954},{"duration":139.90000000596046,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bv2/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":324.29999999701977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":324.29999999701977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":464.20000000298023,"responseStart":0,"secureConnectionStart":0},{"duration":176.09999999403954,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":324.40000000596046,"connectEnd":324.40000000596046,"connectStart":324.40000000596046,"domainLookupEnd":324.40000000596046,"domainLookupStart":324.40000000596046,"fetchStart":324.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":469,"responseEnd":500.5,"responseStart":498,"secureConnectionStart":324.40000000596046},{"duration":140.40000000596046,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bv2/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/css/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.css?jira.create.linked.issue=true","startTime":324.59999999403954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":324.59999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":465,"responseStart":0,"secureConnectionStart":0},{"duration":188.70000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/s/719848dd97ebe0663199f49a3936487a-CDN/lu2bv2/820016/12ta74/d176f0986478cc64f24226b3d20c140d/_/download/contextbatch/js/com.atlassian.jira.projects.sidebar.init,-_super,-project.issue.navigator,-jira.view.issue/batch.js?jira.create.linked.issue=true&locale=en","startTime":324.79999999701977,"connectEnd":324.79999999701977,"connectStart":324.79999999701977,"domainLookupEnd":324.79999999701977,"domainLookupStart":324.79999999701977,"fetchStart":324.79999999701977,"redirectEnd":0,"redirectStart":0,"requestStart":470.09999999403954,"responseEnd":513.5,"responseStart":498.90000000596046,"secureConnectionStart":324.79999999701977},{"duration":374.40000000596046,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":325.5,"connectEnd":325.5,"connectStart":325.5,"domainLookupEnd":325.5,"domainLookupStart":325.5,"fetchStart":325.5,"redirectEnd":0,"redirectStart":0,"requestStart":512.5,"responseEnd":699.9000000059605,"responseStart":695.2000000029802,"secureConnectionStart":325.5},{"duration":375.20000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":325.70000000298023,"connectEnd":325.70000000298023,"connectStart":325.70000000298023,"domainLookupEnd":325.70000000298023,"domainLookupStart":325.70000000298023,"fetchStart":325.70000000298023,"redirectEnd":0,"redirectStart":0,"requestStart":609,"responseEnd":700.9000000059605,"responseStart":697.7000000029802,"secureConnectionStart":325.70000000298023},{"duration":169.09999999403954,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":791.5,"connectEnd":791.5,"connectStart":791.5,"domainLookupEnd":791.5,"domainLookupStart":791.5,"fetchStart":791.5,"redirectEnd":0,"redirectStart":0,"requestStart":931,"responseEnd":960.5999999940395,"responseStart":959.9000000059605,"secureConnectionStart":791.5}],"fetchStart":0,"domainLookupStart":102,"domainLookupEnd":108,"connectStart":108,"connectEnd":129,"secureConnectionStart":117,"requestStart":130,"responseStart":311,"responseEnd":318,"domLoading":315,"domInteractive":1208,"domContentLoadedEventStart":1208,"domContentLoadedEventEnd":1255,"domComplete":2212,"loadEventStart":2212,"loadEventEnd":2212,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1188.2000000029802},{"name":"bigPipe.sidebar-id.end","time":1189},{"name":"bigPipe.activity-panel-pipe-id.start","time":1189.2999999970198},{"name":"bigPipe.activity-panel-pipe-id.end","time":1190.2000000029802},{"name":"activityTabFullyLoaded","time":1267.7000000029802}],"measures":[],"correlationId":"d0a9c59121f299","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":100,"dbReadsTimeInMs":9,"dbConnsTimeInMs":16,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
A fix for this bug was pushed into 10.5. It has to be merged upstream as it is.