Update:
See this blog post for performance details:
SELECT * FROM table WHERE field & number = number SELECT * FROM table WHERE field | number = number
This metric can be effective in two ways:
- To avoid early table scans (since the value for comparison is contained in the index itself)
- To limit the range of considered values.
None of the conditions in the queries above can be changed, this index will not be used to scan the range (with conditions such as now).
However, point 1 saved, and the index may be useful.
If your table contains, say, 100 bytes per row on average and 1,000,000 records, then scanning the table will need to scan 100 Mb data.
If you have an index (with a 4 byte key, a 6 byte line pointer and some internal overhead), the request will only need to scan 10 Mb data, plus additional data from the if table, the filter will be successful.
- A table scan is more efficient if your condition is not selective (you have a high probability of meeting the condition).
- Index scanning is more effective if your condition is selective (you have a low probability to match the condition).
Both of these queries will require a scan of the entire index.
But by rewriting the AND query, you can also take advantage of index ranking.
This condition:
field & number = number
can only match fields if the most significant bits of number are also set to field .
And you should just provide this additional condition for the request:
SELECT * FROM table WHERE field & number = number AND field >= 0xFFFFFFFF & ~((2 << FLOOR(LOG(2, 0xFFFFFFFF & ~number))) - 1)
This will use the range for coarse filtration and the conditions for fine filtration.
The more bits for number not set at the end, the better.