Home Database Mysql Tutorial SQL Server 2008中使用稀疏列和列集的方法(一)_MySQL

SQL Server 2008中使用稀疏列和列集的方法(一)_MySQL

Jun 01, 2016 pm 02:00 PM
space

SQLServer2008

尽管存储的成本已经很低了,但是我们仍然需要考虑使用多种技术(例如压缩和存档)来节省空间。当你思考怎样节省空间时,你第一个想到的是文件系统,但是空间节省也可以用在数据库。当我们创建一个数据库时,我们确保数据文件具有合适的大小和增长速度。

我们定期分析我们的数据库规模并执行缩小操作。我们可能执行这些任务用于不同的目的,但是有一个方面是相同的,这些任务帮助我们确保我们的数据库具有最佳的存储。Microsoft SQL Server为我们提供了用于降低数据库所用空间的各种技术。SQL Server 2008推出了一个用于定位可为空字段的技术,它为可为空字段提供了最佳的存储。在SQL Server 2008中的这个新特性就是稀疏列。这篇文章不会讲述很多关于稀疏列的特性,它介绍了具有列集的稀疏列的使用,以及在使用它们时你需要了解和考虑的事情。

这篇文章描述:

◆什么是稀疏列?

◆什么是列集?

◆在一个列集中插入和更新数据。

◆使用触发器跟踪变更。

◆对列集实施安全。

什么是稀疏列?

稀疏列是一个普通字段,就像其它字段一样,但是它降低了对空值的存储要求。一个可为空字段可以在表创建或修改时添加SPARSE关键字来成为稀疏列。如果一个列是稀疏列,那么SQL Server不会为空值分配空间。注意,在使用这个特性时它会增加对非空值数据提取的花费。因此你需要计算可以节省的空间来仔细地对字段应用这个特性。推荐只在空间至少可以节省20%至40%时使字段成为稀疏列。BLO提供了一个包含字段中每个数据类型所需空值百分比的表,以便使这些字段成为稀疏列。

什么是列集?

列集是一个显示所有稀疏列的字段,它作为一个XML类型的字段添加到表中。它不是物理上存在于这个表中的,它只像是一个计算出来的字段,但是它允许你对它进行修改。推荐你只在有很多稀疏列时使用列集,因为如果使用了列集而不是使用各个稀疏列,那么它会加快修改和提取。

下面的代码显示了为一个表创建一个列集的方法。

代码1:创建一个具有稀疏列和一个列集的表。

CREATE TABLE [dbo].[Customers]
(
[Id] int PRIMARY KEY,
[FirstName] varchar(50) NOT NULL,
[LastName] varchar(50) NOT NULL,
[Gender] bit SPARSE NULL, -- 1 = male, 2 = female
[Telephone] varchar(15) SPARSE NULL,
[MonthlyIncome] money SPARSE NULL,
[Comments] varchar(1000) SPARSE NULL
[AllSparseColumns] xml COLUMN_SET FOR ALL_SPARSE_COLUMNS
) 
 
我为所有可为空字段添加了SPARSE关键字,但是如同我前面提到的,应该在使它们成为稀疏列之前分析空值所占百分比。注意,当你创建这个表时你需要添加这个字段。SQL Server 不允许你没有稀疏列的情况下拥有列集字段。之后添加为稀疏列的字段可以使用添加的列集,看下面的代码:

代码2:创建具有一个列集的表,不使任何字段成为稀疏列。

-- adding column set without sparse columns 
CREATE TABLE [dbo].[Customers_1] 

[Id] int PRIMARY KEY, 
[FirstName] varchar(50) NOT NULL, 
[LastName] varchar(50) NOT NULL, 
[Gender] bit NULL, -- 1 = male, 2 = female 
[Telephone] varchar(15) NULL, 
[MonthlyIncome] money NULL, 
[Comments] varchar(1000) NULL, 
[AllSparseColumns] xml COLUMN_SET FOR ALL_SPARSE_COLUMNS 

-- inserting a record 
INSERT INTO dbo.Customers_1 
([Id], [FirstName], [LastName], [Gender], [Telephone], [MonthlyIncome], [Comments]) 
VALUES 
(1, 'Dinesh', 'Priyankara', 1, '777395871', 20000, 'no comments') 
-- this returns null 
SELECT AllSparseColumns FROM dbo.Customers_1 
-- Make the Gender column as a sparse column 
ALTER TABLE [dbo].[Customers_1] 
ALTER COLUMN [Gender] bit SPARSE NULL 
GO 
-- Make the Telephone column as a sparse column 
ALTER TABLE [dbo].[Customers_1] 
ALTER COLUMN [Telephone] varchar(15) SPARSE NULL 
-- Now it returns values of sparse columns as a xml 
SELECT AllSparseColumns FROM dbo.Customers_1
 

Statement of this Website
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

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)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Numerical distance based on machine learning: the distance between points in space Numerical distance based on machine learning: the distance between points in space Apr 11, 2023 pm 11:40 PM

This article is reprinted from the WeChat public account "Living in the Information Age". The author lives in the information age. To reprint this article, please contact the Living in the Information Age public account. In machine learning, a basic concept is how to judge the difference between two samples, so that the similarity and category information between the two samples can be evaluated. The measure to judge this similarity is the distance between two samples in the feature space. There are many measurement methods based on different data characteristics. Generally speaking, for two data samples x, y, define a function d(x, y). If it is defined as the distance between the two samples, then d(x, y) needs to satisfy the following basic properties : Non-negativity: d(x, y)>=0 Identity: d(x, y)=0 ⇔ x=y pair

How to add swap space on Ubuntu 22.04 LTS How to add swap space on Ubuntu 22.04 LTS Feb 20, 2024 am 11:12 AM

Swap space plays an important role in Linux systems, especially when the system is low on memory. It acts as a backup memory storage space that helps the system run smoothly and maintain stability even under high load. This article provides you with a detailed guide to adding swap space on Ubuntu 22.04LTS to ensure that your system performance is optimized and can handle various workloads. Understanding Swap Space Swap space provides virtual memory that is used to supplement the system's physical RAM. When the system is low on RAM, the kernel swaps data to disk to prevent out-of-memory and system crashes. Linux systems commonly use swap space to handle this situation. Run multiple memory-intensive applications simultaneously to process very large files or data

What is the appropriate amount of remaining space on the c drive? What is the appropriate amount of remaining space on the c drive? Jun 27, 2023 pm 02:51 PM

The remaining space on the c drive is 50-80G which is more suitable. Since the system will generate junk files, cache files, etc. in the future, it is recommended to reserve at least 50GB-80GB of space for the C drive; if you are not used to choosing a path when installing software and do not clean your computer frequently, then at least 100GB is required. .

Introduction to the C drive space required for upgrading win11 Introduction to the C drive space required for upgrading win11 Dec 23, 2023 am 08:57 AM

As we all know, if the system disk occupied is too large after the system installation is completed, it may cause system lags, delays, and even file loss. Therefore, before you install the win11 system, you need to know how much C drive space is required to upgrade win11. Let’s take a look with the editor. How much C drive space is required to upgrade win11: Answer: Upgrading win11 requires 20-30GB of C drive space. 1. According to Microsoft’s win11 configuration requirements, you can see that win11 installation requires 64GB of hard drive space. 2. But in fact, generally speaking, there is no need for such a large space. 3. According to feedback from users who have already installed win11, the win11 upgrade requires about 20-30GB of C drive space. 4. But if our door only has

How to free up space on Xbox Series S or Xbox Series X How to free up space on Xbox Series S or Xbox Series X Apr 19, 2023 pm 01:46 PM

The Xbox console has a huge selection of games to download and play. Coupled with Microsoft's Xbox Game Pass subscription, the fun never stops with your game collection. However, there is the issue of space available for games, which is 356GB on Xbox Series S and 850GB on Xbox Series X. While this was fine in previous versions of the game, the maximum size of the game was probably between 20 and 45GB, which isn't the case with recent games. Recently available games end up taking up a lot of space on the disk, leaving us less space to download other games. ForzaHorizon5 and Halo5Guardians and more

iOS 17.2: How to record spatial video on iPhone 15 Pro iOS 17.2: How to record spatial video on iPhone 15 Pro Dec 15, 2023 pm 01:09 PM

If you have an iPhone 15 or iPhone 15 Pro Max, iOS 17.2 lets you record spatial video for viewing in the Photos app on Apple's upcoming Vision Pro headphones. Here's how you do it. Apple's VisionPro headphones are expected to be released around February 2024. Until then, one way you can prepare for this is to use your iPhone to record video in a special format called spatial video, which can be viewed on Apple's headphones. Spatial videos appear as normal videos when viewed on an iPhone, but they offer near three-dimensionality on VisionPro

A review of artificial intelligence technology in cyberspace security A review of artificial intelligence technology in cyberspace security Apr 11, 2023 pm 04:10 PM

1. Introduction Due to the explosive growth of current computer networks, the ensuing problem is the rapidly increasing number of network attacks. Various sectors of our society, from government departments to various critical infrastructures in society, are heavily dependent on computer networks and information technology. Apparently they are also vulnerable to cyberattacks. Typical network attacks disable the target computer, take services offline, or access the target computer's data. The number and impact of cyberattacks has increased significantly since the 1990s. Network security refers to a set of technologies used to protect network device activities and measures to protect them from all possible threats. In traditional network security technology, most of them are static access management, and the security control system will protect based on preset definitions.

What should I do if there is insufficient space in Toutiao Search Express Edition? What should I do if there is insufficient space in Toutiao Search Express Edition? Feb 27, 2024 am 11:46 AM

As mobile phones are used more and more frequently, many users may find that there is insufficient storage space on the phone, which may cause some applications to not run properly or affect the overall performance of the phone. For users of the express version of Toutiao Search, there may sometimes be a lot of cached content. So what should you do when your phone prompts that there is insufficient space? Then come and follow the editor to clean it up below! What to do if Toutiao Search Express Edition runs out of space. Answer: Clear space. Method to clear space: 1. First open the Toutiao Search Express version software, enter the home page of the software and click [My] in the lower right corner; 2. Then slide to the bottom of the My page and find the [System Settings] function Click; 3. Then in the system settings page we click [Clear Cache]; 4.

See all articles