search
HomeDatabaseMysql TutorialSQL SERVER 2008的数据压缩

SQL SERVER 2008的数据压缩

Jun 07, 2016 pm 03:06 PM
serversqlcompressiondataVersion

一、 数据 库版本 数据 压缩 在Sql Server 2008上才支持,2005不行,并且还要是企业版。我常常忘了这一点,在2005的Studio上闹出语法错误的状况,折腾浪费了好一阵才醒悟过来。 二、 压缩 状况 大约可以节省20%-50%的空间,并且行 压缩 和页 压缩 有所区别。


一、数据库版本
数据压缩在Sql Server 2008上才支持,2005不行,并且还要是企业版。我常常忘了这一点,在2005的Studio上闹出语法错误的状况,折腾浪费了好一阵才醒悟过来。


二、压缩状况
大约可以节省20%-50%的空间,并且行压缩和页压缩有所区别。
但让我失望的是,像含有Varchar(max),xml这种字段类型的,反而似乎压缩不起什么作用。其实我觉得最需要压缩的就是它们。


三、行压缩与页压缩
压缩是将固定长度类型存储为可变长度存储类型。页压缩除了行压缩,还有字典压缩等等。就是说,页级比行级压得更狠,更厉害。通常,表的话我采用页压缩;索引,行压缩。不为什么,想当然耳。


四、开始压缩
压缩的时候,硬盘要有空余的空间,因为压缩需要消耗额外的磁盘。比如说,我压缩一个190GB的表,大概还要额外占用90GB的空间。压缩完了以后,可以通过收缩数据库文件释放。释放了以后空间就连本带利多上一点。


非分区表页级压缩
ALTER TABLE [table1] REBUILD WITH (DATA_COMPRESSION = PAGE);
GO


分区表页级压缩
ALTER TABLE [partitiontable1]
REBUILD PARTITION = ALL
WITH
(
DATA_COMPRESSION = PAGE ON PARTITIONS(1 TO 11)
);
GO


非分区索引行级压缩
ALTER INDEX ix_id
ON table1
REBUILD WITH ( DATA_COMPRESSION = ROW ) ;
GO

分区索引行级压缩
ALTER INDEX Ix_Id ON partitiontable1
REBUILD PARTITION = ALL
WITH
(
DATA_COMPRESSION = ROW ON PARTITIONS(1 TO 16)
);
GO


五、压缩以后收缩数据库文件
DBCC SHRINKFILE ([数据库文件逻辑名], 收缩至多大(以M为单位));
GO


DBCC SHRINKFILE ([data_0], 5371);
GO


这个收缩后大小,我是先在Studio中,选中数据库,鼠标右键,在菜单中选任务,收缩,然后得到这个收缩后的最小尺寸,再抄到脚本上的。
其实帮助里面说,DBCC SHRINKFILE 不会将文件收缩到小于存储文件中的数据所需要的大小。例如,如果使用 10 MB 数据文件中的 7 MB,则带有 target_size 为 6 的 DBCC SHRINKFILE 语句只能将该文件收缩到 7 MB,而不能收缩到 6 MB。那么我们将5371写成1,岂不快哉?我没有试,可能可以。


六、经验总结
压缩和收缩分区表、分区索引消耗好多时间。有个几十G的分区表,我压缩完了以后,收缩花了2天又19个小时,是用脚本执行的,一口气不停歇。
非分区表则很快,100多G的文件,1、2小时就搞定了。


七、为什么要压缩
我觉得数据库服务器的瓶颈往往在于硬盘。象我们的服务器,8个核,平常时CPU很少上到10%,到30%已经顶天了。压缩的意义,就是将硬盘的压力转一部分到CPU,正中下怀。
另一个就是,现在我们网站功能逐渐多了以后,数据增长也很快。几年下来,数据积累相当可观,现在已经用了600多G了。

 

八、不要用操作系统的文件压缩

不要去使用只读用户定义文件组和只读数据库的 NTFS 压缩。结果没有压缩多少,反倒是想将数据库只读去掉,还要先解压。死未?

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Describe the different SQL transaction isolation levels (Read Uncommitted, Read Committed, Repeatable Read, Serializable) and their implications in MySQL/InnoDB.Apr 15, 2025 am 12:11 AM

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL vs. Other Databases: Comparing the OptionsMySQL vs. Other Databases: Comparing the OptionsApr 15, 2025 am 12:08 AM

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment