Contents

[-]
1 Sparse Columns
2 Example
3 Column Sets
4 Example
5 참고자료


1 Sparse Columns #

스파스 열은 Null값에 대해 최적화된 저장소가 잇는 일반 열을 말한다. 권장사항은 최소 20% ~ 40%정도의 공간이 절약되는 경우에 사용한다. 필터링된 인덱스와 같이 사용될 수 있다. Geography, geometry, image, ntext, text, timestamp, user-defined data types 데이터 형식은 sparse로 지정될 수 없다. 다음의 도움말의 내용이다.

고정길이데이터형식
데이터형식비-스파스바이트스파스바이트NULL백분율
bit0.1254.12598%
tinyint1586%
smallint2676%
int4864%
bigint81252%
real4864%
float81252%
smallmoney4864%
money81252%
smalldatetime4864%
datetime81252%
uniqueidentifier162043%
date3769%

전체 자릿수 종속 길이 데이터 형식
데이터 형식비-스파스 바이트스파스 바이트NULL 백분율
datetime2(0)61057%
datetime2(7)81252%
time(0)3769%
time(7)5960%
datetimetoffset(0)81252%
datetimetoffset (7)101449%
decimal/numeric(1,s)5960%
decimal/numeric(38,s)172142%
vardecimal(p,s)decimal 형식을 일반적인 예상치로 사용

데이터 종속 길이 데이터 형식
데이터형식비-스파스바이트스파스바이트NULL백분율
sql_variant기본데이터형식에따라다름
varchar또는char4+평균데이터2+평균데이터60%
nvarchar또는nchar4+평균데이터2+평균데이터60%
varbinary또는binary4+평균데이터2+평균데이터60%
xml4+평균데이터2+평균데이터60%
hierarchyId4+평균데이터2+평균데이터60%

2 Example #

--drop table sparse_table
create table sparse_table
(
	sparse_col nchar(4) sparse null
);

--drop table non_sparse_table
create table non_sparse_table
(
	non_sparse_col nchar(4) null
);

with temp(num)
as
(
        select 1 num
        union all
        select num + 1 from temp
        where num + 1 <= 10
)
insert sparse_table(sparse_col) 
select N'뭡니까?' -- 100000건
from temp a, temp b, temp c, temp d, temp e
union all
select NULL -- 100000건
from temp a, temp b, temp c, temp d, temp e;

with temp(num)
as
(
        select 1 num
        union all
        select num + 1 from temp
        where num + 1 <= 10
)
insert non_sparse_table(non_sparse_col) 
select N'뭡니까?' -- 100000건
from temp a, temp b, temp c, temp d, temp e
union all
select NULL -- 100000건
from temp a, temp b, temp c, temp d, temp e;

exec sp_spaceused 'sparse_table'
exec sp_spaceused 'non_sparse_table'

/*
name         rows   reserved data    index_size unused
------------ ------ -------- ------- ---------- ------
sparse_table 200000 3720 KB  3664 KB 8 KB       48KB

name         rows   reserved data    index_size unused
------------ ------ -------- ------- ---------- ------
sparse_table 200000 3400 KB  3368 KB 8 KB       48KB
*/

3 Column Sets #

스파스 열을 사용하는 테이블에서는 테이블의 모든 스파스 열을 반환하는 열집합을 지정할수 있다. 열 집합은 구조화된 출력으로 테이블의 모든 스파스 열을 결합하는 형식화되지 않은 XML의 표현이다.

4 Example #

--drop table column_sets 
create table column_sets
(
	sparse1 int primary key
,	sparse2 int sparse null
,	sparse3 nvarchar(20) sparse null
,	cs xml column_set FOR ALL_SPARSE_COLUMNS
);

insert column_sets(sparse1, sparse2, sparse3) values
	(1, 1, N'이건또뭐냐?')
,	(2, null, N'뭐가맘에안드는데?');

select * from column_sets;
select sparse1, sparse2, sparse3, cs from column_sets
column_set01.jpg
column_set02.jpg

CREATE TABLE t (
	i int SPARSE
, cs xml column_set FOR ALL_SPARSE_COLUMNS
);

INSERT t(cs) VALUES ('<i/>');
SELECT i FROM t;
SELECT * FROM t;
column_set03.jpg

5 참고자료 #

Retrieved from http://blog.databaser.net/moniwiki/wiki.php/SparseColumnsAndColumnSets
last modified 2018-04-13 23:12:53