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 · 3574 次点击
    这是一个创建于 1865 天前的主题,其中的信息可能已经有所发展或是发生改变。

    现有 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   ·   我们的愿景   ·   实用小工具   ·   1396 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:33 · PVG 01:33 · LAX 10:33 · JFK 13:33
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.