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":1584.7000000476837,"ttfb":747.2000000476837,"pageVisibility":"visible","entityId":107058,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":0.5,"journeyId":"18e19e1f-7e29-49bb-9b9d-2497703c416f","navigationType":0,"readyForUser":1694.5,"redirectCount":0,"resourceLoadedEnd":1237.7999999523163,"resourceLoadedStart":752.5999999046326,"resourceTiming":[{"duration":142.10000014305115,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":752.5999999046326,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":752.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":894.7000000476837,"responseStart":0,"secureConnectionStart":0},{"duration":142.20000004768372,"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":752.7999999523163,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":752.7999999523163,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":895,"responseStart":0,"secureConnectionStart":0},{"duration":337.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":753,"connectEnd":753,"connectStart":753,"domainLookupEnd":753,"domainLookupStart":753,"fetchStart":753,"redirectEnd":0,"redirectStart":0,"requestStart":900.4000000953674,"responseEnd":1090.5999999046326,"responseStart":971.2999999523163,"secureConnectionStart":753},{"duration":484.59999990463257,"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":753.2000000476837,"connectEnd":753.2000000476837,"connectStart":753.2000000476837,"domainLookupEnd":753.2000000476837,"domainLookupStart":753.2000000476837,"fetchStart":753.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":900.4000000953674,"responseEnd":1237.7999999523163,"responseStart":1106.0999999046326,"secureConnectionStart":753.2000000476837},{"duration":195.29999995231628,"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":753.4000000953674,"connectEnd":753.4000000953674,"connectStart":753.4000000953674,"domainLookupEnd":753.4000000953674,"domainLookupStart":753.4000000953674,"fetchStart":753.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":902,"responseEnd":948.7000000476837,"responseStart":948,"secureConnectionStart":753.4000000953674},{"duration":196.10000014305115,"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":753.5999999046326,"connectEnd":753.5999999046326,"connectStart":753.5999999046326,"domainLookupEnd":753.5999999046326,"domainLookupStart":753.5999999046326,"fetchStart":753.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":903.0999999046326,"responseEnd":949.7000000476837,"responseStart":949.2999999523163,"secureConnectionStart":753.5999999046326},{"duration":205.79999995231628,"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":753.7000000476837,"connectEnd":753.7000000476837,"connectStart":753.7000000476837,"domainLookupEnd":753.7000000476837,"domainLookupStart":753.7000000476837,"fetchStart":753.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":903.5,"responseEnd":959.5,"responseStart":958.7999999523163,"secureConnectionStart":753.7000000476837},{"duration":141.69999980926514,"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":753.9000000953674,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":753.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":895.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":197,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":754.0999999046326,"connectEnd":754.0999999046326,"connectStart":754.0999999046326,"domainLookupEnd":754.0999999046326,"domainLookupStart":754.0999999046326,"fetchStart":754.0999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":903.7000000476837,"responseEnd":951.0999999046326,"responseStart":950.5,"secureConnectionStart":754.0999999046326},{"duration":141.89999985694885,"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":754.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":754.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":896.0999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":208.59999990463257,"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":754.4000000953674,"connectEnd":754.4000000953674,"connectStart":754.4000000953674,"domainLookupEnd":754.4000000953674,"domainLookupStart":754.4000000953674,"fetchStart":754.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":903.7000000476837,"responseEnd":963,"responseStart":959.5999999046326,"secureConnectionStart":754.4000000953674},{"duration":322.59999990463257,"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":760.9000000953674,"connectEnd":760.9000000953674,"connectStart":760.9000000953674,"domainLookupEnd":760.9000000953674,"domainLookupStart":760.9000000953674,"fetchStart":760.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":919.2000000476837,"responseEnd":1083.5,"responseStart":1076.2000000476837,"secureConnectionStart":760.9000000953674},{"duration":324.7999999523163,"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":760.9000000953674,"connectEnd":760.9000000953674,"connectStart":760.9000000953674,"domainLookupEnd":760.9000000953674,"domainLookupStart":760.9000000953674,"fetchStart":760.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":940,"responseEnd":1085.7000000476837,"responseStart":1080,"secureConnectionStart":760.9000000953674},{"duration":106.69999980926514,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1244.4000000953674,"connectEnd":1244.4000000953674,"connectStart":1244.4000000953674,"domainLookupEnd":1244.4000000953674,"domainLookupStart":1244.4000000953674,"fetchStart":1244.4000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1319.9000000953674,"responseEnd":1351.0999999046326,"responseStart":1350.0999999046326,"secureConnectionStart":1244.4000000953674},{"duration":330.39999985694885,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1510.9000000953674,"connectEnd":1510.9000000953674,"connectStart":1510.9000000953674,"domainLookupEnd":1510.9000000953674,"domainLookupStart":1510.9000000953674,"fetchStart":1510.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":1810.2999999523163,"responseEnd":1841.2999999523163,"responseStart":1840.5999999046326,"secureConnectionStart":1510.9000000953674}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":68,"responseStart":747,"responseEnd":756,"domLoading":751,"domInteractive":1781,"domContentLoadedEventStart":1781,"domContentLoadedEventEnd":1855,"domComplete":2693,"loadEventStart":2693,"loadEventEnd":2694,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1752},{"name":"bigPipe.sidebar-id.end","time":1752.7999999523163},{"name":"bigPipe.activity-panel-pipe-id.start","time":1753},{"name":"bigPipe.activity-panel-pipe-id.end","time":1755.7999999523163},{"name":"activityTabFullyLoaded","time":1873.4000000953674}],"measures":[],"correlationId":"4f18770022c9","effectiveType":"4g","downlink":9.1,"rtt":0,"serverDuration":570,"dbReadsTimeInMs":13,"dbConnsTimeInMs":23,"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.