SQL Server performance for alter table alter column change data type
最近在搞一个升级脚本,发现有一张业务表中的某些字段长度需要调整,直接使用alter table alter column进行修改发现修改一列要用十几分钟!!!两三个列那用时简直不能容忍啊!google了一下,相关链接参考文末stackoverflow问答。
测试环境配置
- 表中数据量100多万;
- 虚拟机4核16G内存400G硬盘;
优化步骤
- 1.删除修改列关联的index和constraints;
- 2.添加一个新的可空列,类型使用你要修改的类型;
- 3.用要修改的列update新增加的这个列(多个列时节省时间更可观);
- 4.delete要修改的列;
- 5.rename第2步中所见的新列;
简单示例
alter table myTable add myNewColumn int null;
while 1=1
begin
update top (100000) myTable
set
myNewColumn = myOldColumn
where
myNewColumn is null;
if @@ROWCOUNT = 0 break;
end
alter table myTable drop column myOldColumn;
EXEC sp_RENAME '[myTable].[myNewColumn]', 'myOldColumn', 'COLUMN'
Stackoverflow相关链接
- https://stackoverflow.com/questions/10754665/change-column-types-in-a-huge-table
- https://stackoverflow.com/questions/4311559/sql-server-performance-for-alter-table-alter-column-change-data-type
SQL Server performance for alter table alter column change data type的更多相关文章
- SQL Server审计功能入门:CDC(Change Data Capture)
原文:SQL Server审计功能入门:CDC(Change Data Capture) 介绍 SQL Server 2008引入了CDC(Change Data Capture),它能记录: 1. ...
- MSSql Server 索引'uq_f_userName' 依赖于 列'f_userName'。由于一个或多个对象访问此列,ALTER TABLE ALTER COLUMN f_userName 失败
--需求有变,需要往t_login表的f_userName字段添加外国人名,之前设置的varchar(10)不够,商议决定改成varchar(30),执行的时候,提示消息 索引'uq_f_userNa ...
- [Hive - LanguageManual] Alter Table/Partition/Column
Alter Table/Partition/Column Alter Table Rename Table Alter Table Properties Alter Table Comment Add ...
- Oracle alter table modify column Syntax example
http://www.dba-oracle.com/t_alter_table_modify_column_syntax_example.htm For complete tips on Oracle ...
- SQL Server performance tips
Refer to: http://harriyott.com/2006/01/sql-server-performance-tips A colleague of mine has been look ...
- SQL Server审计功能入门:更改跟踪(Change Tracking)
原文:SQL Server审计功能入门:更改跟踪(Change Tracking) 介绍 更改跟踪是一种轻量型解决方案,它为应用程序提供了一种有效的更改跟踪机制.常规的,自定义变更跟踪和读取跟踪数据, ...
- faster alter table add column
Create a new table (using the structure of the current table) with the new column(s) included. execu ...
- MySQL ALTER TABLE: ALTER vs CHANGE vs MODIFY COLUMN
ALTER COLUMN 语法: ALTER [COLUMN] col_name {SET DEFAULT literal | DROP DEFAULT} 作用: 设置或删除列的默认值.该操作会直接修 ...
- 转载:SQL Server 2008-建立分区表(Table Partition) 转载
数据库结构和索引的是否合理在很大程度上影响了数据库的性能,但是随着数据库信息负载的增大,对数据库的性能也发生了很大的影响.可能我们的数据库在一开始有着很高的性能,但是随着数据存储量的急速增长—例如订单 ...
随机推荐
- POJ 1141 括号匹配 DP
黑书原题 区间DP,递归输出 不看Discuss毁一生 (woc还真有空串的情况啊) //By SiriusRen #include <cstdio> #include <cstri ...
- 线程1—Runnable
随便选择两个城市作为预选旅游目标.实现两个独立的线程分别显示10次城市名,每次显示后休眠一段随机时间(1000ms以内),哪个先显示完毕,就决定去哪个城市.分别用Runnable接口和Thread类实 ...
- HDU 4786Fibonacci Tree(最小生成树)
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission( ...
- 1355: [Baltic2009]Radio Transmission
Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 958 Solved: 659[Submit][Status][Discuss] Description ...
- hiho149周 - 数据结构 trie树
题目链接 坑点:accept和deny的ip可能相同,需加个判断 #include <cstdio> #include <cstdlib> #include <vecto ...
- Monad的基本运算
A monad is created by defining a type constructor M and two operations, bind and return (where retur ...
- 图像处理是用的数据类型uint8,double
将原图像的灰度值转换成double的作用主要是考虑计算过程中的精度的问题,double 的数据是有小数点的,而uint8是0-255的整数,如果直接用uint8计算,会在计算过程中产生舍入误差,这种误 ...
- Linux 中挂载 ISO 文件
在 Linux 中挂载 ISO 文件 用 mount 命令,在终端中输入如下命令即可: sudo mount -o loop filename.iso /cdrom 其中 filename.iso 是 ...
- ArcGIS api for javascript——地图配置-定制缩放动画
描述 本例展示了当用户放大或缩小地图时如何定义地图的动画.zoomDuration和zoomRate是Dojo动画属性,他们确定了动画的duration和帧刷新的rate .这些属性单位是毫秒,zoo ...
- hdu 1423 最长公共递增子序列 LCIS
最长公共上升子序列(LCIS)的O(n^2)算法 预备知识:动态规划的基本思想,LCS,LIS. 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列). 首先我们可以看到,这个问题具有相 ...