MIN/MAX Optimization (Select tables optimized away) does not work for DateTime.
This is similar to bug #3855:
https://mariadb.atlassian.net/browse/MDEV-3855
However, that bug is specifically about inet_aton.
This one is regarding datetime values, and they are not optimized away, thus the query can take much longer than expected.
MariaDB:
mysql> EXPLAIN SELECT MIN(b) FROM t1 WHERE b <= '2013-11-06 23:59:59';
|
+------+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
|
| 1 | SIMPLE | t1 | range | idx_b | idx_b | 9 | NULL | 11 | Using where; Using index |
|
+------+-------------+-------+-------+---------------+-------+---------+------+------+--------------------------+
|
MySQL:
mysql> EXPLAIN SELECT MIN(b) FROM t1 WHERE b <= '2013-11-06 23:59:59';
|
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
|
| 1 | SIMPLE | NULL | NULL | NULL | NULL | NULL | NULL | NULL | Select tables optimized away |
|
+----+-------------+-------+------+---------------+------+---------+------+------+------------------------------+
|
The following illustrates this:
CREATE TABLE `t1` (
|
`a` int(11) NOT NULL AUTO_INCREMENT,
|
`b` datetime DEFAULT NULL,
|
PRIMARY KEY (`a`),
|
KEY `idx_b` (`b`)
|
) ENGINE=InnoDB;
|
|
INSERT INTO `t1` (b) VALUES ('2013-01-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-02-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-03-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-04-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-05-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-06-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-07-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-08-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-09-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-10-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-11-06 23:59:59');
|
INSERT INTO `t1` (b) VALUES ('2013-12-06 23:59:59');
|
|
EXPLAIN SELECT MIN(b) FROM t1 WHERE b <= '2013-11-06 23:59:59';
|
{"report":{"fcp":971.6999998092651,"ttfb":221.7999997138977,"pageVisibility":"visible","entityId":26330,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"aafe0ba1-1742-44c5-8579-8d8fafb11a62","navigationType":0,"readyForUser":1057.7999997138977,"redirectCount":0,"resourceLoadedEnd":1279.5,"resourceLoadedStart":227.19999980926514,"resourceTiming":[{"duration":265,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":227.19999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":227.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":492.19999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":265.19999980926514,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/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&whisper-enabled=true","startTime":227.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":227.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":492.59999990463257,"responseStart":0,"secureConnectionStart":0},{"duration":274.30000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":227.59999990463257,"connectEnd":227.59999990463257,"connectStart":227.59999990463257,"domainLookupEnd":227.59999990463257,"domainLookupStart":227.59999990463257,"fetchStart":227.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":227.59999990463257,"responseEnd":501.90000009536743,"responseStart":501.90000009536743,"secureConnectionStart":227.59999990463257},{"duration":326.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/494e4c556ecbb29f90a3d3b4f09cb99c/_/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&whisper-enabled=true","startTime":227.7999997138977,"connectEnd":227.7999997138977,"connectStart":227.7999997138977,"domainLookupEnd":227.7999997138977,"domainLookupStart":227.7999997138977,"fetchStart":227.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":227.7999997138977,"responseEnd":554.1999998092651,"responseStart":554.1999998092651,"secureConnectionStart":227.7999997138977},{"duration":330.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/a9324d6758d385eb45c462685ad88f1d-CDN/lu2cib/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":228,"connectEnd":228,"connectStart":228,"domainLookupEnd":228,"domainLookupStart":228,"fetchStart":228,"redirectEnd":0,"redirectStart":0,"requestStart":228,"responseEnd":558.1999998092651,"responseStart":558.1999998092651,"secureConnectionStart":228},{"duration":330.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":228.19999980926514,"connectEnd":228.19999980926514,"connectStart":228.19999980926514,"domainLookupEnd":228.19999980926514,"domainLookupStart":228.19999980926514,"fetchStart":228.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":228.19999980926514,"responseEnd":558.7999997138977,"responseStart":558.7999997138977,"secureConnectionStart":228.19999980926514},{"duration":330.69999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":228.40000009536743,"connectEnd":228.40000009536743,"connectStart":228.40000009536743,"domainLookupEnd":228.40000009536743,"domainLookupStart":228.40000009536743,"fetchStart":228.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":228.40000009536743,"responseEnd":559.0999999046326,"responseStart":559.0999999046326,"secureConnectionStart":228.40000009536743},{"duration":419.09999990463257,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2cib/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":228.59999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":228.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":647.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":330.90000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":228.69999980926514,"connectEnd":228.69999980926514,"connectStart":228.69999980926514,"domainLookupEnd":228.69999980926514,"domainLookupStart":228.69999980926514,"fetchStart":228.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":228.69999980926514,"responseEnd":559.5999999046326,"responseStart":559.5999999046326,"secureConnectionStart":228.69999980926514},{"duration":418.8999996185303,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2cib/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":228.90000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":228.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":647.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":331.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/5d5e8fe91fbc506585e83ea3b62ccc4b-CDN/lu2cib/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":229,"connectEnd":229,"connectStart":229,"domainLookupEnd":229,"domainLookupStart":229,"fetchStart":229,"redirectEnd":0,"redirectStart":0,"requestStart":229,"responseEnd":560.0999999046326,"responseStart":560.0999999046326,"secureConnectionStart":229},{"duration":537.3000001907349,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":234.7999997138977,"connectEnd":234.7999997138977,"connectStart":234.7999997138977,"domainLookupEnd":234.7999997138977,"domainLookupStart":234.7999997138977,"fetchStart":234.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":234.7999997138977,"responseEnd":772.0999999046326,"responseStart":772.0999999046326,"secureConnectionStart":234.7999997138977},{"duration":1014.9000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":235,"connectEnd":235,"connectStart":235,"domainLookupEnd":235,"domainLookupStart":235,"fetchStart":235,"redirectEnd":0,"redirectStart":0,"requestStart":235,"responseEnd":1249.9000000953674,"responseStart":1249.9000000953674,"secureConnectionStart":235},{"duration":127.90000009536743,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":676.1999998092651,"connectEnd":676.1999998092651,"connectStart":676.1999998092651,"domainLookupEnd":676.1999998092651,"domainLookupStart":676.1999998092651,"fetchStart":676.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":676.1999998092651,"responseEnd":804.0999999046326,"responseStart":804.0999999046326,"secureConnectionStart":676.1999998092651},{"duration":356.7000002861023,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/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&whisper-enabled=true","startTime":922.7999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":922.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1279.5,"responseStart":0,"secureConnectionStart":0},{"duration":336.2000002861023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/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&whisper-enabled=true","startTime":923.7999997138977,"connectEnd":923.7999997138977,"connectStart":923.7999997138977,"domainLookupEnd":923.7999997138977,"domainLookupStart":923.7999997138977,"fetchStart":923.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":923.7999997138977,"responseEnd":1260,"responseStart":1260,"secureConnectionStart":923.7999997138977},{"duration":342.30000019073486,"initiatorType":"script","name":"https://jira.mariadb.org/s/097ae97cb8fbec7d6ea4bbb1f26955b9-CDN/lu2cib/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&whisper-enabled=true","startTime":924.1999998092651,"connectEnd":924.1999998092651,"connectStart":924.1999998092651,"domainLookupEnd":924.1999998092651,"domainLookupStart":924.1999998092651,"fetchStart":924.1999998092651,"redirectEnd":0,"redirectStart":0,"requestStart":924.1999998092651,"responseEnd":1266.5,"responseStart":1266.5,"secureConnectionStart":924.1999998092651},{"duration":318.1000003814697,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":965.2999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":965.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1283.4000000953674,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":61,"responseStart":221,"responseEnd":234,"domLoading":225,"domInteractive":1316,"domContentLoadedEventStart":1316,"domContentLoadedEventEnd":1362,"domComplete":1618,"loadEventStart":1618,"loadEventEnd":1618,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1286.6999998092651},{"name":"bigPipe.sidebar-id.end","time":1287.5999999046326},{"name":"bigPipe.activity-panel-pipe-id.start","time":1287.7999997138977},{"name":"bigPipe.activity-panel-pipe-id.end","time":1289.1999998092651},{"name":"activityTabFullyLoaded","time":1379.5999999046326}],"measures":[],"correlationId":"72a7a6eaa9397d","effectiveType":"4g","downlink":9.4,"rtt":0,"serverDuration":96,"dbReadsTimeInMs":11,"dbConnsTimeInMs":19,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
To reproduce with MTR, SET NAMES utf8 should be added to the test.