MySQL Forums
Forum List  »  Performance

Re: are index scans possible with bitwise comparison
Posted by: Jay Pipes
Date: July 21, 2005 07:14AM

When you say "binary" values, I think you are referring to a set of boolean flags? (not quite the same thing). In the case of bitwise operators, there are times when you can transform the equation to isolate the field on one side of an equation, in which case you can use an index (seek, not scan, BTW). But, for the example you used above, and most other examples using bitwise operators, indexes will not be used because there is no way to optimize the index seek since MySQL is not looking at the value of the index record, but instead a conversion of it. This is similar to why an index will not be used if you do something like WHERE TO_DAYS(some_date_field) > TO_DAYS(CURRENT_DATE()) - 20.

Anyway, there are numerous efficient ways to store this kind of information. Depending on the number of elements you wish to track, you can use an SET column type or use a many-to-many relationship. In the case of the SET column type, there are limitations on the number of elements you can store in the field; the size of the storage will depend on the number of elements in the field. However, depending on the operation you wish to use, the SET column type sometimes falls victim to the same indexing dilemma as the bitwise operator.

A more flexible solution, which can be set up to always use an efficient index, and store only the minimal amount of information, would be to use a many to many relationship table, where you have a primary key composed of the foriegn key identifier for the main record and an identifier for the "bit" which is turned on. One record per bit that is currently on. You can use BIT data type in the most recent MySQL version, or TINYINT UNSIGNED in your version.

I recently went over this topic in depth here:

http://forums.mysql.com/read.php?10,33416,33416

and here:

http://forums.mysql.com/read.php?10,34444,34444

Hope this helps,

Jay Pipes
Community Relations Manager, North America, MySQL Inc.

Got Cluster? http://www.mysql.com/cluster
Personal: http://jpipes.com

Options: ReplyQuote


Subject
Views
Written By
Posted
Re: are index scans possible with bitwise comparison
6492
July 21, 2005 07:14AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.