• 记mysql Insert into select语句问题


    问题:

    由于数据数据库中 order_today 数据量过大,当时好像有 700W 了,并且每天在以 30W 的速度增加。

    所以要 将 order_today 内的部分数据迁移到 order_record 中,并将 order_today 中的数据删除,这样来降低 order_today 表中的数据量。


    错误操作:

    在默认的事务隔离级别下:insert into order_record select * from order_today 加锁规则是:order_record 表锁,order_today 逐步锁(扫描一个锁一个)。
    通过观察迁移 SQL 的执行情况你会发现 order_today 是全表扫描,也就意味着在执行 insert into select from 语句时,MySQL 会从上到下扫描 order_today 内的记录并且加锁,这样一来不就和直接锁表是一样了。

    正确操作:

    由于查询条件会导致 order_today 全表扫描,什么能避免全表扫描呢,很简单嘛,给 pay_success_time 字段添加一个 idx_pay_suc_time 索引就可以了。

    总结:

    使用 insert into tablA select * from tableB 语句时,一定要确保 tableB 后面的 where,order 或者其他条件,都需要有对应的索引,来避免出现 tableB 全部记录被锁定的情况。

  • 相关阅读:
    Spring Boot 使用Redis
    openTSDB(转)
    httpClient 超时时间设置(转)
    HTTPClient 超时链接设置
    入坑python 自己写的小工具,纪念一下
    Linux下SVN创建新的项目
    java对象数组的概述和使用
    解决fastDFS客户端连接超时问题
    显示目录结构
    centos7开启80和8080端口
  • 原文地址:https://www.cnblogs.com/liiu/p/12850886.html
Copyright © 2020-2023  润新知