This is similar to the following upstream bug that was fixed in MySQL 5.6, but this problem seems to effect all InnoDB tables in MariaDB 10.1, not just partitioned ones:
https://bugs.mysql.com/bug.php?id=60023
For example, run the following test:
CREATE TABLE tbl1 (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, a INT NOT NULL, KEY(a)) ENGINE=InnoDB;
|
INSERT INTO tbl1(a) VALUES (1), (2), (3), (4);
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
INSERT INTO tbl1(a) SELECT a FROM tbl1;
|
|
OPTIMIZE TABLE tbl1;
|
EXPLAIN SELECT DISTINCT a FROM tbl1;
|
SELECT DISTINCT a FROM tbl1;
|
EXPLAIN SELECT a FROM tbl1 GROUP BY a;
|
SELECT a FROM tbl1 GROUP BY a;
|
Here's the output seen on MariaDB 10.1.31:
MariaDB [db1]> EXPLAIN SELECT DISTINCT a FROM tbl1;
|
+------+-------------+-------+-------+---------------+------+---------+------+-------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+-------+-------------+
|
| 1 | SIMPLE | tbl1 | index | NULL | a | 4 | NULL | 65895 | Using index |
|
+------+-------------+-------+-------+---------------+------+---------+------+-------+-------------+
|
1 row in set (0.00 sec)
|
|
MariaDB [db1]> SELECT DISTINCT a FROM tbl1;
|
+---+
|
| a |
|
+---+
|
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
+---+
|
4 rows in set (0.02 sec)
|
|
MariaDB [db1]> EXPLAIN SELECT a FROM tbl1 GROUP BY a;
|
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
| 1 | SIMPLE | tbl1 | range | NULL | a | 4 | NULL | 7 | Using index for group-by |
|
+------+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
1 row in set (0.00 sec)
|
|
MariaDB [db1]> SELECT a FROM tbl1 GROUP BY a;
|
+---+
|
| a |
|
+---+
|
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
+---+
|
4 rows in set (0.00 sec)
|
Compare this to the output seen on 5.6.38:
mysql> EXPLAIN SELECT DISTINCT a FROM tbl1;
|
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
| 1 | SIMPLE | tbl1 | range | a | a | 4 | NULL | 7 | Using index for group-by |
|
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
1 row in set (0.00 sec)
|
|
mysql> SELECT DISTINCT a FROM tbl1;
|
+---+
|
| a |
|
+---+
|
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
+---+
|
4 rows in set (0.00 sec)
|
|
mysql> EXPLAIN SELECT a FROM tbl1 GROUP BY a;
|
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
| 1 | SIMPLE | tbl1 | range | a | a | 4 | NULL | 7 | Using index for group-by |
|
+----+-------------+-------+-------+---------------+------+---------+------+------+--------------------------+
|
1 row in set (0.00 sec)
|
|
mysql> SELECT a FROM tbl1 GROUP BY a;
|
+---+
|
| a |
|
+---+
|
| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
+---+
|
4 rows in set (0.00 sec)
|
- relates to
-
MDEV-10880
Assertions `keypart_map' or `prebuilt->search_tuple->n_fields > 0' fail on DISTINCT and GROUP BY constant
-
-
Closed
- links to
-
{"report":{"fcp":1063.5,"ttfb":278.7999999523163,"pageVisibility":"visible","entityId":66071,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"ea192d3d-e805-4db9-926f-8b67be34fe96","navigationType":0,"readyForUser":1158.5,"redirectCount":0,"resourceLoadedEnd":1324,"resourceLoadedStart":286,"resourceTiming":[{"duration":318.39999997615814,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":286,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":286,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":604.3999999761581,"responseStart":0,"secureConnectionStart":0},{"duration":318.10000002384186,"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":286.2999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":286.2999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":604.3999999761581,"responseStart":0,"secureConnectionStart":0},{"duration":346.39999997615814,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":286.39999997615814,"connectEnd":286.39999997615814,"connectStart":286.39999997615814,"domainLookupEnd":286.39999997615814,"domainLookupStart":286.39999997615814,"fetchStart":286.39999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":286.39999997615814,"responseEnd":632.7999999523163,"responseStart":632.7999999523163,"secureConnectionStart":286.39999997615814},{"duration":440.39999997615814,"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":286.5,"connectEnd":286.5,"connectStart":286.5,"domainLookupEnd":286.5,"domainLookupStart":286.5,"fetchStart":286.5,"redirectEnd":0,"redirectStart":0,"requestStart":286.5,"responseEnd":726.8999999761581,"responseStart":726.8999999761581,"secureConnectionStart":286.5},{"duration":444,"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":286.60000002384186,"connectEnd":286.60000002384186,"connectStart":286.60000002384186,"domainLookupEnd":286.60000002384186,"domainLookupStart":286.60000002384186,"fetchStart":286.60000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":286.60000002384186,"responseEnd":730.6000000238419,"responseStart":730.6000000238419,"secureConnectionStart":286.60000002384186},{"duration":444.60000002384186,"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":286.6999999284744,"connectEnd":286.6999999284744,"connectStart":286.6999999284744,"domainLookupEnd":286.6999999284744,"domainLookupStart":286.6999999284744,"fetchStart":286.6999999284744,"redirectEnd":0,"redirectStart":0,"requestStart":286.6999999284744,"responseEnd":731.2999999523163,"responseStart":731.2999999523163,"secureConnectionStart":286.6999999284744},{"duration":445.10000002384186,"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":286.6999999284744,"connectEnd":286.6999999284744,"connectStart":286.6999999284744,"domainLookupEnd":286.6999999284744,"domainLookupStart":286.6999999284744,"fetchStart":286.6999999284744,"redirectEnd":0,"redirectStart":0,"requestStart":286.6999999284744,"responseEnd":731.7999999523163,"responseStart":731.7999999523163,"secureConnectionStart":286.6999999284744},{"duration":445.7999999523163,"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":286.89999997615814,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":286.89999997615814,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":732.6999999284744,"responseStart":0,"secureConnectionStart":0},{"duration":445.60000002384186,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":287,"connectEnd":287,"connectStart":287,"domainLookupEnd":287,"domainLookupStart":287,"fetchStart":287,"redirectEnd":0,"redirectStart":0,"requestStart":287,"responseEnd":732.6000000238419,"responseStart":732.6000000238419,"secureConnectionStart":287},{"duration":446.09999990463257,"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":287.10000002384186,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":287.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":733.1999999284744,"responseStart":0,"secureConnectionStart":0},{"duration":446.10000002384186,"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":287.1999999284744,"connectEnd":287.1999999284744,"connectStart":287.1999999284744,"domainLookupEnd":287.1999999284744,"domainLookupStart":287.1999999284744,"fetchStart":287.1999999284744,"redirectEnd":0,"redirectStart":0,"requestStart":287.1999999284744,"responseEnd":733.2999999523163,"responseStart":733.2999999523163,"secureConnectionStart":287.1999999284744},{"duration":647.7999999523163,"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":288,"connectEnd":288,"connectStart":288,"domainLookupEnd":288,"domainLookupStart":288,"fetchStart":288,"redirectEnd":0,"redirectStart":0,"requestStart":288,"responseEnd":935.7999999523163,"responseStart":935.7999999523163,"secureConnectionStart":288},{"duration":1017.6999999284744,"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":293.10000002384186,"connectEnd":293.10000002384186,"connectStart":293.10000002384186,"domainLookupEnd":293.10000002384186,"domainLookupStart":293.10000002384186,"fetchStart":293.10000002384186,"redirectEnd":0,"redirectStart":0,"requestStart":293.10000002384186,"responseEnd":1310.7999999523163,"responseStart":1310.7999999523163,"secureConnectionStart":293.10000002384186},{"duration":150.29999995231628,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":786.3999999761581,"connectEnd":786.3999999761581,"connectStart":786.3999999761581,"domainLookupEnd":786.3999999761581,"domainLookupStart":786.3999999761581,"fetchStart":786.3999999761581,"redirectEnd":0,"redirectStart":0,"requestStart":786.3999999761581,"responseEnd":936.6999999284744,"responseStart":936.6999999284744,"secureConnectionStart":786.3999999761581},{"duration":322.39999997615814,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bu7/820016/12ta74/e65b778d185daf5aee24936755b43da6/_/download/contextbatch/js/browser-metrics-plugin.contrib,-_super,-atl.general/batch.js?agile_global_admin_condition=true&jag=true&slack-enabled=true","startTime":1001.6000000238419,"connectEnd":1001.6000000238419,"connectStart":1001.6000000238419,"domainLookupEnd":1001.6000000238419,"domainLookupStart":1001.6000000238419,"fetchStart":1001.6000000238419,"redirectEnd":0,"redirectStart":0,"requestStart":1001.6000000238419,"responseEnd":1324,"responseStart":1324,"secureConnectionStart":1001.6000000238419},{"duration":269.90000009536743,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1055.1999999284744,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1055.1999999284744,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1325.1000000238419,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":3,"responseStart":279,"responseEnd":285,"domLoading":282,"domInteractive":1365,"domContentLoadedEventStart":1365,"domContentLoadedEventEnd":1431,"domComplete":1980,"loadEventStart":1981,"loadEventEnd":1981,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1313.6999999284744},{"name":"bigPipe.sidebar-id.end","time":1314.3999999761581},{"name":"bigPipe.activity-panel-pipe-id.start","time":1314.6000000238419},{"name":"bigPipe.activity-panel-pipe-id.end","time":1321},{"name":"activityTabFullyLoaded","time":1452.7999999523163}],"measures":[],"correlationId":"83511db9d48f13","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":113,"dbReadsTimeInMs":12,"dbConnsTimeInMs":21,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
On MariaDB 10.2 it works with
On 10.1. a workarround is to combine group by and distinct.
EXPLAIN SELECT DISTINCT a FROM tbl1 GROUP BY a;