| Revenue Source Veteran
Join Date: Oct 2005 Posts: 8,626 Jack of All Trades
CyberSpace
| Efficient Boolean value storage for Innodb Tables -
04-24-2008
Sometimes you have the task of storing multiple of boolean values (yes/now or something similar) in the table and if you get many columns and many rows you may want to store them as efficient way as possible.
For MyISAM tables you could use BIT(1) fields which get combined together for efficient storage: PLAIN TEXT
SQL: - [FONT='Courier New', Courier, monospace]CREATE TABLE `bbool` ([/font]
- [FONT='Courier New', Courier, monospace] `b1` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b2` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b3` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b4` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b5` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b6` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b7` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b8` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b9` bit(1) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `b10` bit(1) NOT NULL[/font]
- [FONT='Courier New', Courier, monospace]) ENGINE=MyISAM[/font]
- [FONT='Courier New', Courier, monospace] [/font]
- [FONT='Courier New', Courier, monospace]mysql> SHOW TABLE STATUS LIKE 'bbool' \G[/font]
- [FONT='Courier New', Courier, monospace]*************************** 1. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: bbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: MyISAM[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Fixed[/font]
- [FONT='Courier New', Courier, monospace] Rows: 10[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 7[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 70[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 1970324836974591[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 1024[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 00:41:01[/font]
- [FONT='Courier New', Courier, monospace] Update_time: 2008-04-24 00:45:40[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment:[/font]
- [FONT='Courier New', Courier, monospace]1 row IN SET (0.00 sec) [/font]
As you can see for MyISAM 10 columns take just 7 bytes - less than a byte per column. This is just minimum row length we can have for this table - myisam_data_pointer_size is 6 default plus we need space for delete flag which makes 7 minimum row size MyISAM can have in this configuration.
This trick however does not work for Innodb which allocates 1 byte for each BIT(1) column. So we can get 1 byte per column for boolean flag storage in Innodb (not accounting for standard row overhead) if we use BIT(1), TINYINT or ENUM types but can we do better ?
In fact we can - by using CHAR(0) type (without NOT NULL flag) - this will be pretty much column containing NULL bit only which can store one of two values - NULL or Empty String.
Lets see how these 3 different table format look in Innodb (I've populated each with some 2M rows so difference is more visible) PLAIN TEXT
SQL: - [FONT='Courier New', Courier, monospace]CREATE TABLE `tbool` ([/font]
- [FONT='Courier New', Courier, monospace] `t1` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t2` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t3` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t4` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t5` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t6` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t7` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t8` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t9` tinyint(4) NOT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `t10` tinyint(4) NOT NULL[/font]
- [FONT='Courier New', Courier, monospace]) ENGINE=InnoDB[/font]
- [FONT='Courier New', Courier, monospace] [/font]
- [FONT='Courier New', Courier, monospace]CREATE TABLE `cbool` ([/font]
- [FONT='Courier New', Courier, monospace] `c1` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c2` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c3` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c4` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c5` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c6` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c7` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c8` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c9` char(0) DEFAULT NULL,[/font]
- [FONT='Courier New', Courier, monospace] `c10` char(0) DEFAULT NULL[/font]
- [FONT='Courier New', Courier, monospace]) ENGINE=InnoDB DEFAULT CHARSET=latin1[/font]
- [FONT='Courier New', Courier, monospace] [/font]
- [FONT='Courier New', Courier, monospace]mysql> SHOW TABLE STATUS LIKE "%bool%" \G[/font]
- [FONT='Courier New', Courier, monospace]*************************** 1. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: bbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: InnoDB[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Compact[/font]
- [FONT='Courier New', Courier, monospace] Rows: 2097405[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 37[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 78233600[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 0[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 0[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 00:54:18[/font]
- [FONT='Courier New', Courier, monospace] Update_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment: InnoDB free: 6144 kB[/font]
- [FONT='Courier New', Courier, monospace]*************************** 2. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: cbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: InnoDB[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Compact[/font]
- [FONT='Courier New', Courier, monospace] Rows: 2097678[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 34[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 71942144[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 0[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 0[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 00:37:48[/font]
- [FONT='Courier New', Courier, monospace] Update_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment: InnoDB free: 4096 kB[/font]
- [FONT='Courier New', Courier, monospace]*************************** 3. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: tbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: InnoDB[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Compact[/font]
- [FONT='Courier New', Courier, monospace] Rows: 2097405[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 37[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 78233600[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 0[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 0[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 00:58:01[/font]
- [FONT='Courier New', Courier, monospace] Update_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment: InnoDB free: 6144 kB[/font]
- [FONT='Courier New', Courier, monospace]3 rows IN SET (0.11 sec) [/font]
As you can see table which uses BIT(1) column type takes same space as the one which uses TINYINT NOT NULL while CHAR(0) is about 10% smaller. This is modest space savings of course but considering large per row overhead Innodb has this will transform to much larger savings if you have hundreds of such columns.
Lets see how things look for MyISAM for same tables: PLAIN TEXT
SQL: - [FONT='Courier New', Courier, monospace]mysql> SHOW TABLE STATUS LIKE "%bool%" \G[/font]
- [FONT='Courier New', Courier, monospace]*************************** 1. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: bbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: MyISAM[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Fixed[/font]
- [FONT='Courier New', Courier, monospace] Rows: 2097152[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 7[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 14680064[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 1970324836974591[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 1024[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 01:14:06[/font]
- [FONT='Courier New', Courier, monospace] Update_time: 2008-04-24 01:14:09[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment:[/font]
- [FONT='Courier New', Courier, monospace]*************************** 2. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: cbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: MyISAM[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Fixed[/font]
- [FONT='Courier New', Courier, monospace] Rows: 2097152[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 7[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 14680064[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 1970324836974591[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 1024[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 01:14:13[/font]
- [FONT='Courier New', Courier, monospace] Update_time: 2008-04-24 01:14:17[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment:[/font]
- [FONT='Courier New', Courier, monospace]*************************** 3. row ***************************[/font]
- [FONT='Courier New', Courier, monospace] Name: tbool[/font]
- [FONT='Courier New', Courier, monospace] Engine: MyISAM[/font]
- [FONT='Courier New', Courier, monospace] Version: 10[/font]
- [FONT='Courier New', Courier, monospace] Row_format: Fixed[/font]
- [FONT='Courier New', Courier, monospace] Rows: 2097152[/font]
- [FONT='Courier New', Courier, monospace] Avg_row_length: 11[/font]
- [FONT='Courier New', Courier, monospace] Data_length: 23068672[/font]
- [FONT='Courier New', Courier, monospace]Max_data_length: 3096224743817215[/font]
- [FONT='Courier New', Courier, monospace] Index_length: 1024[/font]
- [FONT='Courier New', Courier, monospace] Data_free: 0[/font]
- [FONT='Courier New', Courier, monospace] AUTO_INCREMENT: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_time: 2008-04-24 01:14:23[/font]
- [FONT='Courier New', Courier, monospace] Update_time: 2008-04-24 01:14:26[/font]
- [FONT='Courier New', Courier, monospace] Check_time: NULL[/font]
- [FONT='Courier New', Courier, monospace] Collation: latin1_swedish_ci[/font]
- [FONT='Courier New', Courier, monospace] Checksum: NULL[/font]
- [FONT='Courier New', Courier, monospace] Create_options:[/font]
- [FONT='Courier New', Courier, monospace] Comment:[/font]
- [FONT='Courier New', Courier, monospace]3 rows IN SET (0.00 sec) [/font]
As you can see for MyISAM BIT(1) NOT NULL type is as compact as CHAR(0) while TINYINT NOT NULL is a bit less compact.
Looking at results of these tests using CHAR(0) is the most efficient if you would like optimal structure both for MyISAM and Innodb tables, however it is not as convenient to work with. Using NULL as one of flag values means you can't use normal "=" comparison operator with them: PLAIN TEXT
SQL: - [FONT='Courier New', Courier, monospace]mysql> SELECT count(*) FROM cbool WHERE c1=NULL;[/font]
- [FONT='Courier New', Courier, monospace]+----------+[/font]
- [FONT='Courier New', Courier, monospace]| count(*) |[/font]
- [FONT='Courier New', Courier, monospace]+----------+[/font]
- [FONT='Courier New', Courier, monospace]| 0 |[/font]
- [FONT='Courier New', Courier, monospace]+----------+[/font]
- [FONT='Courier New', Courier, monospace]1 row IN SET (0.20 sec) [/font]
You can use IS NULL operator which is painful because you need to have different query based on parameter (IS '' would not work) or you can use Null-Aware comparison operator: PLAIN TEXT
SQL: - [FONT='Courier New', Courier, monospace]mysql> SELECT count(*) FROM cbool WHERE c1NULL;[/font]
- [FONT='Courier New', Courier, monospace]+----------+[/font]
- [FONT='Courier New', Courier, monospace]| count(*) |[/font]
- [FONT='Courier New', Courier, monospace]+----------+[/font]
- [FONT='Courier New', Courier, monospace]| 1048576 |[/font]
- [FONT='Courier New', Courier, monospace]+----------+[/font]
- [FONT='Courier New', Courier, monospace]1 row IN SET (0.22 sec) [/font]
Should you go and change all flags to use this approach ? I do not think so - for most applications using TINYINT BIT(1) or ENUM for flags benefit would unlikely be worth the trouble. Due to complication I also would not recommend as a base approach for new applications. However in special cases if you have very many rows and very many flag values to deal with which you can't pack to the bitmask this approach can be quite helpful.
Entry posted by peter | No comment
Add to:  |  |  |  | Efficient Boolean value storage for Innodb Tables - Read More... |