Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you want to store boolean values or flags there are basically three options:</p> <ol> <li>Individual columns</li> </ol> <p>This is reflected in your example above. The advantage is that you will be able to put indexes on the flags you intend to use most often for lookups. The disadvantage is that this will take up more space (since the minimum column size that can be allocated is 1 byte.)</p> <p>However, if you're column names are really going to be field_20, field_21, etc. Then this is absolutely NOT the way to go. Numbered columns are a sign you should use either of the other two methods.</p> <ol> <li>Bitmasks</li> </ol> <p>As was suggested above you can store multiple values in a single integer column. A BIGINT column would give you up to 64 possible flags.</p> <p>Values would be something like: UPDATE table SET flags=b'100'; UPDATE table SET flags=b'10000';</p> <p>Then the field would look something like: 10100</p> <p>That would represent having two flag values set. To query for any particular flag value set, you would do</p> <p>SELECT flags FROM table WHERE flags &amp; b'100';</p> <p>The advantage of this is that your flags are very compact space-wise. The disadvantage is that you can't place indexes on the field which would help improve the performance of searching for specific flags.</p> <ol> <li>One-to-many relationship</li> </ol> <p>This is where you create another table, and each row there would have the id of the row it's linked to, and the flag:</p> <p>CREATE TABLE main ( main_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, );</p> <p>CREATE TABLE flag ( main_id INT UNSIGNED NOT NULL, name VARCHAR(16) );</p> <p>Then you would insert multiple rows into the flag table.</p> <p>The advantage is that you can use indexes for lookups, and you can have any number of flags per row without changing your schema. This works best for sparse values, where most rows do not have a value set. If every row needs all flags defined, then this isn't very efficient.</p> <p>For performance comparisson you can read a blog post I wrote on the topic: <a href="http://gtowey.blogspot.com/2009/01/three-types-of-sets-performance.html" rel="nofollow">Set Performance Compare</a></p> <p>Also when you ask which is "Best" that's a <em>very</em> subjective question. Best at what? It all really depends on what your data looks like and what your requirements are and how you want to query it.</p> <p>Keep in mind that if you want to do a query like: SELECT * FROM table WHERE some_flag=true</p> <p>Indexes will only help you if few rows have that value set. If most of the rows in the table have some_flag=true, then mysql will ignore indexes and do a full table scan instead.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload