0

I have a big table which has around 7 million records. (MySQL)

Columns go like;

id | atype | textbody | .... 

id is primary key, atype is index

when I run

select * from tablename where `atype`='doit' 

it uses atype index. (there are 1.7 million doit rows in the table)

but when I run this query

select * from tablename where `atype`='doit' or `atype`='payment'

it doesn't use the index. I only says possible_index is atype. (there are 168 payment rows in the table)

is there any explanation of this behaviour?

If i run this query;

select * from tablename where `atype`='paymentfailed' or `atype`='payment'

It uses atype index.

so whenever I use 'doit', it doesn't use atype index

  • Of what use is an index if you try to grab 1.7 million records? – juergen d May 11 '16 at 01:01
  • select * from tablename where `atype`='doit' -> 1.7 million records select * from tablename where `atype`='payment' -> 168 records select * from tablename -> 7 million records – Hakan Meytan May 11 '16 at 01:06

1 Answers1

0

MySQL is rather finicky about indexes. I think it is smarter about IN rather than OR:

select *
from tablename
where `atype` in ('doit', 'payment');

If that doesn't work, then use union all:

select *
from tablename
where `atype` = 'doit'
union all
select *
from tablename
where `atype` = 'payment';
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786