As of c10e10c6, opt_sum.cc is still stubbed for DESC indexes
if (part->key_part_flag & HA_REVERSE_SORT)
break; // TODO MDEV-13756
Given that we are about to close MDEV-13756, it looks strange: either it was forgotten and needs to be enabled, or maybe it requires a better reference.
Given the above, the optimization expectedly doesn't work.
--source include/have_sequence.inc
createorreplacetable t1 (id int, key(id));
insertinto t1 select seq from seq_1_to_100 orderby rand(1);
explain extended selectmax(id) from t1 where id > 50;
createorreplacetable t1 (id int, key(id desc));
insertinto t1 select seq from seq_1_to_100 orderby rand(1);
explain extended selectmax(id) from t1 where id > 50;
MDEV-27576 Use reverse index for max/min optimization
We use a bool to indicate that the key part used is a descending
index, which will flip the functions and flags used in
get_index_max_value() and get_index_min_value(), that allows correct
optimization for max/min for descending index.
Yuchen Pei
added a comment - Thanks for the comments psergei . I addressed them and updated my commit. ptal thanks (I understand the fixversion is 11.5 so no rush):
upstream/bb-11.3-mdev-27576 8398e5c153320efbbeeeeac9d9029e0f932aaaa2
MDEV-27576 Use reverse index for max/min optimization
We use a bool to indicate that the key part used is a descending
index, which will flip the functions and flags used in
get_index_max_value() and get_index_min_value(), that allows correct
optimization for max/min for descending index.
MDEV-27576 Use reverse index for max/min optimization
We use a bool to indicate that the key part used is a descending
index, which will flip the functions and flags used in
get_index_max_value() and get_index_min_value(), that allows correct
optimization for max/min for descending index.
BTW, is this still targeted at 11.5?
Yuchen Pei
added a comment - - edited Thanks for the review psergei . Applied the formatting patch and
pushed to branch bb-11.4-mdev-27576-preview for testing:
upstream/bb-11.4-mdev-27576-preview 41566ac3896c6a6a01df10062b2fc29df3f84f86
MDEV-27576 Use reverse index for max/min optimization
We use a bool to indicate that the key part used is a descending
index, which will flip the functions and flags used in
get_index_max_value() and get_index_min_value(), that allows correct
optimization for max/min for descending index.
BTW, is this still targeted at 11.5?
But there are some notes about optimization strategies used by the query optimizer when there is ascending/descending index in the table. These moments were discussed with psergei and at this time they do not require improvement (because there is a task for improvement, or there is no need for improvement), but I think it is necessary keep this information.
For the table:
CREATETABLE t1
(a VARCHAR(10),
b VARCHAR(10),
PRIMARYKEY (a DESC, b DESC),
KEY ab_asc (a ASC, b ASC),
KEY a_asc_b_desc (a ASC, b DESC),
key a_desc_b_asc (a DESC, b ASC))
ENGINE = InnoDB;
The following query plans are:
EXPLAIN SELECTmax(a) FROM t1;
id select_type table type possible_keys key key_len ref rows Extra
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 indexNULL ab_asc 24 NULL 13 Using index
Thus second query:
is using index, but in this case the query will return only one result and the "group by" can be ignored
optimizer uses index "ab_asc" instead of Primary key with descending order
Lena Startseva
added a comment - Testing done. Ok to push.
But there are some notes about optimization strategies used by the query optimizer when there is ascending/descending index in the table. These moments were discussed with psergei and at this time they do not require improvement (because there is a task for improvement, or there is no need for improvement), but I think it is necessary keep this information.
For the table:
CREATE TABLE t1
(a VARCHAR (10),
b VARCHAR (10),
PRIMARY KEY (a DESC , b DESC ),
KEY ab_asc (a ASC , b ASC ),
KEY a_asc_b_desc (a ASC , b DESC ),
key a_desc_b_asc (a DESC , b ASC ))
ENGINE = InnoDB;
The following query plans are:
EXPLAIN SELECT max (a) FROM t1;
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
EXPLAIN SELECT max (a) FROM t1 ORDER BY a DESC ;
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE t1 index NULL ab_asc 24 NULL 13 Using index
Thus second query:
is using index, but in this case the query will return only one result and the "group by" can be ignored
optimizer uses index "ab_asc" instead of Primary key with descending order
875377ad824473774c833b1aff4346ba3133f092 MDEV-27576 Use reverse index for max/min optimization
Yuchen Pei
added a comment - - edited pushed the following to 11.4
875377ad824473774c833b1aff4346ba3133f092 MDEV-27576 Use reverse index for max/min optimization
People
Yuchen Pei
Elena Stepanova
Votes:
0Vote for this issue
Watchers:
6Start watching this issue
Dates
Created:
Updated:
Resolved:
Git Integration
Error rendering 'com.xiplink.jira.git.jira_git_plugin:git-issue-webpanel'. Please contact your Jira administrators.
{"report":{"fcp":1740.1999998092651,"ttfb":366.90000009536743,"pageVisibility":"visible","entityId":107058,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"84a00f48-986a-40fa-98a5-1c026f2fcb52","navigationType":0,"readyForUser":1869.0999999046326,"redirectCount":0,"resourceLoadedEnd":1481.1999998092651,"resourceLoadedStart":389.7999997138977,"resourceTiming":[{"duration":463.6000003814697,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":389.7999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":389.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":853.4000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":463.69999980926514,"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":390.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":390.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":853.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":811.1000003814697,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":390.2999997138977,"connectEnd":390.2999997138977,"connectStart":390.2999997138977,"domainLookupEnd":390.2999997138977,"domainLookupStart":390.2999997138977,"fetchStart":390.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":860.6999998092651,"responseEnd":1201.4000000953674,"responseStart":910,"secureConnectionStart":390.2999997138977},{"duration":1017.1999998092651,"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":390.5,"connectEnd":390.5,"connectStart":390.5,"domainLookupEnd":390.5,"domainLookupStart":390.5,"fetchStart":390.5,"redirectEnd":0,"redirectStart":0,"requestStart":860.7999997138977,"responseEnd":1407.6999998092651,"responseStart":910.9000000953674,"secureConnectionStart":390.5},{"duration":526.4000000953674,"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":390.59999990463257,"connectEnd":390.59999990463257,"connectStart":390.59999990463257,"domainLookupEnd":390.59999990463257,"domainLookupStart":390.59999990463257,"fetchStart":390.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":860.9000000953674,"responseEnd":917,"responseStart":912.5,"secureConnectionStart":390.59999990463257},{"duration":528.3000001907349,"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":390.7999997138977,"connectEnd":390.7999997138977,"connectStart":390.7999997138977,"domainLookupEnd":390.7999997138977,"domainLookupStart":390.7999997138977,"fetchStart":390.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":861.0999999046326,"responseEnd":919.0999999046326,"responseStart":914.5,"secureConnectionStart":390.7999997138977},{"duration":526.9000000953674,"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":391,"connectEnd":391,"connectStart":391,"domainLookupEnd":391,"domainLookupStart":391,"fetchStart":391,"redirectEnd":0,"redirectStart":0,"requestStart":861.0999999046326,"responseEnd":917.9000000953674,"responseStart":913.1999998092651,"secureConnectionStart":391},{"duration":464.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":391.19999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":391.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":855.2999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":526.9000000953674,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":391.2999997138977,"connectEnd":391.2999997138977,"connectStart":391.2999997138977,"domainLookupEnd":391.2999997138977,"domainLookupStart":391.2999997138977,"fetchStart":391.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":861.4000000953674,"responseEnd":918.1999998092651,"responseStart":913.9000000953674,"secureConnectionStart":391.2999997138977},{"duration":463.90000009536743,"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":391.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":391.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":855.4000000953674,"responseStart":0,"secureConnectionStart":0},{"duration":528.5,"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":392.2999997138977,"connectEnd":392.2999997138977,"connectStart":392.2999997138977,"domainLookupEnd":392.2999997138977,"domainLookupStart":392.2999997138977,"fetchStart":392.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":861.9000000953674,"responseEnd":920.7999997138977,"responseStart":915.0999999046326,"secureConnectionStart":392.2999997138977},{"duration":976,"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":393.2999997138977,"connectEnd":393.2999997138977,"connectStart":393.2999997138977,"domainLookupEnd":393.2999997138977,"domainLookupStart":393.2999997138977,"fetchStart":393.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":1349.5999999046326,"responseEnd":1369.2999997138977,"responseStart":1367.6999998092651,"secureConnectionStart":393.2999997138977},{"duration":1087.6999998092651,"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":393.5,"connectEnd":393.5,"connectStart":393.5,"domainLookupEnd":393.5,"domainLookupStart":393.5,"fetchStart":393.5,"redirectEnd":0,"redirectStart":0,"requestStart":1466.4000000953674,"responseEnd":1481.1999998092651,"responseStart":1480.1999998092651,"secureConnectionStart":393.5},{"duration":152.7999997138977,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1350,"connectEnd":1350,"connectStart":1350,"domainLookupEnd":1350,"domainLookupStart":1350,"fetchStart":1350,"redirectEnd":0,"redirectStart":0,"requestStart":1462.1999998092651,"responseEnd":1502.7999997138977,"responseStart":1502,"secureConnectionStart":1350}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":170,"responseStart":367,"responseEnd":378,"domLoading":387,"domInteractive":2099,"domContentLoadedEventStart":2099,"domContentLoadedEventEnd":2261,"domComplete":4261,"loadEventStart":4261,"loadEventEnd":4271,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1986.7999997138977},{"name":"bigPipe.sidebar-id.end","time":1991},{"name":"bigPipe.activity-panel-pipe-id.start","time":1991.0999999046326},{"name":"bigPipe.activity-panel-pipe-id.end","time":1994.1999998092651},{"name":"activityTabFullyLoaded","time":2299.2999997138977}],"measures":[],"correlationId":"812f750bfa6682","effectiveType":"4g","downlink":9,"rtt":0,"serverDuration":104,"dbReadsTimeInMs":11,"dbConnsTimeInMs":20,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Thanks for the comments psergei. I addressed them and updated my commit. ptal thanks (I understand the fixversion is 11.5 so no rush):
upstream/bb-11.3-mdev-27576 8398e5c153320efbbeeeeac9d9029e0f932aaaa2
MDEV-27576 Use reverse index for max/min optimization
We use a bool to indicate that the key part used is a descending
index, which will flip the functions and flags used in
get_index_max_value() and get_index_min_value(), that allows correct
optimization for max/min for descending index.