DROP TABLE IF EXISTS t1;
|
CREATE TABLE t1(id INT PRIMARY KEY, msg VARCHAR(10));
|
|
INSERT INTO t1 VALUES(1, 'abc1');
|
INSERT INTO t1 VALUES(2, 'abc2');
|
INSERT INTO t1 VALUES(3, 'abc3');
|
INSERT INTO t1 VALUES(4, 'abc4');
|
INSERT INTO t1 VALUES(5, 'abc5');
|
INSERT INTO t1 VALUES(6, 'abc6');
|
INSERT INTO t1 VALUES(7, 'abc7');
|
INSERT INTO t1 VALUES(8, 'abc8');
|
INSERT INTO t1 VALUES(9, 'abc9');
|
|
DROP TABLE IF EXISTS t2;
|
CREATE TABLE t2(id INT PRIMARY KEY, msg VARCHAR(10));
|
|
INSERT INTO t2 SELECT * FROM t1;
|
|
EXPLAIN SELECT t1.id FROM t1 WHERE t1.id IN ( SELECT id FROM t2 WHERE id=5);
|
|
EXPLAIN DELETE FROM t1 WHERE t1.id IN ( SELECT id FROM t2 WHERE id=5);
|
|
EXPLAIN DELETE t1 FROM t1 JOIN t2 ON t1.id = t2.id AND t2.id = 5;
|
In the SELECT case the query plan looks fine and the subquery is optimized away into a regular join:
+------+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
| 1 | PRIMARY | t1 | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
|
| 1 | PRIMARY | t2 | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
|
+------+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
In the DELETE case the subquery is still there, and a full table scan is done on table t1:
+------+--------------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 9 | Using where |
|
| 2 | DEPENDENT SUBQUERY | t2 | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
|
+------+--------------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
Rewriting the DELETE to use the multi table form works as a workaround:
+------+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
| 1 | SIMPLE | t1 | const | PRIMARY | PRIMARY | 4 | const | 1 | |
|
| 1 | SIMPLE | t2 | const | PRIMARY | PRIMARY | 4 | const | 1 | Using index |
|
+------+-------------+-------+-------+---------------+---------+---------+-------+------+-------------+
|
Similar problems can be seen when using a secondary index in the subquery:
ALTER TABLE t2 ADD INDEX(msg);
|
|
MariaDB [test]> EXPLAIN SELECT t1.id FROM t1 WHERE t1.id IN ( SELECT id FROM t2 WHERE msg='abc5');
|
+------+-------------+-------+--------+---------------+---------+---------+------------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+--------+---------------+---------+---------+------------+------+--------------------------+
|
| 1 | PRIMARY | t2 | ref | PRIMARY,msg | msg | 13 | const | 1 | Using where; Using index |
|
| 1 | PRIMARY | t1 | eq_ref | PRIMARY | PRIMARY | 4 | test.t2.id | 1 | Using index |
|
+------+-------------+-------+--------+---------------+---------+---------+------------+------+--------------------------+
|
2 rows in set (0.00 sec)
|
|
MariaDB [test]> EXPLAIN DELETE FROM t1 WHERE t1.id IN ( SELECT id FROM t2 WHERE msg='abc5');
|
+------+--------------------+-------+-----------------+---------------+---------+---------+------+------+-------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+--------------------+-------+-----------------+---------------+---------+---------+------+------+-------------+
|
| 1 | PRIMARY | t1 | ALL | NULL | NULL | NULL | NULL | 9 | Using where |
|
| 2 | DEPENDENT SUBQUERY | t2 | unique_subquery | PRIMARY,msg | PRIMARY | 4 | func | 1 | Using where |
|
+------+--------------------+-------+-----------------+---------------+---------+---------+------+------+-------------+
|
2 rows in set (0.00 sec)
|
|
MariaDB [test]> EXPLAIN DELETE t1 FROM t1 JOIN t2 ON t1.id = t2.id AND t2.msg='abc5';
|
+------+-------------+-------+--------+---------------+---------+---------+------------+------+--------------------------+
|
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
|
+------+-------------+-------+--------+---------------+---------+---------+------------+------+--------------------------+
|
| 1 | SIMPLE | t2 | ref | PRIMARY,msg | msg | 13 | const | 1 | Using where; Using index |
|
| 1 | SIMPLE | t1 | eq_ref | PRIMARY | PRIMARY | 4 | test.t2.id | 1 | |
|
+------+-------------+-------+--------+---------------+---------+---------+------------+------+--------------------------+
|
2 rows in set (0.00 sec)
|
|
See also: https://bugs.mysql.com/35794
{"report":{"fcp":1228.4000000953674,"ttfb":363.40000009536743,"pageVisibility":"visible","entityId":65999,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"750ab195-a729-4675-b847-379f01d47d55","navigationType":0,"readyForUser":1335.4000000953674,"redirectCount":0,"resourceLoadedEnd":1416.6999998092651,"resourceLoadedStart":385.40000009536743,"resourceTiming":[{"duration":197.2999997138977,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":385.40000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":385.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":582.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":197.30000019073486,"initiatorType":"link","name":"https://jira.mariadb.org/s/7ebd35e77e471bc30ff0eba799ebc151-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/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":385.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":385.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":583,"responseStart":0,"secureConnectionStart":0},{"duration":206.5,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":386,"connectEnd":386,"connectStart":386,"domainLookupEnd":386,"domainLookupStart":386,"fetchStart":386,"redirectEnd":0,"redirectStart":0,"requestStart":386,"responseEnd":592.5,"responseStart":592.5,"secureConnectionStart":386},{"duration":298.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/2d8175ec2fa4c816e8023260bd8c1786-CDN/lu2cib/820016/12ta74/2bf333562ca6724060a9d5f1535471f6/_/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":391,"connectEnd":391,"connectStart":391,"domainLookupEnd":391,"domainLookupStart":391,"fetchStart":391,"redirectEnd":0,"redirectStart":0,"requestStart":391,"responseEnd":689.5999999046326,"responseStart":689.5999999046326,"secureConnectionStart":391},{"duration":302.30000019073486,"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":391.19999980926514,"connectEnd":391.19999980926514,"connectStart":391.19999980926514,"domainLookupEnd":391.19999980926514,"domainLookupStart":391.19999980926514,"fetchStart":391.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":391.19999980926514,"responseEnd":693.5,"responseStart":693.5,"secureConnectionStart":391.19999980926514},{"duration":302.8999996185303,"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":391.40000009536743,"connectEnd":391.40000009536743,"connectStart":391.40000009536743,"domainLookupEnd":391.40000009536743,"domainLookupStart":391.40000009536743,"fetchStart":391.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":391.40000009536743,"responseEnd":694.2999997138977,"responseStart":694.2999997138977,"secureConnectionStart":391.40000009536743},{"duration":303.30000019073486,"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.59999990463257,"connectEnd":391.59999990463257,"connectStart":391.59999990463257,"domainLookupEnd":391.59999990463257,"domainLookupStart":391.59999990463257,"fetchStart":391.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":391.59999990463257,"responseEnd":694.9000000953674,"responseStart":694.9000000953674,"secureConnectionStart":391.59999990463257},{"duration":382.90000009536743,"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.7999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":391.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":774.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":303.59999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":391.90000009536743,"connectEnd":391.90000009536743,"connectStart":391.90000009536743,"domainLookupEnd":391.90000009536743,"domainLookupStart":391.90000009536743,"fetchStart":391.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":391.90000009536743,"responseEnd":695.5,"responseStart":695.5,"secureConnectionStart":391.90000009536743},{"duration":382.69999980926514,"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":392.09999990463257,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":392.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":774.7999997138977,"responseStart":0,"secureConnectionStart":0},{"duration":304.30000019073486,"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.19999980926514,"connectEnd":392.19999980926514,"connectStart":392.19999980926514,"domainLookupEnd":392.19999980926514,"domainLookupStart":392.19999980926514,"fetchStart":392.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":392.19999980926514,"responseEnd":696.5,"responseStart":696.4000000953674,"secureConnectionStart":392.19999980926514},{"duration":441.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":449.40000009536743,"connectEnd":449.40000009536743,"connectStart":449.40000009536743,"domainLookupEnd":449.40000009536743,"domainLookupStart":449.40000009536743,"fetchStart":449.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":449.40000009536743,"responseEnd":891,"responseStart":891,"secureConnectionStart":449.40000009536743},{"duration":872.5,"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":461.5,"connectEnd":461.5,"connectStart":461.5,"domainLookupEnd":461.5,"domainLookupStart":461.5,"fetchStart":461.5,"redirectEnd":0,"redirectStart":0,"requestStart":461.5,"responseEnd":1334,"responseStart":1334,"secureConnectionStart":461.5},{"duration":104.5,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":787.2999997138977,"connectEnd":787.2999997138977,"connectStart":787.2999997138977,"domainLookupEnd":787.2999997138977,"domainLookupStart":787.2999997138977,"fetchStart":787.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":787.2999997138977,"responseEnd":891.7999997138977,"responseStart":891.7999997138977,"secureConnectionStart":787.2999997138977},{"duration":292.40000009536743,"initiatorType":"link","name":"https://jira.mariadb.org/s/d5715adaadd168a9002b108b2b039b50-CDN/lu2cib/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":1124.2999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":1124.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1416.6999998092651,"responseStart":0,"secureConnectionStart":0},{"duration":411.09999990463257,"initiatorType":"script","name":"https://jira.mariadb.org/s/d41d8cd98f00b204e9800998ecf8427e-CDN/lu2cib/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":1125.5999999046326,"connectEnd":1125.5999999046326,"connectStart":1125.5999999046326,"domainLookupEnd":1125.5999999046326,"domainLookupStart":1125.5999999046326,"fetchStart":1125.5999999046326,"redirectEnd":0,"redirectStart":0,"requestStart":1125.5999999046326,"responseEnd":1536.6999998092651,"responseStart":1536.6999998092651,"secureConnectionStart":1125.5999999046326}],"fetchStart":0,"domainLookupStart":0,"domainLookupEnd":0,"connectStart":0,"connectEnd":0,"requestStart":193,"responseStart":364,"responseEnd":462,"domLoading":368,"domInteractive":1496,"domContentLoadedEventStart":1496,"domContentLoadedEventEnd":1569,"domComplete":2321,"loadEventStart":2321,"loadEventEnd":2323,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1468.5},{"name":"bigPipe.sidebar-id.end","time":1469.2999997138977},{"name":"bigPipe.activity-panel-pipe-id.start","time":1469.5},{"name":"bigPipe.activity-panel-pipe-id.end","time":1476.1999998092651},{"name":"activityTabFullyLoaded","time":1580.9000000953674}],"measures":[],"correlationId":"235a01958708d6","effectiveType":"4g","downlink":9.2,"rtt":0,"serverDuration":89,"dbReadsTimeInMs":10,"dbConnsTimeInMs":17,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}