Wrong query results when optimizer_switch="split_materialized=on" optimization is enabled.
Tested with MariaDB 10.3.22, 10.4.10, 10.4.11, 10.4.12.
Works correctly with 10.2.31 (no optimization)
Example
MySQL:
CREATE DATABASE test;
|
CREATE TABLE test.a (id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, name varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL);
|
CREATE TABLE test.b (id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, related_object_id int(11) NOT NULL, is_default int(1) NOT NULL DEFAULT 0, KEY related_object_id (related_object_id) );
|
Shell:
for i in {1..1000}; do echo "INSERT INTO test.a(name) VALUES('name$i');"|mysql;done
|
for i in {1..1000}; do echo "INSERT INTO test.b(related_object_id, is_default) VALUES($i,0);"|mysql;done
|
for i in {1..1000}; do echo "INSERT INTO test.b(related_object_id, is_default) VALUES($i,1);"|mysql;done
|
Query:
SELECT
|
a.id AS id,
|
a.name AS name
|
FROM
|
a AS a
|
WHERE
|
a.id IN
|
(
|
SELECT
|
subp.id
|
FROM
|
(
|
SELECT
|
a.*
|
FROM
|
a a
|
LEFT JOIN
|
b AS b
|
ON (a.id = b.related_object_id AND b.is_default = 1)
|
GROUP BY
|
a.id
|
)
|
AS subp
|
)
|
|
Result:
+----+-------+
|
| id | name |
|
+----+-------+
|
| 1 | name1 |
|
+----+-------+
|
1 row in set (0.00 sec)
|
Query plan:
MariaDB [test]> explain extended SELECT
|
-> a.id AS id,
|
-> a.name AS name
|
-> FROM
|
-> a AS a
|
-> WHERE
|
-> a.id IN
|
-> (
|
-> SELECT
|
-> subp.id
|
-> FROM
|
-> (
|
-> SELECT
|
-> a.*
|
-> FROM
|
-> a a
|
-> LEFT JOIN
|
-> b AS b
|
-> ON (a.id = b.related_object_id AND b.is_default = 1)
|
-> GROUP BY
|
-> a.id
|
-> )
|
-> AS subp
|
-> );
|
+------+-----------------+-------------+--------+-------------------+-------------------+---------+-----------+------+----------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+-----------------+-------------+--------+-------------------+-------------------+---------+-----------+------+----------+-------------+
|
| 1 | PRIMARY | a | ALL | PRIMARY | NULL | NULL | NULL | 1000 | 100.00 | |
|
| 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | 100.00 | |
|
| 2 | MATERIALIZED | <derived3> | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | |
|
| 3 | LATERAL DERIVED | a | eq_ref | PRIMARY | PRIMARY | 4 | test.a.id | 1 | 100.00 | |
|
| 3 | LATERAL DERIVED | b | ref | related_object_id | related_object_id | 4 | test.a.id | 1 | 100.00 | Using where |
|
+------+-----------------+-------------+--------+-------------------+-------------------+---------+-----------+------+----------+-------------+
|
5 rows in set, 1 warning (0.00 sec)
|
|
Turn split_materialized optimization
SET optimizer_switch="split_materialized=off";
|
Result:
| 991 | name991 |
|
| 992 | name992 |
|
| 993 | name993 |
|
| 994 | name994 |
|
| 995 | name995 |
|
| 996 | name996 |
|
| 997 | name997 |
|
| 998 | name998 |
|
| 999 | name999 |
|
| 1000 | name1000 |
|
+------+----------+
|
1000 rows in set (0.00 sec)
|
Query plan:
MariaDB [test]> explain extended SELECT
|
-> a.id AS id,
|
-> a.name AS name
|
-> FROM
|
-> a AS a
|
-> WHERE
|
-> a.id IN
|
-> (
|
-> SELECT
|
-> subp.id
|
-> FROM
|
-> (
|
-> SELECT
|
-> a.*
|
-> FROM
|
-> a a
|
-> LEFT JOIN
|
-> b AS b
|
-> ON (a.id = b.related_object_id AND b.is_default = 1)
|
-> GROUP BY
|
-> a.id
|
-> )
|
-> AS subp
|
-> );
|
+------+--------------+-------------+--------+-------------------+-------------------+---------+-----------+------+----------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | filtered | Extra |
|
+------+--------------+-------------+--------+-------------------+-------------------+---------+-----------+------+----------+-------------+
|
| 1 | PRIMARY | a | ALL | PRIMARY | NULL | NULL | NULL | 1000 | 100.00 | |
|
| 1 | PRIMARY | <subquery2> | eq_ref | distinct_key | distinct_key | 4 | func | 1 | 100.00 | |
|
| 2 | MATERIALIZED | <derived3> | ALL | NULL | NULL | NULL | NULL | 1000 | 100.00 | |
|
| 3 | DERIVED | a | index | NULL | PRIMARY | 4 | NULL | 1000 | 100.00 | |
|
| 3 | DERIVED | b | ref | related_object_id | related_object_id | 4 | test.a.id | 1 | 100.00 | Using where |
|
+------+--------------+-------------+--------+-------------------+-------------------+---------+-----------+------+----------+-------------+
|
5 rows in set, 1 warning (0.00 sec)
|
{"report":{"fcp":801.4000000953674,"ttfb":250.09999990463257,"pageVisibility":"visible","entityId":82837,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":1,"journeyId":"ba2187b4-172f-4a52-8a55-93913d03fc9e","navigationType":0,"readyForUser":910.2000000476837,"redirectCount":0,"resourceLoadedEnd":979.5999999046326,"resourceLoadedStart":267.5,"resourceTiming":[{"duration":12.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":267.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":267.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":280,"responseStart":0,"secureConnectionStart":0},{"duration":17.100000143051147,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/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":267.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":267.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":284.90000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":67.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":268,"connectEnd":268,"connectStart":268,"domainLookupEnd":268,"domainLookupStart":268,"fetchStart":268,"redirectEnd":0,"redirectStart":0,"requestStart":268,"responseEnd":335.40000009536743,"responseStart":335.40000009536743,"secureConnectionStart":268},{"duration":176.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/099b33461394b8015fc36c0a4b96e19f-CDN/lu2bu7/820016/12ta74/8679b4946efa1a0bb029a3a22206fb5d/_/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":268,"connectEnd":268,"connectStart":268,"domainLookupEnd":268,"domainLookupStart":268,"fetchStart":268,"redirectEnd":0,"redirectStart":0,"requestStart":268,"responseEnd":444.09999990463257,"responseStart":444.09999990463257,"secureConnectionStart":268},{"duration":180.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/94c15bff32baef80f4096a08aceae8bc-CDN/lu2bu7/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":268.2000000476837,"connectEnd":268.2000000476837,"connectStart":268.2000000476837,"domainLookupEnd":268.2000000476837,"domainLookupStart":268.2000000476837,"fetchStart":268.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":268.2000000476837,"responseEnd":448.2999999523163,"responseStart":448.2999999523163,"secureConnectionStart":268.2000000476837},{"duration":180.70000004768372,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":268.2000000476837,"connectEnd":268.2000000476837,"connectStart":268.2000000476837,"domainLookupEnd":268.2000000476837,"domainLookupStart":268.2000000476837,"fetchStart":268.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":268.2000000476837,"responseEnd":448.90000009536743,"responseStart":448.90000009536743,"secureConnectionStart":268.2000000476837},{"duration":180.89999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":268.40000009536743,"connectEnd":268.40000009536743,"connectStart":268.40000009536743,"domainLookupEnd":268.40000009536743,"domainLookupStart":268.40000009536743,"fetchStart":268.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":268.40000009536743,"responseEnd":449.2999999523163,"responseStart":449.2999999523163,"secureConnectionStart":268.40000009536743},{"duration":181.20000004768372,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bu7/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":268.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":268.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":449.7000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":181,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":268.7000000476837,"connectEnd":268.7000000476837,"connectStart":268.7000000476837,"domainLookupEnd":268.7000000476837,"domainLookupStart":268.7000000476837,"fetchStart":268.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":268.7000000476837,"responseEnd":449.7000000476837,"responseStart":449.7000000476837,"secureConnectionStart":268.7000000476837},{"duration":181.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bu7/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":268.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":268.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":450.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":181.39999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/3339d87fa2538a859872f2df449bf8d0-CDN/lu2bu7/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":268.90000009536743,"connectEnd":268.90000009536743,"connectStart":268.90000009536743,"domainLookupEnd":268.90000009536743,"domainLookupStart":268.90000009536743,"fetchStart":268.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":268.90000009536743,"responseEnd":450.2999999523163,"responseStart":450.2999999523163,"secureConnectionStart":268.90000009536743},{"duration":381.89999985694885,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":269.7000000476837,"connectEnd":269.7000000476837,"connectStart":269.7000000476837,"domainLookupEnd":269.7000000476837,"domainLookupStart":269.7000000476837,"fetchStart":269.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":269.7000000476837,"responseEnd":651.5999999046326,"responseStart":651.5999999046326,"secureConnectionStart":269.7000000476837},{"duration":498.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":269.7999999523163,"connectEnd":269.7999999523163,"connectStart":269.7999999523163,"domainLookupEnd":269.7999999523163,"domainLookupStart":269.7999999523163,"fetchStart":269.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":269.7999999523163,"responseEnd":768.2999999523163,"responseStart":768.2999999523163,"secureConnectionStart":269.7999999523163},{"duration":164.79999995231628,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":487.2000000476837,"connectEnd":487.2000000476837,"connectStart":487.2000000476837,"domainLookupEnd":487.2000000476837,"domainLookupStart":487.2000000476837,"fetchStart":487.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":487.2000000476837,"responseEnd":652,"responseStart":652,"secureConnectionStart":487.2000000476837},{"duration":107,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2bu7/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/css/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.css?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":723.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":723.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":830.2000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":251.10000014305115,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-project.issue.navigator,-jira.view.issue,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&slack-enabled=true","startTime":724.0999999046326,"connectEnd":724.0999999046326,"connectStart":724.0999999046326,"domainLookupEnd":724.0999999046326,"domainLookupStart":724.0999999046326,"fetchStart":724.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":724.0999999046326,"responseEnd":975.2000000476837,"responseStart":975.0999999046326,"secureConnectionStart":724.0999999046326},{"duration":255.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/f51ef5507eea4c158f257c66c93b2a3f-CDN/lu2bu7/820016/12ta74/be4b45e9cec53099498fa61c8b7acba4/_/download/contextbatch/js/jira.project.sidebar,-_super,-project.issue.navigator,-jira.general,-jira.browse.project,-jira.view.issue,-jira.global,-atl.general,-com.atlassian.jira.projects.sidebar.init/batch.js?agile_global_admin_condition=true&jag=true&jira.create.linked.issue=true&locale=en&slack-enabled=true","startTime":724.5,"connectEnd":724.5,"connectStart":724.5,"domainLookupEnd":724.5,"domainLookupStart":724.5,"fetchStart":724.5,"redirectEnd":0,"redirectStart":0,"requestStart":724.5,"responseEnd":979.5999999046326,"responseStart":979.5999999046326,"secureConnectionStart":724.5},{"duration":186.79999995231628,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":794.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":794.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":981,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":81,"responseStart":250,"responseEnd":254,"domLoading":264,"domInteractive":991,"domContentLoadedEventStart":991,"domContentLoadedEventEnd":1051,"domComplete":2041,"loadEventStart":2041,"loadEventEnd":2043,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":966.7000000476837},{"name":"bigPipe.sidebar-id.end","time":967.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.start","time":967.7000000476837},{"name":"bigPipe.activity-panel-pipe-id.end","time":970.0999999046326},{"name":"activityTabFullyLoaded","time":1065.2999999523163}],"measures":[],"correlationId":"1692a81fb62708","effectiveType":"4g","downlink":9.9,"rtt":0,"serverDuration":87,"dbReadsTimeInMs":13,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}