In join of 4 tables, optimizer won't use covering index with suitable ordering on first table in case join order of last two tables uses index/table scan on one of them instead of eq_ref - that table has only 2 rows, so optimizer does not seem to feel need to use eq_ref on it, but then resulting plan needs filesort. Happens with and without "LIMIT 3".
explain
|
SELECT evaluation, t.ticket_id
|
FROM ticket_game_result g
|
JOIN ticket t ON g.ticket_id=t.ticket_id
|
JOIN uzivatel u ON (t.user_id=u.user_id)
|
JOIN mena c ON c.mena_id=u.mena_id
|
WHERE (g.game_id = 2) AND (g.cancelled = 0) AND (t.vyplacen_date > '2013-06-31 22:00:00')
|
ORDER BY `g`.`evaluation` DESC LIMIT 3;
|
+------+-------------+-------+--------+------------------------------------------------+---------+---------+---------------------------+------+-------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+--------+------------------------------------------------+---------+---------+---------------------------+------+-------------------------------------------------+
|
| 1 | SIMPLE | g | ref | PRIMARY,fk__ticket_game_result__ticket_id,eval | PRIMARY | 2 | const | 54 | Using where; Using temporary; Using filesort |
|
| 1 | SIMPLE | t | eq_ref | PRIMARY,FK_ticket_1,vyplacen_date | PRIMARY | 8 | orderby_index.g.ticket_id | 1 | Using where |
|
| 1 | SIMPLE | c | index | PRIMARY | PRIMARY | 2 | NULL | 2 | Using index; Using join buffer (flat, BNL join) |
|
| 1 | SIMPLE | u | eq_ref | PRIMARY,FK_mena | PRIMARY | 4 | orderby_index.t.user_id | 1 | Using where |
|
+------+-------------+-------+--------+------------------------------------------------+---------+---------+---------------------------+------+-------------------------------------------------+
|
explain
|
SELECT evaluation, t.ticket_id
|
FROM ticket_game_result g IGNORE INDEX (PRIMARY)
|
JOIN ticket t ON g.ticket_id=t.ticket_id
|
JOIN uzivatel u ON (t.user_id=u.user_id)
|
JOIN mena c ON c.mena_id=u.mena_id
|
WHERE (g.game_id = 2) AND (g.cancelled = 0) AND (t.vyplacen_date > '2013-06-31 22:00:00')
|
ORDER BY `g`.`evaluation` DESC LIMIT 3;
|
+------+-------------+-------+--------+----------------------------------------+---------+---------+---------------------------+------+-------------------------------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+--------+----------------------------------------+---------+---------+---------------------------+------+-------------------------------------------------+
|
| 1 | SIMPLE | g | ref | fk__ticket_game_result__ticket_id,eval | eval | 3 | const,const | 54 | Using index; Using temporary; Using filesort |
|
| 1 | SIMPLE | t | eq_ref | PRIMARY,FK_ticket_1,vyplacen_date | PRIMARY | 8 | orderby_index.g.ticket_id | 1 | Using where |
|
| 1 | SIMPLE | c | index | PRIMARY | PRIMARY | 2 | NULL | 2 | Using index; Using join buffer (flat, BNL join) |
|
| 1 | SIMPLE | u | eq_ref | PRIMARY,FK_mena | PRIMARY | 4 | orderby_index.t.user_id | 1 | Using where |
|
+------+-------------+-------+--------+----------------------------------------+---------+---------+---------------------------+------+-------------------------------------------------+
|
explain
|
SELECT evaluation, t.ticket_id
|
FROM ticket_game_result g
|
JOIN ticket t ON g.ticket_id=t.ticket_id
|
JOIN uzivatel u ON (t.user_id=u.user_id)
|
STRAIGHT_JOIN mena c ON c.mena_id=u.mena_id
|
WHERE (g.game_id = 2) AND (g.cancelled = 0) AND (t.vyplacen_date > '2013-06-31 22:00:00')
|
ORDER BY `g`.`evaluation` DESC LIMIT 3;
|
+------+-------------+-------+--------+------------------------------------------------+---------+---------+---------------------------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+--------+------------------------------------------------+---------+---------+---------------------------+------+-------------+
|
| 1 | SIMPLE | g | ref | PRIMARY,fk__ticket_game_result__ticket_id,eval | eval | 3 | const,const | 54 | Using where |
|
| 1 | SIMPLE | t | eq_ref | PRIMARY,FK_ticket_1,vyplacen_date | PRIMARY | 8 | orderby_index.g.ticket_id | 1 | Using where |
|
| 1 | SIMPLE | u | eq_ref | PRIMARY,FK_mena | PRIMARY | 4 | orderby_index.t.user_id | 1 | |
|
| 1 | SIMPLE | c | eq_ref | PRIMARY | PRIMARY | 2 | orderby_index.u.mena_id | 1 | Using index |
|
+------+-------------+-------+--------+------------------------------------------------+---------+---------+---------------------------+------+-------------+
|
Attached is testing dump of 100KB database showing the behavior - on our production server there was one more trouble with plan changing when otherwise unused index is ignored, but I was not able to replicate that on this smaller dataset and I have seen some similar issue already.
Dump contains analyze table for all 4 tables, because without it after import I was not able to replicate. Plans were totally different.
- is part of
-
MDEV-8306
Complete cost-based optimization for ORDER BY with LIMIT
-
-
Stalled
No workflow transitions have been executed yet.
{"report":{"fcp":852,"ttfb":264.09999999403954,"pageVisibility":"visible","entityId":50936,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":32,"apdex":1,"journeyId":"ef7e519d-8cb6-4f50-a784-8d05c5ed4a3d","navigationType":0,"readyForUser":949,"redirectCount":0,"resourceLoadedEnd":983.2999999970198,"resourceLoadedStart":269.29999999701977,"resourceTiming":[{"duration":19.200000002980232,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":269.29999999701977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":269.29999999701977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":288.5,"responseStart":0,"secureConnectionStart":0},{"duration":19.30000001192093,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":269.59999999403954,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":269.59999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":288.90000000596046,"responseStart":0,"secureConnectionStart":0},{"duration":95.29999999701977,"initiatorType":"script","name":"https://jira.mariadb.org/s/e9b27a47da5fb0f74a35acd57e9847fb-CDN/lu2bv2/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":269.70000000298023,"connectEnd":269.70000000298023,"connectStart":269.70000000298023,"domainLookupEnd":269.70000000298023,"domainLookupStart":269.70000000298023,"fetchStart":269.70000000298023,"redirectEnd":0,"redirectStart":0,"requestStart":269.70000000298023,"responseEnd":365,"responseStart":365,"secureConnectionStart":269.70000000298023},{"duration":218.70000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/s/c32eb0da7ad9831253f8397e6cc26afd-CDN/lu2bv2/820016/12ta74/2380add21a9a1006587582385952de73/_/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":270,"connectEnd":270,"connectStart":270,"domainLookupEnd":270,"domainLookupStart":270,"fetchStart":270,"redirectEnd":0,"redirectStart":0,"requestStart":270,"responseEnd":488.70000000298023,"responseStart":488.70000000298023,"secureConnectionStart":270},{"duration":222.1000000089407,"initiatorType":"script","name":"https://jira.mariadb.org/s/bc0bcb146314416123c992714ee00ff7-CDN/lu2bv2/820016/12ta74/c92c0caa9a024ae85b0ebdbed7fb4bd7/_/download/contextbatch/js/atl.global,-_super/batch.js?locale=en","startTime":270.09999999403954,"connectEnd":270.09999999403954,"connectStart":270.09999999403954,"domainLookupEnd":270.09999999403954,"domainLookupStart":270.09999999403954,"fetchStart":270.09999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":270.09999999403954,"responseEnd":492.20000000298023,"responseStart":492.20000000298023,"secureConnectionStart":270.09999999403954},{"duration":222.19999998807907,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-en/jira.webresources:calendar-en.js","startTime":270.40000000596046,"connectEnd":270.40000000596046,"connectStart":270.40000000596046,"domainLookupEnd":270.40000000596046,"domainLookupStart":270.40000000596046,"fetchStart":270.40000000596046,"redirectEnd":0,"redirectStart":0,"requestStart":270.40000000596046,"responseEnd":492.59999999403954,"responseStart":492.59999999403954,"secureConnectionStart":270.40000000596046},{"duration":222.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:calendar-localisation-moment/jira.webresources:calendar-localisation-moment.js","startTime":270.5,"connectEnd":270.5,"connectStart":270.5,"domainLookupEnd":270.5,"domainLookupStart":270.5,"fetchStart":270.5,"redirectEnd":0,"redirectStart":0,"requestStart":270.5,"responseEnd":493,"responseStart":493,"secureConnectionStart":270.5},{"duration":233.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/b04b06a02d1959df322d9cded3aeecc1-CDN/lu2bv2/820016/12ta74/a2ff6aa845ffc9a1d22fe23d9ee791fc/_/download/contextbatch/css/jira.global.look-and-feel,-_super/batch.css","startTime":270.70000000298023,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":270.70000000298023,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":504.20000000298023,"responseStart":0,"secureConnectionStart":0},{"duration":222.70000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":270.79999999701977,"connectEnd":270.79999999701977,"connectStart":270.79999999701977,"domainLookupEnd":270.79999999701977,"domainLookupStart":270.79999999701977,"fetchStart":270.79999999701977,"redirectEnd":0,"redirectStart":0,"requestStart":270.79999999701977,"responseEnd":493.5,"responseStart":493.5,"secureConnectionStart":270.79999999701977},{"duration":233.29999999701977,"initiatorType":"link","name":"https://jira.mariadb.org/s/3ac36323ba5e4eb0af2aa7ac7211b4bb-CDN/lu2bv2/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":271,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":271,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":504.29999999701977,"responseStart":0,"secureConnectionStart":0},{"duration":222.90000000596046,"initiatorType":"script","name":"https://jira.mariadb.org/s/719848dd97ebe0663199f49a3936487a-CDN/lu2bv2/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":271.09999999403954,"connectEnd":271.09999999403954,"connectStart":271.09999999403954,"domainLookupEnd":271.09999999403954,"domainLookupStart":271.09999999403954,"fetchStart":271.09999999403954,"redirectEnd":0,"redirectStart":0,"requestStart":271.09999999403954,"responseEnd":494,"responseStart":494,"secureConnectionStart":271.09999999403954},{"duration":434,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-js/jira.webresources:bigpipe-js.js","startTime":276.29999999701977,"connectEnd":276.29999999701977,"connectStart":276.29999999701977,"domainLookupEnd":276.29999999701977,"domainLookupStart":276.29999999701977,"fetchStart":276.29999999701977,"redirectEnd":0,"redirectStart":0,"requestStart":276.29999999701977,"responseEnd":710.2999999970198,"responseStart":710.2000000029802,"secureConnectionStart":276.29999999701977},{"duration":500.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/820016/12ta74/1.0/_/download/batch/jira.webresources:bigpipe-init/jira.webresources:bigpipe-init.js","startTime":276.29999999701977,"connectEnd":276.29999999701977,"connectStart":276.29999999701977,"domainLookupEnd":276.29999999701977,"domainLookupStart":276.29999999701977,"fetchStart":276.29999999701977,"redirectEnd":0,"redirectStart":0,"requestStart":276.29999999701977,"responseEnd":776.7999999970198,"responseStart":776.7999999970198,"secureConnectionStart":276.29999999701977},{"duration":195.79999999701977,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":515.4000000059605,"connectEnd":515.4000000059605,"connectStart":515.4000000059605,"domainLookupEnd":515.4000000059605,"domainLookupStart":515.4000000059605,"fetchStart":515.4000000059605,"redirectEnd":0,"redirectStart":0,"requestStart":515.4000000059605,"responseEnd":711.2000000029802,"responseStart":711.2000000029802,"secureConnectionStart":515.4000000059605},{"duration":143.6000000089407,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":846.0999999940395,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":846.0999999940395,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":989.7000000029802,"responseStart":0,"secureConnectionStart":0},{"duration":128.5,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2bv2/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","startTime":854.7999999970198,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":854.7999999970198,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":983.2999999970198,"responseStart":0,"secureConnectionStart":0},{"duration":43.20000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2bv2/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","startTime":855.5999999940395,"connectEnd":855.5999999940395,"connectStart":855.5999999940395,"domainLookupEnd":855.5999999940395,"domainLookupStart":855.5999999940395,"fetchStart":855.5999999940395,"redirectEnd":0,"redirectStart":0,"requestStart":855.5999999940395,"responseEnd":898.7999999970198,"responseStart":898.7999999970198,"secureConnectionStart":855.5999999940395},{"duration":46.20000000298023,"initiatorType":"script","name":"https://jira.mariadb.org/s/53a43b6764f587426c7bb9a150184c00-CDN/lu2bv2/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","startTime":856.0999999940395,"connectEnd":856.0999999940395,"connectStart":856.0999999940395,"domainLookupEnd":856.0999999940395,"domainLookupStart":856.0999999940395,"fetchStart":856.0999999940395,"redirectEnd":0,"redirectStart":0,"requestStart":856.0999999940395,"responseEnd":902.2999999970198,"responseStart":902.2999999970198,"secureConnectionStart":856.0999999940395}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":73,"responseStart":264,"responseEnd":268,"domLoading":267,"domInteractive":1019,"domContentLoadedEventStart":1019,"domContentLoadedEventEnd":1065,"domComplete":1394,"loadEventStart":1394,"loadEventEnd":1394,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":992.2999999970198},{"name":"bigPipe.sidebar-id.end","time":993},{"name":"bigPipe.activity-panel-pipe-id.start","time":993.0999999940395},{"name":"bigPipe.activity-panel-pipe-id.end","time":993.5999999940395},{"name":"activityTabFullyLoaded","time":1081.4000000059605}],"measures":[],"correlationId":"3e05fa15939141","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":107,"dbReadsTimeInMs":15,"dbConnsTimeInMs":23,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}