View Single Post

   
  #3 (permalink)  
Old 02-28-2008, 09:47 AM
Joachim Durchholz
 
Posts: n/a
Default Re: Indexes And Sorting

nk schrieb:
> Any ideas? Is there a way to tell MySQL to order rows using a certain
> index?


Yes, but that should be the last resort.
It may be using the wrong index because its idea about the selectivity
of each index is wrong; this can be rectified via ANALYZE TABLE.

Other than that, I'd use Robert Klemme's advice and use an index on
(status, dateLast).

You may also try computing the cut-off date first, then write something like
SELECT field FROM table
WHERE dateLast < [cutoff_date] AND (status = 1)
ORDER BY dateLast ASC
LIMIT 10

I'm not sure that the optimizer knows that DATEDIFF is a monotonic
function. If it doesn't, it cannot really use the dateLast field just
because it is used in DATEDIFF(NOW(), dateLast) > 7.

In general, non-monotonous expressions make using an index almost
useless. Consider, for example,
SELECT foo FROM t WHERE SIN(foo) BETWEEN 0.1 AND 0.2
Using an index on foo is almost useless in this case. It's possible that
mysql cannot tell the difference between SIN(_) and DATEDIFF(NOW(),_),
and even adding another index might not change that.
The cutoff_date trick will always work, of course :-)

Regards,
Jo
Reply With Quote