简体   繁体   中英

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?

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...

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.

Especially when you consider the added complication of querying and updating these fields via standard 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.

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.

If you really need to save disk space that badly look into enabling INNO DB compression and let the database do the work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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