+---------------------------+
| TRUE |
| FALSE |
| TRUE |
+---------------------------+
3 ROWS IN SET (0.00 sec)
看起来很不错,而且只占用一个bit,是不是很节省空间呢(这个…其实我不太确定,求分析)。
注:bit(m)与int(m)是完全不同的,bit(m)表示使用几个bit(取值范围1-64)来做存储,int(m)表示在zerofill时候的Display width。
/mysql-5.6.2-labs-innodb-memcached/sql/field.h
77 class Field
78 {
191 /*
192 pack_length() returns size (in bytes) used to store field data in memory
193 (i.e. it returns the maximum size of the field in a row of the table,
194 which is located in RAM).
195 */
196 virtual uint32 pack_length() const { return (uint32) field_length; }
198 /*
199 pack_length_in_rec() returns size (in bytes) used to store field data on
200 storage (i.e. it returns the maximal size of the field in a row of the
201 table, which is located on disk).
202 */
203 virtual uint32 pack_length_in_rec() const { return pack_length(); }
215 /*
216 data_length() return the "real size" of the data in memory.
217 */
218 virtual uint32 data_length() { return pack_length(); }
2215 class Field_bit :public Field {
2269 uint32 pack_length() const { return (uint32) (field_length + 7) / 8; }
2270 uint32 pack_length_in_rec() const { return bytes_in_rec; }
在内存中占用了 (1+7)/8 = 1个字节,在disk中占用bytes_in_rec个字节,具体是多少不清楚,会不会跟存储引擎相关的…求解惑。
innodb为每个bit列使用一个足够存储的最小整数类型来存放,所以不能节省存储空间。
7) BIT(n)
由bit很容易联想到bitmask。如果有多个字段都是bool型的,就可以使用bitmask把它们组合起来。
不过缺点也很多:
首先,它不够直观,在缺少文档的情况下,无法得知每一位的意义。
其次,位操作的代码比较难写,别人也很难看懂你写的代码。
最后,无法在此字段上使用索引。
结论
推荐使用bool或者tinyint类型。