Sparse columns are ordinary columns that have an optimized storage for null values. Sparse columns reduce the space requirements for null values at the cost of more overhead to retrieve non-NULL values. Consider using sparse columns when the space saved is at least 20 percent to 40 percent.Sparse 關鍵字
USE AdventureWorks2025;
GO
CREATE TABLE DocumentStore
(DocID int PRIMARY KEY,
Title varchar(200) NOT NULL,
ProductionSpecification varchar(20) SPARSE NULL, -- SPARSE Column
ProductionLocation smallint SPARSE NULL, -- SPARSE Column
MarketingSurveyGroup varchar(20) SPARSE NULL ) ; -- SPARSE Column
GO
Sparse Column 查詢
use AdventureWorks2025
go
SELECT
[name] AS ColumnName ,
is_nullable ,
is_sparse ,
is_column_set
FROM sys.columns
WHERE object_name(object_id) = 'DocumentStore'
限制- Sparse columns are a property of the storage layer, rather than the logical table. Therefore a SELECT ... INTO statement does not copy over the sparse column property into a new table.
- A sparse column must be nullable and cannot have the ROWGUIDCOL or IDENTITY properties. A sparse column cannot be of the following data types: text, ntext, image, timestamp, user-defined data type, geometry, or geography; or have the FILESTREAM attribute.
- A sparse column cannot have a default value.
- Sparse columns are incompatible with data compression. Therefore sparse columns cannot be added to compressed tables, nor can any tables containing sparse columns be compressed.
- Changing a column from sparse to non-sparse, or non-sparse to sparse, requires changing the storage format of the column. The SQL Server Database Engine uses the following procedure to accomplish this change:
- Adds a new column to the table in the new storage size and format.
- For each row in the table, updates and copies the value stored in the old column to the new column.
- Removes the old column from the table schema.
- Rebuilds the table (if there is no clustered index) or rebuilds the clustered index to reclaim space used by the old column.
重構某單位 AP 時發現 Table 整併後,配合商業邏輯而新增欄位內有 90% 資料會是 null,原本是想要多拆一個 Table 來存放節省空間,但該情境似乎蠻符合 Sparse Column 就來使用看看囉













