TiDB 4.0是否支持关联表删除

  • 【TiDB 版本】:4.0.0
  • 【问题描述】:目前使用中有条件删除临时表的情况,
    如果SQL写成delete a from tableA a inner join tableB b on a.col1 = b.col1这种关联表删除的形式,虽然tidb认为语法正确,但执行后影响行数为0,show warnings没有错误,
    但如果改为delete from tableA where Id in (select Id from tableB),执行计划中A表一定是全表扫描
    请问下这种关联删除从2.0之后还是不支持吗,或者这种删除有没有更好的优化方式?

2.0 是否还不支持什么意思

select count(*) from tableA a inner join tableB b on a.col1 = b.col1;
看下返回结果

在 tidb 事务要求范围内,可以直接删除,如果删除区间较大建议分批循环删除,减少对 tidb 集群的影响

因为我搜到2.0也有人问这个,当时的回答是研发团队确认不支持该操作。
几十万数据,肯定没超过tidb DML的事务10G限制,如果不考虑分批是否没有别的办法了

上面 select count 是否符合删除区间的预期呢?

create table tmpa (id int primary key);
create table tmpb (id int primary key);

insert into tmpa(id) values (1),(2);
insert into tmpb(id) values (1),(3);

select count(1) from tmpa a join tmpb b on a.id = b.id
– Affected rows: 1

delete a from tmpa a join tmpb b on a.id = b.id
– Affected rows: 0

看起来关联查询能够查出来1条,删除的时候就影响行数0,实际也没有删除掉,是暂时不支持这种语法么…

测试版本是 v4.0.1 ,select 看下原始数据?

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@172.16.4.107 : (none) 11:35:25>use test
Database changed
root@172.16.4.107 : test 11:35:30>insert into tmpb(id) values (1),(3);
ERROR 1146 (42S02): Table 'test.tmpb' doesn't exist
root@172.16.4.107 : test 11:35:31>create table tmpa (id int primary key);
Query OK, 0 rows affected (0.57 sec)

root@172.16.4.107 : test 11:35:38>create table tmpb (id int primary key);
Query OK, 0 rows affected (1.03 sec)

root@172.16.4.107 : test 11:35:40>
root@172.16.4.107 : test 11:35:40>insert into tmpa(id) values (1),(2);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

root@172.16.4.107 : test 11:35:40>insert into tmpb(id) values (1),(3);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

root@172.16.4.107 : test 11:35:40>select count(1) from tmpa a join tmpb b on a.id = b.id
    -> ;
+----------+
| count(1) |
+----------+
|        1 |
+----------+
1 row in set (0.01 sec)

root@172.16.4.107 : test 11:35:49>delete a from tmpa a join tmpb b on a.id = b.id
    -> 
    -> ;
Query OK, 1 row affected (0.01 sec)

root@172.16.4.107 : test 11:35:55>select count(1) from tmpa a join tmpb b on a.id = b.id;
+----------+
| count(1) |
+----------+
|        0 |
+----------+
1 row in set (0.00 sec)

root@172.16.4.107 : test 11:36:02>select * from tmpa ;
+----+
| id |
+----+
|  2 |
+----+
1 row in set (0.01 sec)

root@172.16.4.107 : test 11:36:14>select * from tmpb ;
+----+
| id |
+----+
|  1 |
|  3 |
+----+
2 rows in set (0.01 sec)

root@172.16.4.107 : test 11:36:16>

tidb 版本4.0.0