我最好使用哪种数据类型在sql server中存储标志字段?

时间:2021-07-30 16:57:22

What data type I should preferably use for storing flag fields in sql server, bit(if I have only 0,1 and null), tinyint, smallint or int for flags which cannot be accommodated into bit? Does it effect my query performance?

我应该优选使用哪种数据类型来存储sql server中的标志字段,bit(如果我只有0,1和null),tinyint,smallint或int用于不能容纳到位的标志?它会影响我的查询性能吗?

3 个解决方案

#1


4  

Use Bit Datatype which is set to 1 if true or 0 ir False, much smaller and faster to query against Bit values Your column could be saved as 1 Byte if you have 8 or less Bit columns in your table and I think if you have more then 8 then SQL Server uses 2 Bytes Column , Which is A lot smaller than any other options you might have, tinyint, smallint and int all requires more space then a bit column and querying a bigger datatype means more time to query the data.
My Suggestion
My Suggestion would be to use Bit Column as it requires less space to store it and queries can perform much faster.

使用位数据类型,如果为真,则设置为1,或者如果为0,则设置为False,对于位值查询要小得多,速度要快得多如果表中有8位或更少的位列,我的列可以保存为1字节,我想如果你有更多然后8然后SQL Server使用2字节列,这比你可能有的任何其他选项小很多,tinyint,smallint和int都需要更多的空间然后一个位列和查询更大的数据类型意味着更多的时间来查询数据。我的建议我的建议是使用Bit Column,因为它需要更少的空间来存储它,查询可以更快地执行。

#2


1  

bit

its a boolean, so its only 1 or 0, and doesn't require a whole byte to store. Plus you can just make it nullable on the table design.

它是一个布尔值,因此它只有1或0,并且不需要存储整个字节。此外,您可以在桌面设计上使其可以为空。

#3


1  

I got the answer for what I was looking, we should consider using smallest data type sufficient enough to store flag values required. This is with respect to the performance of database as storage size is not a big concern these days.

我得到了我正在寻找的答案,我们应该考虑使用足够的最小数据类型来存储所需的标志值。这与数据库的性能有关,因为存储大小近来并不是一个大问题。

have a look at this link for more insights.

请查看此链接以获取更多见解。

As per this link think about how resources other than disk space are used, think about buffer pool and storage bandwidth. At the extreme end, CPU cache and memory bus bandwidth, and also about how SQL Server works.

根据此链接考虑如何使用磁盘空间以外的资源,请考虑缓冲池和存储带宽。最极端的是CPU缓存和内存总线带宽,以及SQL Server的工作原理。

#1


4  

Use Bit Datatype which is set to 1 if true or 0 ir False, much smaller and faster to query against Bit values Your column could be saved as 1 Byte if you have 8 or less Bit columns in your table and I think if you have more then 8 then SQL Server uses 2 Bytes Column , Which is A lot smaller than any other options you might have, tinyint, smallint and int all requires more space then a bit column and querying a bigger datatype means more time to query the data.
My Suggestion
My Suggestion would be to use Bit Column as it requires less space to store it and queries can perform much faster.

使用位数据类型,如果为真,则设置为1,或者如果为0,则设置为False,对于位值查询要小得多,速度要快得多如果表中有8位或更少的位列,我的列可以保存为1字节,我想如果你有更多然后8然后SQL Server使用2字节列,这比你可能有的任何其他选项小很多,tinyint,smallint和int都需要更多的空间然后一个位列和查询更大的数据类型意味着更多的时间来查询数据。我的建议我的建议是使用Bit Column,因为它需要更少的空间来存储它,查询可以更快地执行。

#2


1  

bit

its a boolean, so its only 1 or 0, and doesn't require a whole byte to store. Plus you can just make it nullable on the table design.

它是一个布尔值,因此它只有1或0,并且不需要存储整个字节。此外,您可以在桌面设计上使其可以为空。

#3


1  

I got the answer for what I was looking, we should consider using smallest data type sufficient enough to store flag values required. This is with respect to the performance of database as storage size is not a big concern these days.

我得到了我正在寻找的答案,我们应该考虑使用足够的最小数据类型来存储所需的标志值。这与数据库的性能有关,因为存储大小近来并不是一个大问题。

have a look at this link for more insights.

请查看此链接以获取更多见解。

As per this link think about how resources other than disk space are used, think about buffer pool and storage bandwidth. At the extreme end, CPU cache and memory bus bandwidth, and also about how SQL Server works.

根据此链接考虑如何使用磁盘空间以外的资源,请考虑缓冲池和存储带宽。最极端的是CPU缓存和内存总线带宽,以及SQL Server的工作原理。