As you probably know, boolean values in software systems represent the states “true” or “false”. For some use cases in a software application, you may need a lot of them. To give you a very simplified example, imagine a form where the user can check which languages he or she speaks:
Let’s assume that every language option should be stored as a boolean value in a “users” database table, being either “true” or “false”, depending on which of the boxes are checked.
Technically, modern relational database systems such as MySQL can handle many columns per table just fine. However, adding new columns to a table while the database is on load is not that easy if the table already has millions of rows. This is a change that could take hours to complete, depending on the database system and data size. Additionally, there are limits of how many columns can be stored in a single table.
For quite some time, we have been using a nice technique to store boolean values of some large tables more efficiently as a single bit field. Using this approach, we can add new boolean attributes whenever we need to, without needing to alter the database tables, which would cause a downtime. For the part of our system that is based on the Ruby on Rails framework, we have solved this via our own plugin, that we are happy to share:
FlagShihTzu – A rails plugin to store a collection of boolean attributes in a single ActiveRecord column as a bit field.
As some of our other XING open source projects, the code is hosted on GitHub, a great website to share and collaborate on open source software. So if you have ideas to improve the plugin, please contribute!