简体   繁体   中英

Optimize mysql query to use index on a Bitwise where clause

I have a query which looks like:

select count(*) from m1
WHERE  (m1.`resource` & 1472 );

Although I have index on resource it doesn't use it. How can this be optimized to use on bitwise operation.

I do not believe MySQL can be made to use indexes for bitwise operations.

There's some discussion of this in the MySQL Performance forum: http://forums.mysql.com/read.php?24,35318 ("Are index scans possible with bitwise comparison?") where a MySQL employee suggests a solution based on having a table with one row per (thing,set-bit) pair and doing a bunch of joins. I'd guess that how well this works will depend a lot on your particular application.

According to http://dev.mysql.com/tech-resources/articles/mysql-set-datatype.html indexes aren't any use for doing the same sort of operations on SET values (which are implemented with integers and bitwise operations). I'd have thought that if there were any clever index optimization for bitwise operations it would already have been applied to SET s.

MySQL doesn't use indexes if it believes it has to perform calculations on each row in the table.

The only way to optimize these queries is to remove the calculation from the resource column. How you do that depends on what you want to achieve.

eg - This does not use index

where indexedCol*2 =6;

But this does -

where indexedCol =6/2;

Not sure if you can achieve that in your bitwise & operation though.

On Sql server, it does use index on bitwise operation. I am trying to reproduce the performance on MySql but cannot.... MySql is a great idea but if they do not consider fundammental performance on bitwise operation for example, this is a big disappointment indeed!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM