zoukankan      html  css  js  c++  java
  • mysql大表修改结构

    参考原理依据:

    http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#alter-table

    ALTER TABLE运行时会对原表进行临时复制,在副本上进行更改,然后删除原表,再对新表进行重命名。在执行ALTER TABLE时,其它用户可以阅读原表,但是对表的更新和修改的操作将被延迟,直到新表生成为止。新表生成后,这些更新和修改信息会自动转移到新表上。

    注意,如果您在执行ALTER TABLE时使用除了RENAME以外的选项,则MySQL会创建一个临时表。即使数据并不需要进行复制(例如当您更改列的名称时),MySQL也会这么操作。对于MyISAM表,您可以通过把myisam_sort_buffer_size系统变量设置到一个较高的值,来加快重新创建索引(该操作是变更过程中速度最慢的一部分)的速度。

    实际问题,对一个上亿数据的表增加字段

    实际操作:

    #####
    create table ppy_myfishinstanceone_bak like ppy_myfishinstanceone;

    alter table ppy_myfishinstanceone_bak add column `top_num` int(10) DEFAULT 0;

    insert into ppy_myfishinstanceone_bak(member_id,my_fish_tank_id,fish_id,hungry,life,exp,last_calculated,last_potion_used)
    select member_id,my_fish_tank_id,fish_id,hungry,life,exp,last_calculated,last_potion_used
    from ppy_myfishinstanceone;

    drop table ppy_myfishinstanceone;

    rename table ppy_myfishinstanceone_bak to ppy_myfishinstanceone;
    #####

  • 相关阅读:
    20210110-正则表达式
    20210105
    C# Expression 树转化为SQL语句(一)
    5000行js db
    Keras智能
    nginx 设置多个tcp IP代理 socket 转发
    FTP连接时出现“227 Entering Passive Mode” 的解决方法
    windows nginx TCP代理 负载均衡
    nginx 代理ftp
    Intellij IDEA添加项目依赖
  • 原文地址:https://www.cnblogs.com/imouren/p/2837353.html
Copyright © 2011-2022 走看看