Query optimization when no row is returned I am using explain to figure out what is happening in my query which is:

explain select t from c where u1_id = 1 group by t;


I see "Using where" in the Extra column of the resultset. But this only happens if I use a valid u1_id. So if I run the same query, on the same server but with an invalid u1_id I get "Using where; Using temporary; Using filesort" in the Extra column.

I assume this means that when the user_id is not in the table then MySQL takes another route to process the query. Does anyone know why this happens and how can I prevent this from happening?

EDIT

Here is my table:

CREATE TABLE  c  (
   pk  int(11) NOT NULL AUTO_INCREMENT,
   u1_id  int(10) unsigned NOT NULL,
   u2_id  int(10) unsigned NOT NULL,
   s  decimal(11,5) NOT NULL DEFAULT ''0.00000'',
   date  datetime NOT NULL,
   f  decimal(11,5) NOT NULL,
   t  enum(''F'',''P'') ,
   ti  decimal(11,5) DEFAULT ''0.00000'',
   ts  decimal(11,5) DEFAULT ''0.00000'',
   latest  tinyint(1) DEFAULT ''0'',
   sc  decimal(11,5) DEFAULT ''0.00000'',
   p_id  int(10) unsigned NOT NULL,
  PRIMARY KEY ( pk ),
  KEY  i1  ( u1_id ),
  KEY  i2  ( u2_id ),
  KEY  i3  ( u1_id , u2_id ),
  KEY  i4  ( sc ),
  KEY  i5  ( sc , u2_id , latest ),
  KEY  i5  ( p_id ),
  KEY  i8  ( ti ),
  KEY  i9  ( t ),
  KEY  i10  ( p_id , u1_id , latest , t ),
 )
The best index for this query is (u1_id, t)

My initial guess was right, that you have indexes on (t) alone and on (u1_id) alone. You haven't told us the exact EXPLAIN output (which index is used), so the most probable explanation is that mysql is choosing to use one of these existing indexes or none at all (doing a full scan of the table), which yields the not so efficient plan.