List:General Discussion« Previous MessageNext Message »
From:Baron Schwartz Date:October 25 2007 10:24am
Subject:Re: Index usage for simple query
View as plain text  
Hi,

Colin Martin wrote:
> Hi there,
> 
> Can someone please explain why when the query below uses one constant in 
> the WHERE clause, MySQL decides to use the index on the 'source' column, 
> and why in the second query where there are two constants, it decides 
> not to?
> 
> Is there a way to get MySQL to use the index for the second query?
> 
> mysql> explain select * from data_total where source=8;
> 
> | id | select_type | table      | type | possible_keys | key    |
> |  1 | SIMPLE      | data_total | ref  | source        | source |
> 
> 
> mysql> explain select * from data_total where (source=8 or source=9);
> 
> | id | select_type | table      | type | possible_keys | key  |
> |  1 | SIMPLE      | data_total | ALL  | source        | NULL |
> 
> (Output chopped for better formatting)

I'd need more info to know for sure, but I'm guessing you're using 
pre-MySQL 5 or MySQL is deciding it's cheaper to scan the table.  If 
it's too slow, try using an IN(8, 9) clause instead and see if that 
helps.  Or try turning the query into a UNION.

Baron
Thread
Index usage for simple queryColin Martin25 Oct
  • Re: Index usage for simple queryBaron Schwartz25 Oct
    • Re: Index usage for simple queryColin Martin25 Oct
      • Re: Index usage for simple queryBaron Schwartz25 Oct
        • Re: Index usage for simple queryJoerg Bruehe25 Oct
          • Re: Index usage for simple queryBaron Schwartz25 Oct