SQLServer BIT字段存储

SQLServer  BIT字段存储

Author:zfive5

Email:[email protected]

引子

和同事探讨BIT怎么存储,发生了分歧

create 
table A1

(

a
CHAR(5),

b
bit,

c
CHAR(5),

d
BIT

)

由于之前成天再看《存储引擎》,深知这样定义表字段也会把两个BIT压缩成1个BYTE

而是同事说会分别存2个BYTE

开始证明

正文

在证明前,列出一些工具SQL:


CREATE FUNCTION
convert_page_nums(@page_num
binary(6))

RETURNS
VARCHAR(11)

AS

BEGIN

RETURN(

CONVERT(VARCHAR(2),(

CONVERT(INT,SUBSTRING(@page_num,6,1))*
POWER(2,8)

)
+

(CONVERT(INT,SUBSTRING(@page_num,5,1)))

)
+ ‘:‘ +

CONVERT(VARCHAR(11),

(CONVERT(INT,SUBSTRING(@page_num,4,1))
* POWER(2,24))
+

(CONVERT(INT,SUBSTRING(@page_num,3,1))
* POWER(2,16))
+

(CONVERT(INT,SUBSTRING(@page_num,2,1))
* POWER(2,8)) 
+

(CONVERT(INT,SUBSTRING(@page_num,1,1)))

)

)

END

select
object_name(object_id)
as name,partition_id,partition_number
as pnum,rows,

allocation_unit_id
as au_id,type_desc
as page_type_desc,total_pages
as pages

from sys.partitions
p join
sys.allocation_units
a

on p.partition_id
= a.container_id

where
object_id = object_id(‘dbo.A1‘)

select
convert(char(8),object_name(i.object_id))
as table_name,

i.name
as index_name,i.index_id,i.type_desc
as index_type,

partition_id,partition_number
as pnum,rows,

allocation_unit_id
as au_id,a.type_desc
as page_type_desc,total_pages
as pages

from sys.indexes
i join
sys.partitions
p on
i.object_id
= p.object_id
and i.index_id
= p.index_id

join sys.allocation_units
a on
p.partition_id
= a.container_id

where i.object_id
= object_id(‘dbo.a1‘)

执行如下SQL:

select
convert(char(8),object_name(i.object_id))
as table_name,

indexproperty(i.object_id,i.name,‘minlen‘)
as minlen,

i.name
as index_name,i.index_id,i.type_desc
as index_type,

partition_id,partition_number
as pnum,first_page,rows,

a.allocation_unit_id
as au_id,a.type_desc
as page_type_desc,a.total_pages
as pages

from sys.indexes
i join
sys.partitions
p on
i.object_id
= p.object_id
and i.index_id
= p.index_id

join sys.system_internals_allocation_units
a on
p.partition_id
= a.container_id

where i.object_id
= object_id(‘dbo.a1‘)

SELECT DBO.convert_page_nums(0xEC2D00000100)

执行如下:

insert A1(a,b,c,d)
values(‘AAAAA‘,1,‘BBBBB‘,1)

insert A1(a,b,c,d)
values(‘BBBBB‘,0,‘CCCCC‘,0)

insert A1(a,b,c,d)
values(‘CCCCC‘,0,‘DDDDD‘,1)

insert A1(a,b,c,d)
values(‘DDDDD‘,1,‘FFFFF‘,0)

dbcc TRACEon(3604)

DBCC page
(A,1,121,3)

得到如下信息:


DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。

PAGE: (1:121)

BUFFER:

BUF @0x000000046E165B80

bpage = 0x000000045DDDA000          bhash = 0x0000000000000000          bpageno = (1:121)

bdbid = 9                           breferences = 0                     bcputicks = 0

bsampleCount = 0                    bUse1 = 3353                        bstat = 0x10b

blog = 0x15acc                      bnext = 0x0000000000000000

PAGE HEADER:

Page @0x000000045DDDA000

m_pageId = (1:121)                  m_headerVersion = 1                 m_type = 1

m_typeFlagBits = 0x0                m_level = 0                         m_flagBits = 0x8000

m_objId (AllocUnitId.idObj) = 85    m_indexId (AllocUnitId.idInd) = 256

Metadata: AllocUnitId = 72057594043498496

Metadata: PartitionId = 72057594039107584                                Metadata: IndexId = 0

Metadata: ObjectId = 581577110      m_prevPage = (0:0)                  m_nextPage = (0:0)

pminlen = 15                        m_slotCnt = 4                       m_freeCnt = 8016

m_freeData = 168                    m_reservedCnt = 0                   m_lsn = (34:25:2)

m_xactReserved = 0                  m_xdesId = (0:0)                    m_ghostRecCnt = 0

m_tornBits = 0                      DB Frag ID = 1

Allocation Status

GAM (1:2) = ALLOCATED               SGAM (1:3) = ALLOCATED

PFS (1:1) = 0x61 MIXED_EXT ALLOCATED  50_PCT_FULL                        DIFF (1:6) = CHANGED

ML (1:7) = NOT MIN_LOGGED

Slot 0 Offset 0x60 Length 18

Record Type = PRIMARY_RECORD        Record Attributes =  NULL_BITMAP    Record Size = 18

Memory Dump @0x0000000013D6A060

0000000000000000:   10000f00 41414141 41034242 42424204 0000      ....AAAAA.BBBBB...

Slot 0 Column 1 Offset 0x4 Length 5 Length (physical) 5

a = AAAAA

Slot 0 Column 2 Offset 0x9 Length 1 (Bit position 0)

b = 1

Slot 0 Column 3 Offset 0xa Length 5 Length (physical) 5

c = BBBBB

Slot 0 Column 4 Offset 0x9 Length 1 (Bit position 1)

d = 1

Slot 1 Offset 0x72 Length 18

Record Type = PRIMARY_RECORD        Record Attributes =  NULL_BITMAP    Record Size = 18

Memory Dump @0x0000000013D6A072

0000000000000000:   10000f00 42424242 42004343 43434304 0000      ....BBBBB.CCCCC...

Slot 1 Column 1 Offset 0x4 Length 5 Length (physical) 5

a = BBBBB

Slot 1 Column 2 Offset 0x9 Length 1 (Bit position 0)

b = 0

Slot 1 Column 3 Offset 0xa Length 5 Length (physical) 5

c = CCCCC

Slot 1 Column 4 Offset 0x9 Length 1 (Bit position 1)

d = 0

Slot 2 Offset 0x84 Length 18

Record Type = PRIMARY_RECORD        Record Attributes =  NULL_BITMAP    Record Size = 18

Memory Dump @0x0000000013D6A084

0000000000000000:   10000f00 43434343 43024444 44444404 0000      ....CCCCC.DDDDD...

Slot 2 Column 1 Offset 0x4 Length 5 Length (physical) 5

a = CCCCC

Slot 2 Column 2 Offset 0x9 Length 1 (Bit position 0)

b = 0

Slot 2 Column 3 Offset 0xa Length 5 Length (physical) 5

c = DDDDD

Slot 2 Column 4 Offset 0x9 Length 1 (Bit position 1)

d = 1

Slot 3 Offset 0x96 Length 18

Record Type = PRIMARY_RECORD        Record Attributes =  NULL_BITMAP    Record Size = 18

Memory Dump @0x0000000013D6A096

0000000000000000:   10000f00 44444444 44014646 46464604 0000      ....DDDDD.FFFFF...

Slot 3 Column 1 Offset 0x4 Length 5 Length (physical) 5

a = DDDDD

Slot 3 Column 2 Offset 0x9 Length 1 (Bit position 0)

b = 1

Slot 3 Column 3 Offset 0xa Length 5 Length (physical) 5

c = FFFFF

Slot 3 Column 4 Offset 0x9 Length 1 (Bit position 1)

d = 0

DBCC 执行完毕。如果 DBCC 输出了错误信息,请与系统管理员联系。

更直观的比较

1、10000f00 41414141 41034242 42424204 0000

2、10000f00 42424242 42004343 43434304 0000

3、10000f00 44444444 44014646 46464604 0000

4、10000f00 44444444 44014646 46464604 0000

二进制的中间分别为 03 00  02  01

insert A1(a,b,c,d)
values(‘AAAAA‘,1,‘BBBBB‘,1)  
03

insert A1(a,b,c,d)
values(‘BBBBB‘,0,‘CCCCC‘,0)  
00

insert A1(a,b,c,d)
values(‘CCCCC‘,0,‘DDDDD‘,1)  
02

insert A1(a,b,c,d)
values(‘DDDDD‘,1,‘FFFFF‘,0)  
01

足以证明SQLServer 不管创建的顺序,都是试图压缩到一个BYTE上去,当然超过8位,会再一次申请一个新BYTE。

SQLServer BIT字段存储,布布扣,bubuko.com

时间: 2024-10-12 15:30:44

SQLServer BIT字段存储的相关文章

[原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式

我们日常开发中,不管是表设计问题抑或是其他什么原因,或多或少都会遇到一张表中有一个字段存储的内容是用逗号隔开的列表. 具体效果如下图: ------> 从左边图转换成右边图,像这种需求,我们难免会遇到. 今天我写了个存储过程来解决这种问题.主要方式是利用master..spt_values表. 具体存储过程如下: -- Author: LHM -- Create date: 2015-01-10 -- Description: 把表中某一个列按照逗号拼接列表 --示例: EXEC [Sp_Str

Mysql text类型字段存储提示错误信息 String data,right truncated:1406 Data too long for column 'content' at row 1

String data,right truncated:1406 Data too long for column 'content' at row 1 当Mysql提示如下时: 1.首先查看存入的文本内容的大小,根据内容大小设置类型 如果超出,根据大小进行更换类型 2.如果还是超出那可能是Mysql根据配置文件限制Server接受数据包大小,有时候大的插入和更新会受max_allowed_packet参数限制,导致写入或者更新失败. 查看目前配置: 代码如下: 以上说明目前的配置是:60 M

sqlserver 同字段值拼接 列转行

sqlserver 同字段值拼接(效果同oracle中的wm_concat) 2012-08-13 18:20:46|  分类: sqlserver |  标签:sqlserver   |举报 |字号大中小 订阅 合并列值  --******************************************************************************************* 表结构,数据如下:  id    value  ----- ------  1   

SQL 把表中字段存储的逗号隔开内容转换成列表形式

原文:[原创]SQL 把表中字段存储的逗号隔开内容转换成列表形式 我们日常开发中,不管是表设计问题抑或是其他什么原因,或多或少都会遇到一张表中有一个字段存储的内容是用逗号隔开的列表. 具体效果如下图: ------> 从左边图转换成右边图,像这种需求,我们难免会遇到. 今天我写了个存储过程来解决这种问题.主要方式是利用master..spt_values表. 具体存储过程如下: -- Author: LHM -- Create date: 2015-01-10 -- Description: 把

mysql 字段存储类型

摘自:http://zuo.ai.xiao.blog.163.com/blog/static/6079155320121293750732/ 1.数字类型                                          有符号                         无符号                                  存储(bytes)tinyint                            -128到127              

mssql sqlserver 将字段null(空值)值替换为指定值的三种方法分享

转自:http://www.maomao365.com/?p=6965   摘要: 下文将分享两种将字段中null值替换为指定值的方法分享,如下所示: 实验环境:sqlserver 2008 R2 例: create table test(keyId int identity, info varchar(30)) go insert into test(info)values('a'),('b'),(null),('d') go ---方法1:使用isnull替换 select keyId,is

Hive:map字段存储和取用 ( str_to_map函数 )

str_to_map(字符串参数, 分隔符1, 分隔符2) 使用两个分隔符将文本拆分为键值对. 分隔符1将文本分成K-V对,分隔符2分割每个K-V对.对于分隔符1默认分隔符是 ',',对于分隔符2默认分隔符是 '='. 例子: 1. 创建map字段 DROP TABLE IF EXISTS tmp.tmp_str_to_map; CREATE TABLE IF NOT EXISTS tmp.tmp_str_to_map ( ocolumn string comment '原始字段', rcolu

sqlServer去除字段中的中文

很多时候数据库表中某些字段是由中文和字母或数字组成,但有时我们又需要将字段中的中文去掉.想要实现这种需求的方法有很多,下面就是其中一种解决方法. 首先我们先建立测试数据 create table test( id int primary key identity(1,1), name varchar(20) not null ) insert into test(name) values('测试2') insert into test(name) values('测试a') insert int

SqlServer常用字段类型

bit:整型,取值范围[0,1,null],用于存取布尔值 tinyint:整型,取值范围[0~256) smallint:整型,取值范围[-215~215) int:整型,取值范围[-231~231) decimal:精确数值型 ,示例:decimal(8,4); //共8位,小数点右4位 numeric:与decimal类似 smallmoney:货币型 money:货币型 float:近似数值型 real:近似数值型 Smalldatetime:日期时间型,表示从1900年1月1日到207