On tables with foreign key on delete cascade, the data is kept when the referenced item is deleted. However it cannot be deleted or altered and on optimize on the table the data is suddenly gone.
test case:
create table t1 ( id int unsigned auto_increment, primary key(id) ) engine=innodb;
|
create table t2 ( t2id int unsigned, id int unsigned, primary key(t2id, id), foreign key (`id`) references t1(`id`) on delete cascade ) engine=innodb;
|
|
insert into t1 values (1);
|
insert into t2 values (1,1);
|
|
select * from t1;
|
+----+
|
| id |
|
+----+
|
| 1 |
|
+----+
|
1 row in set (0.00 sec)
|
|
select * from t2;
|
+------+----+
|
| t2id | id |
|
+------+----+
|
| 1 | 1 |
|
+------+----+
|
1 row in set (0.00 sec)
|
|
delete from t1;
|
Query OK, 1 row affected (0.00 sec)
|
|
select * from t2;
|
+------+----+
|
| t2id | id |
|
+------+----+
|
| 1 | 1 |
|
+------+----+
|
1 row in set (0.00 sec)
|
|
delete from t2;
|
Query OK, 0 rows affected (0.00 sec)
|
|
select * from t2;
|
+------+----+
|
| t2id | id |
|
+------+----+
|
| 1 | 1 |
|
+------+----+
|
1 row in set (0.00 sec)
|
|
optimize table t2;
|
+----------------------+----------+----------+-------------------------------------------------------------------+
|
| Table | Op | Msg_type | Msg_text |
|
+----------------------+----------+----------+-------------------------------------------------------------------+
|
| xxxxxxx_xxxxxxxxx.t2 | optimize | note | Table does not support optimize, doing recreate + analyze instead |
|
| xxxxxxx_xxxxxxxxx.t2 | optimize | status | OK |
|
+----------------------+----------+----------+-------------------------------------------------------------------+
|
2 rows in set (0.04 sec)
|
|
select * from t2;
|
Empty set (0.00 sec)
|
|
{"report":{"fcp":986.0999999046326,"ttfb":205.59999990463257,"pageVisibility":"visible","entityId":60889,"key":"jira.project.issue.view-issue","isInitial":true,"threshold":1000,"elementTimings":{},"userDeviceMemory":8,"userDeviceProcessors":64,"apdex":0.5,"journeyId":"c12a76b8-6163-4427-8223-72ac841969e6","navigationType":0,"readyForUser":1072.9000000953674,"redirectCount":0,"resourceLoadedEnd":1762.7999997138977,"resourceLoadedStart":214.90000009536743,"resourceTiming":[{"duration":264,"initiatorType":"link","name":"https://jira.mariadb.org/s/2c21342762a6a02add1c328bed317ffd-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/css/_super/batch.css","startTime":214.90000009536743,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":214.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":478.90000009536743,"responseStart":0,"secureConnectionStart":0},{"duration":264,"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":215.19999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":215.19999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":479.19999980926514,"responseStart":0,"secureConnectionStart":0},{"duration":273.40000009536743,"initiatorType":"script","name":"https://jira.mariadb.org/s/0917945aaa57108d00c5076fea35e069-CDN/lu2cib/820016/12ta74/0a8bac35585be7fc6c9cc5a0464cd4cf/_/download/contextbatch/js/_super/batch.js?locale=en","startTime":215.2999997138977,"connectEnd":215.2999997138977,"connectStart":215.2999997138977,"domainLookupEnd":215.2999997138977,"domainLookupStart":215.2999997138977,"fetchStart":215.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":215.2999997138977,"responseEnd":488.69999980926514,"responseStart":488.69999980926514,"secureConnectionStart":215.2999997138977},{"duration":376.90000009536743,"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":215.5,"connectEnd":215.5,"connectStart":215.5,"domainLookupEnd":215.5,"domainLookupStart":215.5,"fetchStart":215.5,"redirectEnd":0,"redirectStart":0,"requestStart":215.5,"responseEnd":592.4000000953674,"responseStart":592.4000000953674,"secureConnectionStart":215.5},{"duration":380.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":215.7999997138977,"connectEnd":215.7999997138977,"connectStart":215.7999997138977,"domainLookupEnd":215.7999997138977,"domainLookupStart":215.7999997138977,"fetchStart":215.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":215.7999997138977,"responseEnd":596.0999999046326,"responseStart":596.0999999046326,"secureConnectionStart":215.7999997138977},{"duration":380.69999980926514,"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":215.90000009536743,"connectEnd":215.90000009536743,"connectStart":215.90000009536743,"domainLookupEnd":215.90000009536743,"domainLookupStart":215.90000009536743,"fetchStart":215.90000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":215.90000009536743,"responseEnd":596.5999999046326,"responseStart":596.5999999046326,"secureConnectionStart":215.90000009536743},{"duration":381,"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":216.09999990463257,"connectEnd":216.09999990463257,"connectStart":216.09999990463257,"domainLookupEnd":216.09999990463257,"domainLookupStart":216.09999990463257,"fetchStart":216.09999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":216.09999990463257,"responseEnd":597.0999999046326,"responseStart":597.0999999046326,"secureConnectionStart":216.09999990463257},{"duration":438.2000002861023,"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":216.2999997138977,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":216.2999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":654.5,"responseStart":0,"secureConnectionStart":0},{"duration":381.19999980926514,"initiatorType":"script","name":"https://jira.mariadb.org/rest/api/1.0/shortcuts/820016/47140b6e0a9bc2e4913da06536125810/shortcuts.js?context=issuenavigation&context=issueaction","startTime":216.40000009536743,"connectEnd":216.40000009536743,"connectStart":216.40000009536743,"domainLookupEnd":216.40000009536743,"domainLookupStart":216.40000009536743,"fetchStart":216.40000009536743,"redirectEnd":0,"redirectStart":0,"requestStart":216.40000009536743,"responseEnd":597.5999999046326,"responseStart":597.5999999046326,"secureConnectionStart":216.40000009536743},{"duration":437.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":216.69999980926514,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":216.69999980926514,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":654.5999999046326,"responseStart":0,"secureConnectionStart":0},{"duration":381.40000009536743,"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":216.7999997138977,"connectEnd":216.7999997138977,"connectStart":216.7999997138977,"domainLookupEnd":216.7999997138977,"domainLookupStart":216.7999997138977,"fetchStart":216.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":216.7999997138977,"responseEnd":598.1999998092651,"responseStart":598.1999998092651,"secureConnectionStart":216.7999997138977},{"duration":716.3000001907349,"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":223.59999990463257,"connectEnd":223.59999990463257,"connectStart":223.59999990463257,"domainLookupEnd":223.59999990463257,"domainLookupStart":223.59999990463257,"fetchStart":223.59999990463257,"redirectEnd":0,"redirectStart":0,"requestStart":223.59999990463257,"responseEnd":939.9000000953674,"responseStart":939.9000000953674,"secureConnectionStart":223.59999990463257},{"duration":1526,"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":236.7999997138977,"connectEnd":236.7999997138977,"connectStart":236.7999997138977,"domainLookupEnd":236.7999997138977,"domainLookupStart":236.7999997138977,"fetchStart":236.7999997138977,"redirectEnd":0,"redirectStart":0,"requestStart":236.7999997138977,"responseEnd":1762.7999997138977,"responseStart":1762.7999997138977,"secureConnectionStart":236.7999997138977},{"duration":277.69999980926514,"initiatorType":"xmlhttprequest","name":"https://jira.mariadb.org/rest/webResources/1.0/resources","startTime":665.9000000953674,"connectEnd":665.9000000953674,"connectStart":665.9000000953674,"domainLookupEnd":665.9000000953674,"domainLookupStart":665.9000000953674,"fetchStart":665.9000000953674,"redirectEnd":0,"redirectStart":0,"requestStart":665.9000000953674,"responseEnd":943.5999999046326,"responseStart":943.5,"secureConnectionStart":665.9000000953674},{"duration":826.4000000953674,"initiatorType":"script","name":"https://www.google-analytics.com/analytics.js","startTime":978.5,"connectEnd":0,"connectStart":0,"domainLookupEnd":0,"domainLookupStart":0,"fetchStart":978.5,"redirectEnd":0,"redirectStart":0,"requestStart":0,"responseEnd":1804.9000000953674,"responseStart":0,"secureConnectionStart":0}],"fetchStart":1,"domainLookupStart":1,"domainLookupEnd":1,"connectStart":1,"connectEnd":1,"requestStart":28,"responseStart":206,"responseEnd":237,"domLoading":213,"domInteractive":1809,"domContentLoadedEventStart":1809,"domContentLoadedEventEnd":1869,"domComplete":2411,"loadEventStart":2411,"loadEventEnd":2412,"userAgent":"Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)","marks":[{"name":"bigPipe.sidebar-id.start","time":1768.0999999046326},{"name":"bigPipe.sidebar-id.end","time":1769},{"name":"bigPipe.activity-panel-pipe-id.start","time":1769.1999998092651},{"name":"bigPipe.activity-panel-pipe-id.end","time":1773.7999997138977},{"name":"activityTabFullyLoaded","time":1887.0999999046326}],"measures":[],"correlationId":"7217c78fda8ac0","effectiveType":"4g","downlink":9.6,"rtt":0,"serverDuration":107,"dbReadsTimeInMs":9,"dbConnsTimeInMs":18,"applicationHash":"9d11dbea5f4be3d4cc21f03a88dd11d8c8687422","experiments":[]}}
Thanks for the report. I assume you have query cache enabled.
MTR test case
--source include/have_innodb.inc
--connect (con1,localhost,root,,)
--disconnect con1
--connection default
Actual result on 10.2
delete from t1;
select * from t2;
t2id id
1 1
optimize table t2;
Table Op Msg_type Msg_text
test.t2 optimize note Table does not support optimize, doing recreate + analyze instead
test.t2 optimize status OK
select * from t2;
t2id id
Not reproducible on 10.1.
Not reproducible when query cache is disabled.