{"id":2508,"date":"2007-07-12T03:55:00","date_gmt":"2007-07-12T03:55:00","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/beware-ye-of-ancient-columns\/"},"modified":"2016-07-28T10:49:06","modified_gmt":"2016-07-28T10:49:06","slug":"beware-ye-of-ancient-columns","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/beware-ye-of-ancient-columns\/","title":{"rendered":"Beware ye of ancient columns"},"content":{"rendered":"<p>I&#8217;m sure you all make changes to your schema over time adding columns to tables and occassionally dropping them. But were you aware that even though you drop a column from a table the space in the row continues to be taken up by the <i>ghost<\/i> of the column that you deleted. So if you make lots of schema changes to a database over time the row becomes much larger than it needs to be.<\/p>\n<p>Of course this has the interesting effect that in the <i>syscolumns<\/i> table the <i>colid<\/i> for your rows aren&#8217;t sequential so they can&#8217;t be used as an index.<\/p>\n<p>Just a little thing to be aware of and one of those things that doesn&#8217;t seem to be mentioned in books on-line ( at least in the 5 minutes I spent looking for it this morning ).<\/p>\n<p>CREATE&#160;TABLE&#160;ItsTooBig&#160;(col1&#160;CHAR(100),&#160;col2&#160;CHAR(100))<br \/>SELECT&#160;name,&#160;colid,&#160;xoffset<br \/>                             &#160;&#160;&#160;FROM&#160;syscolumns<br \/>                                 &#160;&#160;&#160;WHERE&#160;OBJECT_ID(&#8216;ItsTooBig&#8217;)&#160;=&#160;id<\/p>\n<p>DECLARE&#160;@i&#160;INT<br \/>                                             SET&#160;@i&#160;=&#160;1<br \/>WHILE&#160;@i&#160;&lt;&#160;100<br \/>BEGIN<br \/>                                                         &#160;&#160;&#160;INSERT&#160;INTO&#160;ItsTooBig          VALUES&#160;(1,@i)<br \/>                 &#160;&#160;&#160;DECLARE&#160;@command&#160;VARCHAR(50)<br \/>                         &#160;&#160;&#160;SET&#160;@command&#160;=<br \/>                             &#160; &#160; &#160;&#8217;alter&#160;table&#160;ItsTooBig&#160;drop&#160;column&#160;col&#8217;<br \/>                                 &#160; &#160; &#160;+&#160;CAST(@i&#160;AS&#160;VARCHAR(3))<br \/>                                             &#160;&#160;&#160;EXEC(@command)<br \/>                                                     &#160;&#160;&#160;<br \/>                                                     &#160;&#160; SET&#160;@i&#160;=&#160;@i&#160;+&#160;1<br \/>                                                             &#160;&#160;&#160;<br \/>                                                             &#160;&#160; SET&#160;@command&#160;=<br \/>                                                                 &#160; &#160; &#160;&#8217;alter&#160;table&#160;ItsTooBig&#160;add&#160;col&#8217;<br \/>                                                                     &#160; &#160; &#160; &#160;&#160;+&#160;CAST((@i+1)&#160;AS&#160;VARCHAR(3))<br \/>                                                                                     &#160; &#160; &#160; &#160;&#160;+&#160;&#8217;&#160;char(100)&#8217;<br \/>                                                                                         &#160;&#160;&#160;EXEC(@command)<\/p>\n<p>                                                                                                 &#160;&#160;&#160;IF&#160;@i&#160;%&#160;10&#160;=&#160;0<br \/>                                                                                                         &#160;&#160;&#160;&#160;&#160;&#160;SELECT&#160;@i,&#160;name,&#160;colid,&#160;xoffset<br \/>                                                                                                                     &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;          FROM&#160;syscolumns<br \/>             &#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; WHERE&#160;OBJECT_ID(&#8216;ItsTooBig&#8217;)&#160;=&#160;id<br \/>END<br \/>                             DROP&#160;TABLE&#160;ItsTooBig<\/p>\n<p>Take care out there people &#8211; those columns are out to get you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m sure you all make changes to your schema over time adding columns to tables and occassionally dropping them. But were you aware that even though you drop a column from a table the space in the row continues to be taken up by the ghost of the column that you deleted. So if you&#8230;&hellip;<\/p>\n","protected":false},"author":167198,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-2508","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/167198"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=2508"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2508\/revisions"}],"predecessor-version":[{"id":41536,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/2508\/revisions\/41536"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=2508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=2508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=2508"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=2508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}