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
cirton
V2EX  ›  MySQL

为什么这条语句表会被锁?

  •  
  •   cirton · 2021-01-15 14:44:06 +08:00 · 1879 次点击
    这是一个创建于 1190 天前的主题,其中的信息可能已经有所发展或是发生改变。

    insert into a 表 select * from b 表 where not exists ( select 1 from c 表 where .... );

    执行这条语句,发现 c 表 被锁了?

    为什么一张查询的关联表会被锁呢?

    数据引擎是 innodb

    4 条回复    2021-04-21 09:13:15 +08:00
    luoyou1014
        1
    luoyou1014  
       2021-01-15 16:07:54 +08:00
    C 表不锁,B 表会出现幻读。
    cirton
        2
    cirton  
    OP
       2021-01-15 16:22:57 +08:00
    @luoyou1014 可以在更新 a 表时不锁 c 表吗?
    现在更新 a 表时,c 表就会被锁导致业务暂停。对 mysql 不太熟,一般怎么处理这种情况呢?
    qiayue
        3
    qiayue  
       2021-01-15 16:29:40 +08:00
    不要用一条语句完成这个业务,用程序来完成
    512357301
        4
    512357301  
       2021-04-21 09:13:15 +08:00 via Android
    @cirton c 表不锁的话,如果 c 表在语句执行中更新了怎么办,以更新前为准还是更新后为准
    还有就是尽量不要在 where 中用子查询,我个人比较反感这种写法,有些场景下执行效率低,写起来也麻烦,唯一的好处就是语句逻辑比较直观
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2748 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 12:28 · PVG 20:28 · LAX 05:28 · JFK 08:28
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.