indexing - Need an Index to speed up a MySQL Query -


i have mysql query takes long time process.

 select count(*) table1   col1='abc'  or (col2='xyz' , col3='mnl' , col4='efg')  or (col2='xyz' , col3='mnl' , col5='ijk') 

i need index query. in advance!

to query plan use indexes, may need create multiple indexes, , break "or" conditions in predicate separate queries, , combine results union all operators.

select sum(cnt) cnt   (           select sum(1) cnt            table1           col1 = 'abc'           union          select count(1) cnt            table1           col2='xyz'             , col3='mnl'             , (col4='efg' or col5='ijk')             , not (col1 <=> 'abc')        ) s 

note: use "union all" operator, avoid reducing count half, in corner case both queries happen return same value, , sure include inequality test on col1 in second query, row don't double counted.

these should sufficient covering indexes:

create index table1_ix1 on table1 (col1)  create index table1_ix2 on table1 (col2,col3,col4,col5,col1) 

verify each subquery can make use of index, examining output explain.


Comments