简体   繁体   English

Mysql:为什么我在定义表时使用适当的数据大小并选择最佳数据长度

[英]Mysql: why should I use appropriate data sizes and choose the optimal data length while defining tables

I have a table which stores the details of users. 我有一个表,用于存储用户的详细信息。 I can hardly have 10K values. 我几乎没有10K的值。 Its ID field is defined as bigint(20), which can hold even big big data ranges. 它的ID字段定义为bigint(20),可以容纳更大的大数据范围。

Now, changing it to SMALLINT will bring any favour wither in terms of performance or storage...? 现在,将其更改为SMALLINT会带来性能或存储方面的青睐吗? Shall some one plase explain how will it. 请一个人解释一下。

i create two small tables 我创建两个小桌子
one with ID as INT(10) ID为INT(10)的一个
another with ID as INT(100) 另一个ID为INT(100)的对象

I inserted 513 rows into each. 我在每一行中插入了513行。 when i see the show create table of each of them, i did not see any change in the data sizes or index size. 当我看到它们各自的显示创建表时,我没有看到数据大小或索引大小的任何变化。 they are MYISAM tables. 它们是MYISAM表。 Then whats the better thing in choosing SMALLINT than int(100) or INT(10) 那么在选择SMALLINT方面比int(100)或INT(10)更好的事情是什么

Here is that info 这是信息

| id    | int(10) | NO   | PRI | NULL    | auto_increment |
| size  | int(10) | YES  |     | NULL    |                |
Data_length: 4617
Index_length: 8192


| id    | int(100) | NO   | PRI | NULL    | auto_increment |
| size  | int(10)  | YES  |     | NULL    |                |
Data_length: 4617
Index_length: 8192

There's an important distinction here between the data type and the "length". 数据类型和“长度”之间有一个重要的区别。

int(10) and int(100) are actually the same data type, so they will both take up 4 bytes. int(10)和int(100)实际上是相同的数据类型,因此它们都将占用4个字节。 The "10" and "100" just affect the way the data is displayed, not the way it is stored. “ 10”和“ 100”仅影响数据的显示方式,而不影响数据的存储方式。

Choosing the data type is a trade-off between storage efficiency and the flexibility to store a greater range of values. 选择数据类型是在存储效率和存储更大范围值的灵活性之间进行权衡。

Here's a helpful chart from the manual : 这是手册中的有用图表:

Type    Storage     Minimum Value       Maximum Value
        (Bytes)     (Signed/Unsigned)   Signed/Unsigned)
TINYINT     1   -128    127
                0   255
SMALLINT    2   -32768  32767
                0   65535
MEDIUMINT   3   -8388608    8388607
                0   16777215
INT     4   -2147483648     2147483647
                0   4294967295
BIGINT  8   -9223372036854775808    9223372036854775807
                0   18446744073709551615

One reason to be more specific in the field sizes is simply because it reduces the amount of mistakes. 在字段大小中更具体的原因之一仅仅是因为它减少了错误的数量。 The more you limit someones particular freedom, the more accurate the information will be. 您对某人的特定自由的限制越多,信息就越准确。 That's why on a lot of sites when you register and it has drop down lists for what country, or what state you live in. This reduces the amount of options the user has, thus maintaining safe practices. 这就是为什么在许多站点上注册时会在下拉列表中列出您所居住的国家或地区的列表。这会减少用户拥有的选项数量,从而保持安全做法。 If a site allowed you to type in your state, think of every example you would get from different users. 如果某个站点允许您输入状态,请考虑从不同用户那里得到的每个示例。 For example, Florida would be: 例如,佛罗里达州将是:

  • Florida 佛罗里达
  • FL FL
  • fl FL
  • Folrida 福里达

If you notice, I misspelt Florida wrong in the last example. 如果您注意到,在上一个示例中,我会错失佛罗里达州的错误。 Now imagine trying to run a query on users from Florida, which one would you choose? 现在想象一下要对佛罗里达州的用户执行查询,您会选择哪一个? You would have to take into account every option that a user could input. 您将必须考虑用户可以输入的每个选项。 Limiting their freedom so you KNOW what is inside the field is the best way. 限制他们的自由,以便您知道领域内的内容是最好的方法。

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

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