• 【MySQL】使用硬链接的方式删除大表


    Intro

    MySQL中删除比较大的表时,如果直接用drop table的方式进行删除,有可能会对整个实例产生影响甚至使得实例夯住。因此可以通过硬链接的方式对表进行删除,使得对生产环境的影响降到最低。

    drop table 的过程

    1. 持有 buffer pool mutex;
    2. 持有 buffer pool 中的 flush list mutex;
    3. 开始扫描 LRU list:
      1. 如果 dirty page 属于 drop table,那么就直接从 LRU list 中移除;
      2. 如果删除的 page 个数超过了define buf_lru_drop_search_size 1024的话,则释放 buffer pool mutex 和 flush list mutex ,强制通过 pthread_yield 进行一次 os context switch ,释放 cpu 时间片;
      3. 重新持有 buffer pool mutex 和 flush list mutex,继续遍历 LRU list,直到 LRU 的表头。
    4. 释放 flush list mutex;
    5. 释放 buffer pool mutex。
    6. 再次重复上述的 1-5 步骤,只不过 1-5 是删除 dirty page,这次的重复执行,删除的是 buffer pool 中的 clean page。

    简单来看,整个过程可以简化为:

    1. 获取 buffer pool mutex 和 flush list mutex;
    2. 从尾部开始遍历 LRU 链表;
    3. 如果是 dirty page,那么将 dirty page 置为 clean page,并从 flush list 中删除;
    4. 然后进行第二次遍历 LRU,将 page 从 LRU 中移动到 free list 中;
    5. 释放 buffer pool mutex 和 flush list mutex。

    在整个删除表的过程中,持有了 buffer pool mutex 和 flush list mutex ,如果整个 buffer pool 比较大,或者表有较多的脏页,那么持有锁的时间会比较长,导致其他事务在用到这个 buffer pool 的时候被阻塞,现象上来看就是这个实例被夯住。

    硬链接删除表

    1. 主库和从库上对表建立硬链接

      ln table_1.ibd table_1.ibd.hdlk
      ln table_1.frm table_1.frm.hdlk
      
    2. 在主库进行 drop table

      drop table table_1;
      
    3. 在 os 层删除物理文件

      rm table_1.ibd.hdlk
      rm table_1.frm.hdlk
      
    4. 如果表达到 500G 或者上 TB,则可以用 truncate 命令进行截断删除

      truncate -s 2G table_1.ibd.hdlk
      
  • 相关阅读:
    IT认证一一看过来
    SQL Server连接中三个常见的错误分析
    解决SFTP时,NetBeans恼人的RSA提示
    Mixing Integrated Authentication and Anonymous Authentication with PreAuthenticated = true doesn’t work
    一段扫flash跨站的脚本
    图解用WAS对Web服务器进行压力测试
    Google TrustRank与Hilltop算法
    Stupid smart code
    Archlinux桌面配置指南
    TSVNCache占用CPU的解决办法
  • 原文地址:https://www.cnblogs.com/haohaozhang/p/12236174.html
Copyright © 2020-2023  润新知