oracle11g alter table set unused column指定表某列不可用之系列一

bitifi發表於2015-11-04
<h2> 結論 </h2> 1,alter table set unused column指定表的某列為不可用<br /> 2,alter table set unused column生效後不可用列已經不再屬於表,但空間不會釋放,需要重組方可釋放<br /> 3,alter table set unused column的資訊可用user_unused_col_tabs查詢<br /> 4,alter table set unused column適用於高併發OLTP環境,在業務繁忙時,先執行此語句,待業務壓力小時,然後用alter table drop unused columns真正刪除不可用的列;<br /> &nbsp; alter table drop column刪除列消耗的時間明顯要高於alter table set unused column<br /> 5,alter table set unused column指定某個LONG列不可用,不能新增其它LONG列<br /> 6,同一個表中不能同時存在2個LONG,其它LOB列不受其限制<br /> 7,透過alter table set unsed(表中列的列表,以逗號分割),可以同時指定多個列不可用<br /> 8, alter table set unused column cascade constraints配置某列不可用會遞迴刪除定義的約束,發現指不指定cascade constraints選項都會遞迴刪除其約束<br /> 9, 透過alter table set unsed(表中列的列表,以逗號分割),可以同時指定多個列不可用<br /> <br /> <br /> <h2> 測試 </h2> 1,資料庫版本<br /> SQL&gt; select * from v$version where rownum=1;<br /> <br /> <br /> BANNER<br /> --------------------------------------------------------------------------------<br /> Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production<br /> <br /> <br /> 2,獲取暫時不使用的列的表資訊<br /> SQL&gt; select count(*) from user_unused_col_tabs;<br /> <br /> <br /> &nbsp; COUNT(*)<br /> ----------<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;0<br /> <br /> <br /> 3,建立某個測試表<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int,c int);<br /> <br /> <br /> Table created. &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<br /> <br /> <br /> <br /> <br /> 4,配置指定列為不可用<br /> <br /> <br /> 查閱官方手冊,配置表的列為不可用,其語法請參考drop_column_clause 節<br /> SQL&gt; alter table t_unused_col set unused column b;&nbsp;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column c;&nbsp;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select count(*) from user_unused_col_tabs;<br /> <br /> <br /> &nbsp; COUNT(*)<br /> ----------<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br /> 5,配置指定不可用後不再顯示其列<br /> SQL&gt; desc user_unused_col_tabs;<br /> &nbsp;Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Null? &nbsp; &nbsp;Type<br /> &nbsp;----------------------------------------- -------- ----------------------------<br /> &nbsp;TABLE_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NOT NULL VARCHAR2(30)<br /> &nbsp;COUNT &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;NUMBER<br /> <br /> <br /> 6,可以在如下字典查詢到不可用列的相關資訊<br /> SQL&gt; select table_name,count from user_unused_col_tabs;<br /> <br /> <br /> TABLE_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;COUNT<br /> ------------------------------------------------------------ ----------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;2<br /> <br /> <br /> SQL&gt; insert into t_unused_col values(1,1,1);<br /> insert into t_unused_col values(1,1,1)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; *<br /> ERROR at line 1:<br /> ORA-00913: too many values<br /> <br /> <br /> <br /> <br /> SQL&gt; insert into t_unused_col values(1);<br /> <br /> <br /> 1 row created.<br /> <br /> <br /> SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> SQL&gt; select * from t_unused_col;<br /> <br /> <br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;A<br /> ----------<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;1<br /> <br /> <br /> 7,刪除測試表中不可用的列<br /> SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 8,重新新增B與C列<br /> SQL&gt; alter table t_unused_col add b int;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add c int;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 9,配置某列為不可用後,可以新增同名的列<br /> SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add b int;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 10,配置某列為不可用,其列佔用的空間不會釋放<br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int,c int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; insert into t_unused_col select level,level,level from dual connect by level&lt;=100000;<br /> <br /> <br /> 100000 rows created.<br /> <br /> <br /> SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br /> <br /> <br /> 11,刪除表中不可用的列後,其空間仍不會釋放<br /> SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3<br /> <br /> <br /> 12,只有重組表後,才會釋放刪除不可用列的空間<br /> SQL&gt; alter table t_unused_col move;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select segment_name,bytes/1024/1024 from user_segments where segment_name='T_UNUSED_COL';<br /> <br /> <br /> SEGMENT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; BYTES/1024/1024<br /> ------------------------------ ---------------<br /> T_UNUSED_COL &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2<br /> <br /> <br /> 13,配置某個LONG型別的列為不可用後,必須刪除其列後方可新增新的LONG列<br /> SQL&gt; alter table t_unused_col add b clob;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add x long;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column x;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add y long;<br /> alter table t_unused_col add y long<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br /> ERROR at line 1:<br /> ORA-01754: a table may<br /> contain only one column of<br /> type LONG<br /> <br /> <br /> <br /> <br /> SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add y long;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b long,c long);<br /> create table t_unused_col(a int,b long,c long)<br /> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;*<br /> ERROR at line 1:<br /> ORA-01754: a table may<br /> contain only one column of<br /> type LONG<br /> <br /> <br /> 14,可見alter table drop columns消耗的時間明顯要大於alter table set unused columns,所以後者適用於高併發的OLTP環境,減少鎖持有情況發生;<br /> 到業務壓力不大時,可以採用alter table drop unused columns<br /> SQL&gt; create table t_unused_col(a int,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; insert into t_unused_col select level,level from dual connect by level&lt;=1000000;<br /> <br /> <br /> 1000000 rows created.<br /> <br /> <br /> SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> <br /> <br /> SQL&gt; set timing on time on<br /> 06:34:03 SQL&gt; alter table t_unused_col drop column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> Elapsed: 00:03:06.30<br /> <br /> <br /> <br /> <br /> 06:37:42 SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> Elapsed: 00:00:03.94<br /> <br /> <br /> 06:37:56 SQL&gt; create table t_unused_col(a int,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> Elapsed: 00:00:01.98<br /> <br /> <br /> 06:38:12 SQL&gt; insert into t_unused_col select level,level from dual connect by level&lt;=1000000;<br /> <br /> <br /> <br /> <br /> 1000000 rows created.<br /> <br /> <br /> Elapsed: 00:00:15.59<br /> 06:40:58 SQL&gt; 06:40:58 SQL&gt; commit;<br /> <br /> <br /> 06:41:11 SQL&gt; alter table t_unused_col set unused column b;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> Elapsed: 00:00:00.77<br /> <br /> <br /> 06:41:32 SQL&gt; alter table t_unused_col drop unused columns;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> Elapsed: 00:02:22.51<br /> 06:44:13 SQL&gt; commit;<br /> <br /> <br /> Commit complete.<br /> <br /> <br /> Elapsed: 00:00:00.00<br /> <br /> <br /> <br /> <br /> 15,透過alter table set unsed(表中列的列表,以逗號分割),可以同時指定多個列不可用<br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int,c int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused (b,c);<br /> <br /> <br /> Table altered.<br /> <br /> <br /> 16,alter table set unused column cascade constraints配置某列不可用會遞迴刪除定義的約束,發現指不指定cascade constraints選項都會遞迴刪除其約束<br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; alter table t_unused_col add constraint chk_b check(b&gt;1);<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> CONSTRAINT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CO STATUS<br /> ------------------------------ -- ----------------<br /> CHK_B &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;C &nbsp;ENABLED<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column b cascade constraints;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> no rows selected<br /> <br /> <br /> SQL&gt; drop table T_UNUSED_COL purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; drop table t_unused_col purge;<br /> <br /> <br /> Table dropped.<br /> <br /> <br /> SQL&gt; create table t_unused_col(a int primary key,b int);<br /> <br /> <br /> Table created.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> CONSTRAINT_NAME &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;CO STATUS<br /> ------------------------------ -- ----------------<br /> SYS_C0011644 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; P &nbsp;ENABLED<br /> <br /> <br /> SQL&gt; alter table t_unused_col set unused column a;<br /> <br /> <br /> Table altered.<br /> <br /> <br /> SQL&gt; select constraint_name,constraint_type,status from user_constraints where table_name='T_UNUSED_COL';<br /> <br /> <br /> no rows selected<br /> <br /> <br /> <b style="color:#666666;font-family:宋體, Arial;line-height:26px;white-space:normal;"> <h2 style="margin:0px;padding:0px;"> <span style="text-indent:2em;">個人簡介</span> </h2> <b style="text-indent:24px;"><br /> <div style="text-indent:2em;"> 8年oracle從業經驗,具備豐富的oracle技能,目前在國內北京某專業oracle服務公司從事高階技術顧問。 </div> <div style="text-indent:2em;"> </div> <div style="text-indent:2em;"> 服務過的客戶: </div> <div style="text-indent:2em;"> 中國電信 </div> <div style="text-indent:2em;"> 中國移動 </div> <div style="text-indent:2em;"> 中國聯通 </div> <div style="text-indent:2em;"> 中國電通 </div> <div style="text-indent:2em;"> 國家電網 </div> <div style="text-indent:2em;"> 四川達州商業銀行 </div> <div style="text-indent:2em;"> 湖南老百姓大藥房 </div> <div style="text-indent:2em;"> 山西省公安廳 </div> <div style="text-indent:2em;"> 中國郵政 </div> <div style="text-indent:2em;"> 北京302醫院 &nbsp; &nbsp;&nbsp; </div> <div style="text-indent:2em;"> 河北廊坊新奧集團公司 </div> <div style="text-indent:2em;"> <br /> &nbsp;專案經驗: </div> <div style="text-indent:2em;"> 中國電信3G專案AAA系統資料庫部署及最佳化<br /> &nbsp; &nbsp; &nbsp; 中國聯通4G資料庫效能分析與最佳化 </div> <div style="text-indent:2em;"> 中國聯通CRM資料庫效能最佳化 </div> <div style="text-indent:2em;"> 中國移動10086電商平臺資料庫部署及最佳化 </div> <div style="text-indent:2em;"> 湖南老百姓大藥房ERR資料庫sql最佳化專案 </div> <div style="text-indent:2em;"> 四川達州商業銀行TCBS核心業務系統資料庫模型設計和RAC部署及最佳化 </div> <div style="text-indent:2em;"> 四川達州商業銀行TCBS核心業務系統後端批處理儲存過程功能模組編寫及最佳化 </div> <div style="text-indent:2em;"> 北京高鐵訊號監控系統RAC資料庫部署及最佳化 </div> <div style="text-indent:2em;"> 河南宇通客車資料庫效能最佳化 </div> <div style="text-indent:2em;"> 中國電信電商平臺核心採購模組表模型設計及最佳化 </div> <div style="text-indent:2em;"> 中國郵政儲蓄系統資料庫效能最佳化及sql最佳化 </div> <div style="text-indent:2em;"> 北京302醫院資料庫遷移實施 </div> <div style="text-indent:2em;"> 河北廊坊新奧data guard部署及最佳化 </div> <div style="text-indent:2em;"> 山西公安廳身份證審計資料庫系統故障評估 </div> <div style="text-indent:2em;"> 國家電網上海災備專案4 node&nbsp;rac+adg&nbsp;<br /> &nbsp; &nbsp; &nbsp; &nbsp;貴州移動crm及客服資料庫效能最佳化專案<br /> &nbsp; &nbsp; &nbsp; &nbsp;貴州移動crm及客服務資料庫sql稽核專案<br /> &nbsp; &nbsp; &nbsp; &nbsp;深圳穆迪軟體有限公司資料庫效能最佳化專案 </div> <div style="text-indent:2em;"> <br /> 聯絡方式: </div> <div style="text-indent:2em;"> 手機:18201115468 </div> <div style="text-indent:2em;"> qq &nbsp; : &nbsp; 305076427 </div> <div style="text-indent:2em;"> qq微博: wisdomone1 </div> <div style="text-indent:2em;"> 新浪微博:wisdomone9 </div> <div style="text-indent:2em;"> qq群:275813900 &nbsp; &nbsp; </div> <div style="text-indent:2em;"> itpub部落格名稱:wisdomone1 &nbsp; &nbsp;<a href="http://blog.itpub.net/9240380/" style="text-decoration:none;color:#565656;">http://blog.itpub.net/9240380/</a> </div> </b></b><br /> <br /> <br /> <br />

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/30496894/viewspace-1822203/,如需轉載,請註明出處,否則將追究法律責任。

相關文章