› 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 · Feb 18, 2019 · 4489 views
    This topic created in 2785 days ago, the information mentioned may be changed or developed.

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

    7 replies  •  2019-02-18 20:22:47 +08:00
    roscoecheung1993
        1
    roscoecheung1993  
       Feb 18, 2019
    sql 不精,yy 一下,用表连接会不会好些? where B.userid = A.userId and 用户会员过期
    Ehj8PSYA
        2
    Ehj8PSYA  
       Feb 18, 2019 via iPhone
    delete from B where B.userid in (select userid from (select userid from A where 用户会员过期) as tmp)
    效率百倍?!
    sunnyadamm
        3
    sunnyadamm  
       Feb 18, 2019 via Android
    加索引,left join,几千条一提交
    des
        4
    des  
       Feb 18, 2019 via Android
    MySQL 的 where in 效率不好,特别是子查询的时候
    这个好多人说过了
    qa2080639
        5
    qa2080639  
       Feb 18, 2019
    in 里面子查询 不使用索引 会很慢
    c6h6benzene
        6
    c6h6benzene  
       Feb 18, 2019 via iPhone
    试试 inner join ?
    xuanbg
        7
    xuanbg  
       Feb 18, 2019
    delete b
    from b
    join a on a.userid = B.userid
    where a.用户会员过期
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2163 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 46ms · UTC 00:49 · PVG 08:49 · LAX 17:49 · JFK 20:49
    ♥ Do have faith in what you're doing.