一个表根据另一个表的查询结果删除数据,怎么操作, sql 咋写效率比较高? - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
MySQL 5.5 Community Server
MySQL 5.6 Community Server
Percona Configuration Wizard
XtraBackup 搭建主从复制
Great Sites on MySQL
Percona
MySQL Performance Blog
Severalnines
推荐管理工具
Sequel Pro
phpMyAdmin
推荐书目
MySQL Cookbook
MySQL 相关项目
MariaDB
Drizzle
参考文档
http://mysql-python.sourceforge.net/MySQLdb.html
albb99
V2EX    MySQL

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

  •  
  •   albb99 2019-02-18 18:48:08 +08:00 4035 次点击
    这是一个创建于 2504 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    7 条回复    2019-02-18 20:22:47 +08:00
    roscoecheung1993
        1
    roscoecheung1993  
       2019-02-18 18:59:48 +08:00
    sql 不精,yy 一下,用表连接会不会好些? where B.userid = A.userId and 用户会员过期
    lzz2394677796
        2
    lzz2394677796  
       2019-02-18 19:01:06 +08:00 via iPhone
    delete from B where B.userid in (select userid from (select userid from A where 用户会员过期) as tmp)
    效率百倍?!
    sunnyadamm
        3
    sunnyadamm  
       2019-02-18 19:08:16 +08:00 via Android
    加索引,left join,几千条一提交
    des
        4
    des  
       2019-02-18 19:43:03 +08:00 via Android
    MySQL 的 where in 效率不好,特别是子查询的时候
    这个好多人说过了
    qa2080639
        5
    qa2080639  
       2019-02-18 19:46:56 +08:00
    in 里面子查询 不使用索引 会很慢
    c6h6benzene
        6
    c6h6benzene  
       2019-02-18 19:56:03 +08:00 via iPhone
    试试 inner join ?
    xuanbg
        7
    xuanbg  
       2019-02-18 20:22:47 +08:00
    delete b
    from b
    join a on a.userid = B.userid
    where a.用户会员过期
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2733 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 30ms UTC 13:17 PVG 21:17 LAX 05:17 JFK 08:17
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86