Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the storage size of BIT(1)?

I'd always thought that the storage size of a BIT(1) column is 1 bit.

But http://dev.mysql.com/doc/refman/5.6/en/storage-requirements.html

states that it is approximately (1+7)/8 bytes = 1 byte = 8 bits.

If I have 32000 rows of BIT(1) data, does it mean that it takes 32000 * (1+7)/8 bytes = 32000 bytes of storage ?

like image 813
Pacerier Avatar asked Oct 16 '11 16:10

Pacerier


People also ask

What is bit byte KB MB GB?

KB, MB, GB - A kilobyte (KB) is 1,024 bytes. A megabyte (MB) is 1,024 kilobytes. A gigabyte (GB) is 1,024 megabytes. A terabyte (TB) is 1,024 gigabytes. kb, Mb, Gb - A kilobit (kb) is 1,024 bits.

Is a byte always 8 bits?

byte: Abbreviation for binary term, a unit of storage capable of holding a single character. On almost all modern computers, a byte is equal to 8 bits. Large amounts of memory are indicated in terms of kilobytes, megabytes, and gigabytes.

Why do bytes have 8 bits?

The byte was originally the smallest number of bits that could hold a single character (I assume standard ASCII). We still use ASCII standard, so 8 bits per character is still relevant.

How many bytes is a number?

Whole numbers (integers) are usually represented with 4 bytes, or 32 bits. In the past, symbols (e.g., letters, digits) were represented with one byte (8 bits), with each symbol being mapped to a number between 0-255. The ASCII table provides the mapping.


2 Answers

BIT(M) approximately (M+7)/8 bytes

http://dev.mysql.com/doc/refman/5.0/en/storage-requirements.html

-> data needs to be aligned so mysql need multiples of byte

like image 172
aligot Avatar answered Sep 27 '22 19:09

aligot


What this means is that the database engine can't store just one bit. It will combine all bit fields in a table and store them together until it reaches the minimum size it can store on disk (say 4 bytes on a 32 bit machine). The next bit field will be stored in a second 4-byte memory slot.

So on this typical 32 bit machine, one bit or 32 bits will need 4 bytes, and 33 bits would need 8 bytes.

like image 33
Charles Bretana Avatar answered Sep 27 '22 20:09

Charles Bretana