Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8160893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:22:46+00:00 2026-06-06T18:22:46+00:00

What is the best solution for inserting boolean values on database if you want

  • 0

What is the best solution for inserting boolean values on database if you want more query performance and minimum losing of memory on select statement.

For example:
I have a table with 36 fields that 30 of them has boolean values (zero or one) and i need to search records using the boolean fields that just have true values.

SELECT * FROM `myTable`
WHERE 
    `field_5th` = 1 
    AND `field_12th` = 1 
    AND `field_20` = 1 
    AND `field_8` = 1

Is there any solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:22:49+00:00Added an answer on June 6, 2026 at 6:22 pm

    If you want to store boolean values or flags there are basically three options:

    1. Individual columns

    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.)

    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.

    1. Bitmasks

    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.

    Values would be something like:
    UPDATE table SET flags=b’100′;
    UPDATE table SET flags=b’10000′;

    Then the field would look something like: 10100

    That would represent having two flag values set. To query for any particular flag value set, you would do

    SELECT flags FROM table WHERE flags & b’100′;

    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.

    1. One-to-many relationship

    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:

    CREATE TABLE main (
    main_id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    );

    CREATE TABLE flag (
    main_id INT UNSIGNED NOT NULL,
    name VARCHAR(16)
    );

    Then you would insert multiple rows into the flag table.

    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.

    For performance comparisson you can read a blog post I wrote on the topic:
    Set Performance Compare

    Also when you ask which is “Best” that’s a very 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.

    Keep in mind that if you want to do a query like:
    SELECT * FROM table WHERE some_flag=true

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

best solution for use access in project for database with security i need to
what is the best solution in terms of performance and readability/good coding style to
What is the best solution if I want to make a OnTouch event repeat
I'm inserting approximently 3 million records in a database using this solution . Eventually
A) What is the best solution for regularly backing up large PostgreSQL database (version
what is the best solution to import large amounts of records into an MySQL
What's the best solution for authoring HTML5 in Emacs? Is there a mode that
What is the best solution to programmatically take a snapshot of a webpage? The
What is the best solution for many-to-many joins in Entity Framework. It looks like
I'm looking for the generally accepted/best solution to this problem. Problem I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.