{"id":354,"date":"2010-12-09T11:04:04","date_gmt":"2010-12-09T11:04:04","guid":{"rendered":"http:\/\/www.esds.co.in\/blog\/?p=354"},"modified":"2014-03-14T10:00:40","modified_gmt":"2014-03-14T10:00:40","slug":"compress-the-database-in-sql-server-2008","status":"publish","type":"post","link":"https:\/\/www.esds.co.in\/blog\/compress-the-database-in-sql-server-2008\/","title":{"rendered":"Compress The Database in SQL Server 2008"},"content":{"rendered":"<p>In SQL Server 2008, there is the possibility to compress the data stored in the database.<\/p>\n<p>Compression can give a good performance gain for those databases whose size is much larger than the server&#8217;s memory and as a consequence, the server is experiences a substantial load on the disk system. Efficient use of disk in such cases is increasing at an actual rate of data compression.<\/p>\n<p>In order to use this compression, it is necessary for a particular table or index to use the rebuild command with the desired compression type (page or row).<\/p>\n<p>The following script compresses the data in all user tables in the database, after assessing the extent appropriate compression. Will not be compressed tables and indexes, the compression ratio which is estimated server in less than 1.3 or size &#8211; less than 10 megabytes.<\/p>\n<p>This script is used to compress the page level, but the script is easy to refine and to determine the most appropriate for a particular table or index compression, &#8211; page \/ row.<\/p>\n<blockquote><p>set nocount on<br \/>\ndeclare @idx table(<br \/>\nobjnname sysname,<br \/>\nschname sysname,<br \/>\nindex_id int,<br \/>\npartition_number int,<br \/>\ncurrentSize bigint,<br \/>\ncompSize bigint,<br \/>\nScurrentSize bigint,<br \/>\nScompSize bigint)<\/p>\n<p>declare<br \/>\n@sql varchar(max),<br \/>\n@tn varchar(128),<br \/>\n@sn varchar(128),<br \/>\n@oid int,<br \/>\n@iid int<\/p>\n<p>declare c cursor local fast_forward for<\/p>\n<p>select object_schema_name(object_id), object_name(object_id), t.[object_id]\nfrom sys.tables t with(nolock)<br \/>\nwhere t.[type] = &#8216;U&#8217;<br \/>\norder by object_schema_name(object_id), object_name(object_id)<br \/>\nopen c<br \/>\nwhile 1=1<br \/>\nbegin<br \/>\nfetch next from c into @sn, @tn, @oid<br \/>\nif @@FETCH_STATUS &lt;&gt; 0 break<br \/>\nraiserror(&#8216;%s.%s&#8217;, 10, 1, @sn, @tn) with nowait<\/p>\n<p>&#8211; <strong>Get the evaluation of the effectiveness of compression index<\/strong><\/p>\n<p>insert into @idx<br \/>\nexec sp_estimate_data_compression_savings @sn, @tn, NULL, NULL, &#8216;PAGE&#8217; ;<\/p>\n<p>declare ic cursor local fast_forward for<br \/>\nselect si.name, si.index_id<br \/>\nfrom @idx i<br \/>\ninner join sys.indexes si<br \/>\non si.[object_id] = @oid<br \/>\nand si.index_id = i.index_id<br \/>\n&#8212; <strong>Do not compress that bad contracts<\/strong><br \/>\nwhere i.ScurrentSize &gt; i.ScompSize*1.3<br \/>\n&#8212; <strong>Do not compress small indexes and tables<\/strong><br \/>\nand i.currentSize &gt; 10000 &#8212; &gt;10Mb<br \/>\nopen ic<br \/>\ndeclare @index varchar(128)<br \/>\nwhile 1=1<br \/>\nbegin<br \/>\nfetch next from ic into @index, @iid<br \/>\nif @@FETCH_STATUS &lt;&gt; 0 break<br \/>\nprint @iid<br \/>\n&#8212; If index_id&gt; 0, this index<br \/>\nif @iid &gt; 0<br \/>\nset @sql = &#8216;alter index [&#8216; @index &#8216;] on [&#8216; @sn &#8216;].[&#8216; @tn &#8216;] rebuild with(data_compression=page)&#8217;<br \/>\n&#8212; If index_id = 0, then it is &#8220;a lot&#8221;<br \/>\nelse<br \/>\nset @sql = &#8216;alter table [&#8216; @sn &#8216;].[&#8216; @tn &#8216;] rebuild with(data_compression=page)&#8217;<br \/>\nraiserror(&#8216;%s&#8217;, 10, 1, @sql) with nowait<br \/>\nexec(@sql)<br \/>\nend<br \/>\nclose ic<br \/>\ndeallocate ic<br \/>\ndelete from @idx<\/p><\/blockquote>\n<p>end<\/p>\n<p>Before you apply compression on the industrial base, it is better to assess the degree of compression in both modes to test and evaluate the growth or productivity. And only then, apply compression to the tables and indexes for which it is really important.<\/p>\n<p>The compression and the extraction of compressed data processor should be the same, or depending on the database, you can lose in performance. For example, there&#8217;s virtually no point to compress data in the database, if the total amount of real-world data is less than the amount of available SQL Server memory. In this case, you can get a win when the dedicated server web hosting starts at the time when the buffer pool (cache server data) is filled with data from the database, but the rest of the time probably any advantage from the fact that the data is compressed, will be gone.<\/p>\n<p><strong>Please Note:<\/strong> If you are looking for Rich Data Center Web Hosting, Cloud Services, Dedicated Hosting, Colocation or Software Services in India at Affordable Costs, Visit our website at <strong><a href=\"http:\/\/www.esds.co.in\/\" target=\"_blank\">WWW.ESDS.CO.IN<\/a><\/strong>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In SQL Server 2008, there is the possibility to compress the data stored in the database. Compression can give a good performance gain for those databases whose size is much larger than the server&#8217;s memory and as a consequence, the server is experiences a substantial load on the disk system. Efficient use of disk in&#8230; <\/p>\n<div class=\"clear\"><\/div>\n<p><a href=\"https:\/\/www.esds.co.in\/blog\/compress-the-database-in-sql-server-2008\/\" class=\"gdlr-button small excerpt-read-more\">Read More<\/a><\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[6],"tags":[72],"class_list":["post-354","post","type-post","status-publish","format-standard","hentry","category-dedicated-server-hosting","tag-sql-server-2008"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts\/354","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/comments?post=354"}],"version-history":[{"count":9,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts\/354\/revisions"}],"predecessor-version":[{"id":4135,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/posts\/354\/revisions\/4135"}],"wp:attachment":[{"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/media?parent=354"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/categories?post=354"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.esds.co.in\/blog\/wp-json\/wp\/v2\/tags?post=354"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}