Optimize mysql query to use index on a Bitwise where clause

后端 未结 3 1381
梦如初夏
梦如初夏 2021-02-10 10:52

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

3条回答
  •  南方客
    南方客 (楼主)
    2021-02-10 11:10

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

提交回复
热议问题