Create the test dataset
--source include/have_innodb.inc
|
create table t1(a int) engine=innodb;
|
insert into t1 values (0),(1),(2),(3),(4),(5),(6),(7),(8),(9);
|
|
create table t2(
|
id int primary key,
|
key1 int,
|
col1 int,
|
key(key1)
|
) engine=innodb;
|
|
insert into t2
|
select
|
A.a + B.a*10 + C.a*100 + D.a* 1000,
|
A.a + 10*B.a,
|
123456
|
from t1 A, t1 B, t1 C, t1 D;
|
|
alter table t2 add key2 int;
|
update t2 set key2=key1;
|
alter table t2 add key(key2);
|
analyze table t2;
|
flush tables;
|
|
Then run the query
explain select
|
(SELECT
|
concat(id, '-', key1, '-', col1)
|
FROM t2
|
WHERE t2.key1 = t1.a
|
ORDER BY t2.key2 ASC LIMIT 1)
|
from
|
t1;
|
The query plan is
+------+--------------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
|
| 2 | DEPENDENT SUBQUERY | t2 | ref | key1 | key1 | 5 | test.t1.a | 49 | Using where; Using filesort |
|
+------+--------------------+-------+------+---------------+------+---------+-----------+------+-----------------------------+
|
Now I run the query
explain select
|
(SELECT
|
concat(id, '-', key1, '-', col1)
|
FROM t2
|
WHERE t2.key1 = t1.a and t2.key1 is NOT NULL
|
ORDER BY t2.key2 ASC LIMIT 1)
|
from t1;
|
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 10 | |
|
| 2 | DEPENDENT SUBQUERY | t2 | index | key1 | key2 | 5 | NULL | 2 | Using where |
|
+------+--------------------+-------+-------+---------------+------+---------+------+------+-------------+
|
As I see none of the elements for key1 is NULL, so the second query should behave exactly as the first one but it doesn't.
{"report":{"fcp":746.3999999761581,"ttfb":153.29999995231628,"pageVisibility":"visible","entityId":67627,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":1,"journeyId":"2719faa8-fe69-45f5-891d-f046c9910848","navigationType":0,"readyForUser":809.3999999761581,"redirectCount":0,"resourceLoadedEnd":917.7999999523163,"resourceLoadedStart":160.39999997615814,"resourceTiming":[{"duration":151,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":160.39999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":160.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":311.39999997615814,"responseStart":0,"secureConnectionStart":0},{"duration":150.69999992847443,"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":160.70000004768372,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":160.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":311.39999997615814,"responseStart":0,"secureConnectionStart":0},{"duration":159.70000004768372,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":160.79999995231628,"connectEnd":160.79999995231628,"connectStart":160.79999995231628,"domainLookupEnd":160.79999995231628,"domainLookupStart":160.79999995231628,"fetchStart":160.79999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":160.79999995231628,"responseEnd":320.5,"responseStart":320.5,"secureConnectionStart":160.79999995231628},{"duration":184.10000002384186,"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":160.89999997615814,"connectEnd":160.89999997615814,"connectStart":160.89999997615814,"domainLookupEnd":160.89999997615814,"domainLookupStart":160.89999997615814,"fetchStart":160.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":160.89999997615814,"responseEnd":345,"responseStart":345,"secureConnectionStart":160.89999997615814},{"duration":187.39999997615814,"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":161,"connectEnd":161,"connectStart":161,"domainLookupEnd":161,"domainLookupStart":161,"fetchStart":161,"redirectEnd":0,"redirectStart":0,"requestStart":161,"responseEnd":348.39999997615814,"responseStart":348.39999997615814,"secureConnectionStart":161},{"duration":187.79999995231628,"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":161.10000002384186,"connectEnd":161.10000002384186,"connectStart":161.10000002384186,"domainLookupEnd":161.10000002384186,"domainLookupStart":161.10000002384186,"fetchStart":161.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":161.10000002384186,"responseEnd":348.89999997615814,"responseStart":348.89999997615814,"secureConnectionStart":161.10000002384186},{"duration":188.70000004768372,"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":161.29999995231628,"connectEnd":161.29999995231628,"connectStart":161.29999995231628,"domainLookupEnd":161.29999995231628,"domainLookupStart":161.29999995231628,"fetchStart":161.29999995231628,"redirectEnd":0,"redirectStart":0,"requestStart":161.29999995231628,"responseEnd":350,"responseStart":350,"secureConnectionStart":161.29999995231628},{"duration":190.39999997615814,"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":161.39999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":161.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":351.7999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":190.5,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":161.39999997615814,"connectEnd":161.39999997615814,"connectStart":161.39999997615814,"domainLookupEnd":161.39999997615814,"domainLookupStart":161.39999997615814,"fetchStart":161.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":161.39999997615814,"responseEnd":351.89999997615814,"responseStart":351.89999997615814,"secureConnectionStart":161.39999997615814},{"duration":190.79999995231628,"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":161.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":161.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":352.2999999523163,"responseStart":0,"secureConnectionStart":0},{"duration":190.69999992847443,"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":161.70000004768372,"connectEnd":161.70000004768372,"connectStart":161.70000004768372,"domainLookupEnd":161.70000004768372,"domainLookupStart":161.70000004768372,"fetchStart":161.70000004768372,"redirectEnd":0,"redirectStart":0,"requestStart":161.70000004768372,"responseEnd":352.39999997615814,"responseStart":352.39999997615814,"secureConnectionStart":161.70000004768372},{"duration":406.2999999523163,"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":162.5,"connectEnd":162.5,"connectStart":162.5,"domainLookupEnd":162.5,"domainLookupStart":162.5,"fetchStart":162.5,"redirectEnd":0,"redirectStart":0,"requestStart":162.5,"responseEnd":568.7999999523163,"responseStart":568.7999999523163,"secureConnectionStart":162.5},{"duration":755.1999999284744,"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":162.60000002384186,"connectEnd":162.60000002384186,"connectStart":162.60000002384186,"domainLookupEnd":162.60000002384186,"domainLookupStart":162.60000002384186,"fetchStart":162.60000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":162.60000002384186,"responseEnd":917.7999999523163,"responseStart":917.7999999523163,"secureConnectionStart":162.60000002384186},{"duration":367.10000002384186,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":550,"connectEnd":550,"connectStart":550,"domainLookupEnd":550,"domainLookupStart":550,"fetchStart":550,"redirectEnd":0,"redirectStart":0,"requestStart":550,"responseEnd":917.1000000238419,"responseStart":917.1000000238419,"secureConnectionStart":550},{"duration":196,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":724.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":724.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":920.2000000476837,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":14,"responseStart":153,"responseEnd":158,"domLoading":157,"domInteractive":942,"domContentLoadedEventStart":942,"domContentLoadedEventEnd":977,"domComplete":1479,"loadEventStart":1479,"loadEventEnd":1480,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":923.8999999761581},{"name":"bigPipe.sidebar-id.end","time":924.7000000476837},{"name":"bigPipe.activity-panel-pipe-id.start","time":924.8999999761581},{"name":"bigPipe.activity-panel-pipe-id.end","time":927.1000000238419},{"name":"activityTabFullyLoaded","time":982.8999999761581}],"measures":[],"correlationId":"80395e5ba1b646","effectiveType":"4g","downlink":9.4,"rtt":0,"serverDuration":82,"dbReadsTimeInMs":13,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}