#title Sparse Columns and Column Sets [[TableOfContents]] ==== Sparse Columns ==== 스파스 열은 Null값에 대해 최적화된 저장소가 잇는 일반 열을 말한다. 권장사항은 최소 20% ~ 40%정도의 공간이 절약되는 경우에 사용한다. 필터링된 인덱스와 같이 사용될 수 있다. Geography, geometry, image, ntext, text, timestamp, user-defined data types 데이터 형식은 sparse로 지정될 수 없다. 다음의 도움말의 내용이다. '''고정길이데이터형식''' ||데이터형식||비-스파스바이트||스파스바이트||NULL백분율|| ||bit||0.125||4.125||98%|| ||tinyint||1||5||86%|| ||smallint||2||6||76%|| ||int||4||8||64%|| ||bigint||8||12||52%|| ||real||4||8||64%|| ||float||8||12||52%|| ||smallmoney||4||8||64%|| ||money||8||12||52%|| ||smalldatetime||4||8||64%|| ||datetime||8||12||52%|| ||uniqueidentifier||16||20||43%|| ||date||3||7||69%|| '''전체 자릿수 종속 길이 데이터 형식''' ||데이터 형식||비-스파스 바이트||스파스 바이트||NULL 백분율|| ||datetime2(0)||6||10||57%|| ||datetime2(7)||8||12||52%|| ||time(0)||3||7||69%|| ||time(7)||5||9||60%|| ||datetimetoffset(0)||8||12||52%|| ||datetimetoffset (7)||10||14||49%|| ||decimal/numeric(1,s)||5||9||60%|| ||decimal/numeric(38,s)||17||21||42%|| ||vardecimal(p,s)||decimal 형식을 일반적인 예상치로 사용|||| '''데이터 종속 길이 데이터 형식''' ||데이터형식||비-스파스바이트||스파스바이트||NULL백분율|| ||sql_variant||기본데이터형식에따라다름|| || || ||varchar또는char||4+평균데이터||2+평균데이터||60%|| ||nvarchar또는nchar||4+평균데이터||2+평균데이터||60%|| ||varbinary또는binary||4+평균데이터||2+평균데이터||60%|| ||xml||4+평균데이터||2+평균데이터||60%|| ||hierarchyId||4+평균데이터||2+평균데이터||60%|| ==== 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 */ }}} ==== Column Sets ==== 스파스 열을 사용하는 테이블에서는 테이블의 모든 스파스 열을 반환하는 열집합을 지정할수 있다. 열 집합은 구조화된 출력으로 테이블의 모든 스파스 열을 결합하는 형식화되지 않은 XML의 표현이다. ==== 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 }}} attachment:column_set01.jpg attachment:column_set02.jpg {{{ CREATE TABLE t ( i int SPARSE , cs xml column_set FOR ALL_SPARSE_COLUMNS ); INSERT t(cs) VALUES (''); SELECT i FROM t; SELECT * FROM t; }}} attachment:column_set03.jpg ==== 참고자료 ==== * [Working with sparse columns in SQL Server 2008 http://searchsqlserver.techtarget.com/tip/0,289483,sid87_gci1357672_mem1,00.html?track=sy41]