Would storing my data bitwise make search results faster

72 views Asked by At

My tinyint field has 6 possible options, and for each search the user chooses which options are acceptable. Currently I'm storing each option as simply the values 0 through 6, and executing a separate clause in the WHERE for each acceptable option. Example (User selected option 0, 3, and 4):

SELECT * FROM table WHERE type = 0 OR type = 3 OR type = 4

I want to know if it would be faster if the data was stored as 1, 2, 4, 8, 16, and 32, and I performed a bitwise search.

SELECT * FROM table WHERE type & 25

I believe that the first option would normally be able to be indexed and then be faster, but this table is actually 43 fields, 33 of them search-able, including various operations. (The query often has a lot of ANDs by the time it's finished.) Not only would I need to index a lot of fields, but would that not negate the advantage of indexing?

1

There are 1 answers

1
Sergey Kalinichenko On BEST ANSWER

A good optimizer should produce roughly the same speed for both these options. I would opt for the solution that matches the logic better: if your program uses types 0 through 6, then storing numbers 0 through 6 in the database would be better.

You can make your query more readable by using the IN operator, too:

SELECT * FROM table WHERE type IN (0, 3, 4)