一个表根据另一个表的查询结果删除数据,怎么操作, sql 咋写效率比较高?

2019-02-18 18:48:08 +08:00
 albb99

现有 A、B 两个表,A 是用户表(100 万条),B 是绑定设备的表(50 万条),现在要删去过期用户的绑定设备 按正常操作, 直接用 delete from B where B.userid in (select userid from A where 用户会员过期) 这个语句删除会超时,请问怎么操作,sql 咋写效率比较高?

3600 次点击
所在节点    MySQL
7 条回复
roscoecheung1993
2019-02-18 18:59:48 +08:00
sql 不精,yy 一下,用表连接会不会好些? where B.userid = A.userId and 用户会员过期
lzz2394677796
2019-02-18 19:01:06 +08:00
delete from B where B.userid in (select userid from (select userid from A where 用户会员过期) as tmp)
效率百倍?!
sunnyadamm
2019-02-18 19:08:16 +08:00
加索引,left join,几千条一提交
des
2019-02-18 19:43:03 +08:00
MySQL 的 where in 效率不好,特别是子查询的时候
这个好多人说过了
qa2080639
2019-02-18 19:46:56 +08:00
in 里面子查询 不使用索引 会很慢
c6h6benzene
2019-02-18 19:56:03 +08:00
试试 inner join ?
xuanbg
2019-02-18 20:22:47 +08:00
delete b
from b
join a on a.userid = B.userid
where a.用户会员过期

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/536249

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX