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
{"report":{"fcp":1626.2000000476837,"ttfb":919.9000000357628,"pageVisibility":"visible","entityId":50936,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"266c0c56-9ed9-4abc-890a-f482e284e010","navigationType":0,"readyForUser":1717.800000011921,"redirectCount":0,"resourceLoadedEnd":2175.100000023842,"resourceLoadedStart":925.4000000357628,"resourceTiming":[{"duration":161.39999997615814,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":925.4000000357628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":925.4000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1086.800000011921,"responseStart":0,"secureConnectionStart":0},{"duration":161.39999997615814,"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":925.7000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":925.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1087.1000000238419,"responseStart":0,"secureConnectionStart":0},{"duration":218.30000001192093,"initiatorType":"script","name":"https://jira.mariadb.org/s/fbf975c0cce4b1abf04784eeae9ba1f4-CDN/lu2bu7/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":926,"connectEnd":926,"connectStart":926,"domainLookupEnd":926,"domainLookupStart":926,"fetchStart":926,"redirectEnd":0,"redirectStart":0,"requestStart":926,"responseEnd":1144.300000011921,"responseStart":1144.300000011921,"secureConnectionStart":926},{"duration":296.60000002384186,"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":926.1000000238419,"connectEnd":926.1000000238419,"connectStart":926.1000000238419,"domainLookupEnd":926.1000000238419,"domainLookupStart":926.1000000238419,"fetchStart":926.1000000238419,"redirectEnd":0,"redirectStart":0,"requestStart":926.1000000238419,"responseEnd":1222.7000000476837,"responseStart":1222.6000000238419,"secureConnectionStart":926.1000000238419},{"duration":300.10000002384186,"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":926.3000000119209,"connectEnd":926.3000000119209,"connectStart":926.3000000119209,"domainLookupEnd":926.3000000119209,"domainLookupStart":926.3000000119209,"fetchStart":926.3000000119209,"redirectEnd":0,"redirectStart":0,"requestStart":926.3000000119209,"responseEnd":1226.4000000357628,"responseStart":1226.4000000357628,"secureConnectionStart":926.3000000119209},{"duration":300.30000001192093,"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":926.5,"connectEnd":926.5,"connectStart":926.5,"domainLookupEnd":926.5,"domainLookupStart":926.5,"fetchStart":926.5,"redirectEnd":0,"redirectStart":0,"requestStart":926.5,"responseEnd":1226.800000011921,"responseStart":1226.800000011921,"secureConnectionStart":926.5},{"duration":300.5,"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":926.7000000476837,"connectEnd":926.7000000476837,"connectStart":926.7000000476837,"domainLookupEnd":926.7000000476837,"domainLookupStart":926.7000000476837,"fetchStart":926.7000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":926.7000000476837,"responseEnd":1227.2000000476837,"responseStart":1227.2000000476837,"secureConnectionStart":926.7000000476837},{"duration":365.5,"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":926.9000000357628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":926.9000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1292.4000000357628,"responseStart":0,"secureConnectionStart":0},{"duration":300.7000000476837,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":927,"connectEnd":927,"connectStart":927,"domainLookupEnd":927,"domainLookupStart":927,"fetchStart":927,"redirectEnd":0,"redirectStart":0,"requestStart":927,"responseEnd":1227.7000000476837,"responseStart":1227.7000000476837,"secureConnectionStart":927},{"duration":365.2999999523163,"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":927.2000000476837,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":927.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1292.5,"responseStart":0,"secureConnectionStart":0},{"duration":301.4000000357628,"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":927.3000000119209,"connectEnd":927.3000000119209,"connectStart":927.3000000119209,"domainLookupEnd":927.3000000119209,"domainLookupStart":927.3000000119209,"fetchStart":927.3000000119209,"redirectEnd":0,"redirectStart":0,"requestStart":927.3000000119209,"responseEnd":1228.7000000476837,"responseStart":1228.7000000476837,"secureConnectionStart":927.3000000119209},{"duration":793,"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":933.5,"connectEnd":933.5,"connectStart":933.5,"domainLookupEnd":933.5,"domainLookupStart":933.5,"fetchStart":933.5,"redirectEnd":0,"redirectStart":0,"requestStart":933.5,"responseEnd":1726.5,"responseStart":1726.5,"secureConnectionStart":933.5},{"duration":1240.8999999761581,"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":934.2000000476837,"connectEnd":934.2000000476837,"connectStart":934.2000000476837,"domainLookupEnd":934.2000000476837,"domainLookupStart":934.2000000476837,"fetchStart":934.2000000476837,"redirectEnd":0,"redirectStart":0,"requestStart":934.2000000476837,"responseEnd":2175.100000023842,"responseStart":2175.100000023842,"secureConnectionStart":934.2000000476837},{"duration":422.5,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":1304.4000000357628,"connectEnd":1304.4000000357628,"connectStart":1304.4000000357628,"domainLookupEnd":1304.4000000357628,"domainLookupStart":1304.4000000357628,"fetchStart":1304.4000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":1304.4000000357628,"responseEnd":1726.9000000357628,"responseStart":1726.9000000357628,"secureConnectionStart":1304.4000000357628},{"duration":578.5,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":1612.4000000357628,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1612.4000000357628,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":2190.900000035763,"responseStart":0,"secureConnectionStart":0}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":712,"responseStart":920,"responseEnd":932,"domLoading":923,"domInteractive":2223,"domContentLoadedEventStart":2223,"domContentLoadedEventEnd":2276,"domComplete":3124,"loadEventStart":3124,"loadEventEnd":3124,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":2192.400000035763},{"name":"bigPipe.sidebar-id.end","time":2193.400000035763},{"name":"bigPipe.activity-panel-pipe-id.start","time":2193.5},{"name":"bigPipe.activity-panel-pipe-id.end","time":2197.100000023842},{"name":"activityTabFullyLoaded","time":2293.100000023842}],"measures":[],"correlationId":"aac53b2423664e","effectiveType":"4g","downlink":10,"rtt":0,"serverDuration":134,"dbReadsTimeInMs":17,"dbConnsTimeInMs":26,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Note: Looks pretty much the same on MySQL 5.6
MySQL [test]> explain
MySQL [test]> explain
| @@version |
| 5.6.24-debug |