简体   繁体   English

(My)SQL:声明的SMALLINT(4)和INT(4)之间的负载差异是实数吗?

[英](My)SQL: Proclaimed load difference between SMALLINT(4) en INT(4) real?

According to this (Dutch) webpage , one should use SMALLINT if the expected number will not exceed 9999 (4 digits), because INT would reserve too much system resources (free interpretation). 根据此(荷兰语)网页 ,如果期望的数字不超过9999(4位数字),则应使用SMALLINT ,因为INT会保留过多的系统资源(自由解释)。 But does that still count if the digits are limited, as in SMALLINT(4) and INT(4) ? 但这是否仍然可以限制数字,例如SMALLINT(4)INT(4)

Firstly, the upper limit of a SMALLINT is not 9999 but 32767 - numbers are not stored in decimal, so the number of digits in the decimal representation of a number is irrelevant. 首先, SMALLINT上限不是9999,而是32767-数字不是以十进制存储的,因此数字的十进制表示形式中的数字位数是无关紧要的。 So while it is the correct type to use for numbers not exceeding 9999, it is also the correct type for numbers not exceeding 19999, for instance. 因此,例如,对于不超过9999的数字,它是正确的类型,对于不超过19999的数字,它也是正确的类型。

Secondly, the storage required for each type is given on the MySQL manual page describing the types . 其次,在描述类型的MySQL手册页上给出了每种类型所需的存储空间。 Larger types will certainly reserve more resources; 更大的类型肯定会保留更多资源; whether they reserve too much is another matter. 他们是否储备过多是另一回事。

The INT(4) syntax you referred to is a MySQL-specific feature discussed separately in the manual under Numeric Type Attributes . 您所指的INT(4)语法是MySQL特定的功能,在手册中“ 数字类型属性”下单独讨论。 According to that page, it represents a "display width", in digits, constraining the minimum width when displaying values. 在该页面上,它以数字表示“显示宽度”,限制了显示值时的最小宽度。 It specifically states that "The display width does not constrain the range of values that can be stored in the column." 它特别指出“显示宽度不限制可以存储在列中的值的范围”。 so the storage allocated could not be adjusted downwards based on that hint. 因此无法根据该提示向下调整分配的存储空间。

Before answer your question Frank, let's take a look on the size of types: 在回答您的问题弗兰克之前,让我们看一下类型的大小:

tinyint: 1 byte, -128 to +127 / 0 to 255 (unsigned)
smallint: 2 bytes, -32,768 to +32,767 / 0 to 65,535 (unsigned)
mediumint: 3 bytes, -8,388,608 to 8,388,607 / 0 to 16,777,215 (unsigned)
int/integer: 4 bytes, -2,147,483,648 to +2,147,483,647 / 0 to 4,294,967,295 (unsigned)
bigint: 8 bytes, -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807 / 0 to 18,446,744,073,709,551,615 (unsigned)

Every time you use INT(4), INT(3), INT(1) the number in the brackets is not about the storage, but how many you want to display/output. 每次使用INT(4),INT(3),INT(1)时,括号中的数字与存储无关,而是与要显示/输出的数目有关。 It is not related with the storage factor. 它与存储因子无关。 Which means, 183542 is gonna still be kept as it is in a INT(2) field. 这意味着183542将仍然保留在INT(2)字段中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM