简体   繁体   English

具有空值的Mysql列-空间要求是什么?

[英]Mysql column with null values - what are the space requirements?

I have a table with quite a lot entries. 我的桌子上有很多条目。 I need an additional column with an integer value or null. 我需要一个带有整数值或null的附加列。 The thing is that only very few rows will have that field populated. 事实是,只有很少的几行会填充该字段。

So i wonder whether its better to create a seperate table where i link the entries in an 1:1 relation. 因此,我想知道创建一个单独的表是否更好,在该表中以1:1关系链接条目。

I know one integer entry takes 4 bytes in mysql/myisam. 我知道一个整数项在mysql / myisam中需要4个字节。 If I have the column set to allow null values, and only 100 of 100 000 rows have the field populated, will the rest still consume 4 bytes for every null value? 如果我将列设置为允许空值,并且在100 000行中仅填充了100个字段,那么其余的每个空值是否仍会占用4个字节?

Or is mysql intelligent enough to set the value where it is populated and just regard everything as null, where nothing is set? 还是mysql足够智能,可以在填充它的位置设置值,而只是将所有内容都视为null,而没有设置任何内容?

This depends on the ROW_FORMAT value you give when you create your table. 这取决于创建表时提供​​的ROW_FORMAT值。

Before version 5.0.3, the default format is set to "REDUNDANT" : any fixed-length field will use the same space, even if it's value is NULL. 在版本5.0.3之前,默认格式设置为“ REDUNDANT”:任何定长字段将使用相同的空间,即使其值为NULL。

Starting with version 5.0.3, the value is set to "COMPACT" : NULL values will never use any space in your database. 从5.0.3版开始,该值设置为“ COMPACT”:NULL值将永远不会使用数据库中的任何空间。

You can do an ALTER TABLE to be sure to use the correct format : 您可以执行ALTER TABLE以确保使用正确的格式:

ALTER TABLE ... ROW_FORMAT=COMPACT

More details here : http://dev.mysql.com/doc/refman/5.1/en/data-size.html 此处有更多详细信息: http : //dev.mysql.com/doc/refman/5.1/en/data-size.html

As far as my understanding goes, once you declare a field as int, 4 bytes will be set aside for it. 据我所知,一旦您将一个字段声明为int,将为其留出4个字节。 So, for 100,000 rows you are looking at ~ 400 KB of space. 因此,对于100,000行,您正在寻找〜400 KB的空间。

If space is a constraint, then separate table will be better. 如果空间有限,那么单独的表会更好。 On the other hand, if performance is a criteria, then you'll have to take into account how many times that field is queried and whether it is checked for existence or non-existence. 另一方面,如果以性能为标准,则必须考虑查询该字段的次数以及是否检查该字段的存在或不存在。 In either case, you'll need a join. 无论哪种情况,都需要加入。 If you want to check whether the field is set you can use inner join, which will be slower than single table query. 如果要检查是否设置了该字段,则可以使用内部联接,这将比单表查询慢。 If you want to check for non-existence, you'll need left/right outer join which will be slower than inner join. 如果要检查不存在,则需要左/右外部联接,该联接比内部联接慢。

It will use bitfields to store nulls so it may need less than one byte. 它将使用位域存储空值,因此可能需要少于一个字节。 But, even if it did - who cares, unless you are using 3.5" floppies to store your backend in ;-) 但是,即使这样-谁在乎,除非您使用3.5英寸软盘将后端存储在;-)中

NULL in MySQL (Performance & Storage) MySQL中的NULL(性能和存储)

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

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