限制行的大小有益于MySQL表大小吗? [英] Does limiting the size of rows benefit MySQL table size?

查看:95
本文介绍了限制行的大小有益于MySQL表大小吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,限制行大小将保持大小设置的最大值,但是,如果我将所有的行限制到他们将需要的最大值,这将提高行的性能(即占用更少的空间)



或者mysql是否动态分配行所需的大小,最大行长对此没有影响?

解决方案

MySQL不会动态调整字段大小;潜在的所需大小由您选择的数据类型决定。但是,您可以选择适当的数据类型来帮助优化数据库。



例如, TINYINT SMALLINT 数据类型需要1个字节,每个数据类型需要1个字节, 2字节,3字节,4字节和8字节。如果你只需要存储高达10,000的整数, SMALLINT 将是最好的选择,因为它可以存储高达65,535(无符号)的值,大量额外的空间和要求对于 VARCHAR

的变长类型, INT 的一半大小。



任何长度达到并包括255使用一个字节的前缀来存储长度。任何更多使用2字节前缀。如果可能,将 VARCHAR 限制为最多255个是最好的。作为示例,字符串 foo VARCHAR(3)字段和 VARCHAR(255)字段。如果字段的最大长度为500,例如 foo 将需要5个字节。



短(或有时甚至长)文本列,如果字符串总是具有相同的长度或非常接近的长度,则 CHAR 类型可能是有益的。此外,如果您使用的是MyISAM存储引擎,并且所有列都是固定长度的,您可以使用 FIXED 行格式,这是有益的。



有关MySQL数据类型的更多信息,请参见手动


I know that limiting the row size will keep the size at a set maximum, however, if i limit all my rows to the maximum that they will ever need, does that increase performance on the rows (i.e. take up less space)

Or does mysql dynamically allocate the size necessary for rows and maximum row length has no affect on it?

解决方案

MySQL will not dynamically size fields; the potential required size is determined by the data type you choose. However, you can choose appropriate data types to help optimize the database.

For example, the TINYINT, SMALLINT, MEDIUMINT, INT, and BIGINT data types require 1 byte, 2 bytes, 3 bytes, 4 bytes, and 8 bytes respectively. If you are only ever going to store integers up to 10,000, SMALLINT would be the best choice since it can store values up to 65,535 (unsigned) wioth plenty of extra room and requires half of the size of an INT.

With variable length types like VARCHAR any length up to and including 255 uses a one byte prefix to store the length. Anything more uses a 2 byte prefix. Limiting VARCHARs to at most 255 if possible is best. As an example, the string foo will require 4 bytes in both a VARCHAR(3) field and a VARCHAR(255) field. If the max length of the field was 500, for example, foo would require 5 bytes.

When possible with short (or sometimes even long) text xolumns, the CHAR type can be beneficial if the strings always have the same length or a very close length. Also, if you're using the MyISAM storage engine and all of your columns are fixed length, you can use the FIXED row format, which is beneficial.

More information about the MySQL data types is available in the manual.

这篇关于限制行的大小有益于MySQL表大小吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆