Optimize mysql query to use index on a Bitwise where clause

后端 未结 3 1374
梦如初夏
梦如初夏 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:15

    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.

提交回复
热议问题