简体   繁体   English

使用BIT值将多列合并为一列

[英]Combining multiple columns into one using BIT values

In a given table I have multiple columns with a "yes"/ "no" value.... 在给定的表中,我有多列带有“是” /“否”值的列...

Table someTable

 is_shipped (ENUM) 'n','y' DEFAULT n
 is_paid_for (ENUM) 'n','y' DEFAULT n

Would it be a better idea to have the above fields as a BIT(2) coloumn which would effectively COMBINE the two fields into one field? 将上述字段作为BIT(2)列,将两个字段有效地组合为一个字段,是否是一个更好的主意?

Table someTable
 is_shipped_paid BIT(2)

Where each value of the bit would determine the value of the binary flag? 该位的每个值将决定二进制标志的值在哪里?

'0' = Not Shipped
'1' = Shipped

Hence the value of '01' for the above column would indicate that the item is Not Shipped and Is Paid For...etc... 因此,以上列的值'01'表示该商品未发货且已付款...等...

Any thoughts/comments/critique? 有什么想法/评论/批评吗?

So you save two or three bytes a row, consider that having a row at all needs at least 26 bytes of storage and for a table to be usable you need at least 10% free space, plus another 20% for a few indexes to make the rows accessible then the saving is not significant. 因此,您每行要保存两个或三个字节,请考虑完全拥有一行至少需要26个字节的存储空间,并且要使一个表可用,您至少需要10%的可用空间,另外还要有20%的空间才能创建一些索引可访问的行,则节省不大。

Especially when you consider the added complication of querying and updating these fields via standard SQL. 尤其是考虑到通过标准SQL查询和更新这些字段所带来的复杂性时。

Also consider that these days you buy disk in units of terabytes for about $150 per and a few bytes saved here and there just does not seem worth the bother. 还要考虑一下,这些天来,您以TB的价格购买磁盘,每GB大约需要$ 150,并且在此处保存了几个字节,似乎并不值得打扰。

In the 1980s when you bought disk by the K at great expense this sort of optimization was common: most large data centers then spent the next 30 years trying to fix the problems caused. 在1980年代,您花了大钱购买了K磁盘,这种优化很常见:大多数大型数据中心都在接下来的30年中试图解决所引起的问题。

If you really need to save disk space that badly look into enabling INNO DB compression and let the database do the work. 如果您确实需要节省磁盘空间,那么不建议您启用INNO DB压缩,然后让数据库完成工作。

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

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