SQL:如果我的最大值是255,那么使用tinyint而不是Integer是否有效?

时间:2021-10-25 16:57:40

Lets assume I want to save the count of datagrid rows which can be max. 24 because each row is 1 hour.

让我们假设我想保存datagrid行的数量,这可以是max。 24因为每行是1小时。

To save the row index in the database a tinyint field would be totally enough. But back in my mind I remember slightly that databases are optimized for integers?!

要在数据库中保存行索引,tinyint字段就足够了。但是在我的脑海里,我稍微记得数据库是针对整数进行优化的吗?!

So is it worth to use tinyint?

那么使用tinyint是否值得?

4 个解决方案

#1


9  

With a narrower table, the database will fit more records in a single IO page, and will therefore require fewer hard disk reads.

使用较窄的表,数据库将在单个IO页面中容纳更多记录,因此将需要更少的硬盘读取。

The rule of thumb is to always use the data type that will require the least storage size.

经验法则是始终使用需要最小存储大小的数据类型。

#2


3  

Generally, less space the better, as the more rows can fit on a signle 8k I/O Page on disk (or in memory), the fewer I/Os are required to search and/or retrieve data... This is especially important for columns used in indices. However, if your machine is, for example, a 32 bit machine and is running a 32 bit OS, then the smallest memory chunk that can be independantly addressed is 32 bits, so if this is the only column in your table schema that is smaller than 32 bits, then it doesn't matter because each full row of data must start and end on a 32 bit boundary, so each row must be a multiple of 32 bits wide.

通常,空间越小越好,因为磁盘(或内存)上的8k I / O页面上可以容纳的行越多,搜索和/或检索数据所需的I / O就越少......这一点尤其重要对于索引中使用的列。但是,如果您的计算机是32位计算机并运行32位操作系统,那么可以独立寻址的最小内存块是32位,因此如果这是表模式中唯一较小的列因为每个完整的数据行必须在32位边界上开始和结束,所以每个行必须是32位宽的倍数,而不是32位。

i.e., if your table was

即,如果你的桌子是

MyTable(ColA tinyint, ColB Int, ColC DateTime) Then each row will take 16 bytes (128 bits) and 24 bits will be wasted.

MyTable(ColA tinyint,ColB Int,ColC DateTime)然后每行将占用16个字节(128位),浪费24位。

On the other hand if you have 4 columns that could be tinyInts, then by all means use that as SQL server will put four of them in one 32 bit storage location on disk (no matter what order you declare them in).

另一方面,如果你有4列可能是tinyInts,那么一定要使用它,因为SQL服务器会将其中的四个放在磁盘上的一个32位存储位置(无论你声明它们的顺序如何)。

Same principles apply to 64bit SQL Server running on 64bit OS/CPUbB

同样的原则适用于在64位OS / CPUbB上运行的64位SQL Server

#3


2  

tinyint

TINYINT

less space is good.

空间越小越好。

#4


2  

Tinyint is an integer, and it'd be faster than INT because TINYINT takes less bytes (1 byte) than the INT data type (4 bytes).

Tinyint是一个整数,它比INT快,因为TINYINT比INT数据类型(4个字节)占用更少的字节(1个字节)。

Reference:

参考:

#1


9  

With a narrower table, the database will fit more records in a single IO page, and will therefore require fewer hard disk reads.

使用较窄的表,数据库将在单个IO页面中容纳更多记录,因此将需要更少的硬盘读取。

The rule of thumb is to always use the data type that will require the least storage size.

经验法则是始终使用需要最小存储大小的数据类型。

#2


3  

Generally, less space the better, as the more rows can fit on a signle 8k I/O Page on disk (or in memory), the fewer I/Os are required to search and/or retrieve data... This is especially important for columns used in indices. However, if your machine is, for example, a 32 bit machine and is running a 32 bit OS, then the smallest memory chunk that can be independantly addressed is 32 bits, so if this is the only column in your table schema that is smaller than 32 bits, then it doesn't matter because each full row of data must start and end on a 32 bit boundary, so each row must be a multiple of 32 bits wide.

通常,空间越小越好,因为磁盘(或内存)上的8k I / O页面上可以容纳的行越多,搜索和/或检索数据所需的I / O就越少......这一点尤其重要对于索引中使用的列。但是,如果您的计算机是32位计算机并运行32位操作系统,那么可以独立寻址的最小内存块是32位,因此如果这是表模式中唯一较小的列因为每个完整的数据行必须在32位边界上开始和结束,所以每个行必须是32位宽的倍数,而不是32位。

i.e., if your table was

即,如果你的桌子是

MyTable(ColA tinyint, ColB Int, ColC DateTime) Then each row will take 16 bytes (128 bits) and 24 bits will be wasted.

MyTable(ColA tinyint,ColB Int,ColC DateTime)然后每行将占用16个字节(128位),浪费24位。

On the other hand if you have 4 columns that could be tinyInts, then by all means use that as SQL server will put four of them in one 32 bit storage location on disk (no matter what order you declare them in).

另一方面,如果你有4列可能是tinyInts,那么一定要使用它,因为SQL服务器会将其中的四个放在磁盘上的一个32位存储位置(无论你声明它们的顺序如何)。

Same principles apply to 64bit SQL Server running on 64bit OS/CPUbB

同样的原则适用于在64位OS / CPUbB上运行的64位SQL Server

#3


2  

tinyint

TINYINT

less space is good.

空间越小越好。

#4


2  

Tinyint is an integer, and it'd be faster than INT because TINYINT takes less bytes (1 byte) than the INT data type (4 bytes).

Tinyint是一个整数,它比INT快,因为TINYINT比INT数据类型(4个字节)占用更少的字节(1个字节)。

Reference:

参考: