views:

221

answers:

3

I would like to speed a MySQL query that basically retrieve a page of data following the pattern below

select
 my_field_A,
 my_field_B
where
 time_id >= UNIX_TIMESTAMP('1901-01-01  00:00:00') AND
 time_id < UNIX_TIMESTAMP('2009-01-16  00:00:00')

The field time_id is an MySQL index, yet, the query behaves as if the entire database was read at each query (retrieving a couple of lines being already quite slow). I not an expert in MySQL. Can someone guess what I am doing wrong?

+1  A: 

As there are probably lots of time_id's falling under these criteria, MySQL may think that the full table scan is better.

Try forcing the index:

SELECT
 my_field_A,
 my_field_B
FROM mytable FORCE INDEX (index_name_on_time_id)
WHERE
 time_id >= UNIX_TIMESTAMP('1901-01-01  00:00:00') AND
 time_id < UNIX_TIMESTAMP('2009-01-16  00:00:00')
Quassnoi
+3  A: 

Make sure you have an index (B-tree) on time_id, this should be efficient for range queries. Also make sure that time_id is in the appropriate time format.

If you really want to understand what mysql is doing you can add the keyword 'explain' infront of the query and run it in your mysql client. This will show some information about what mysql is doing and what kind of scans are performed.

http://dev.mysql.com/doc/refman/5.0/en/using-explain.html

Tomh
A: 

Do you need the lower range? Are there any entries earlier than 1901? How is the time_id column generated? If the time_id is always greater with each new entry being added into DB, you may want to consider finding ID with closest entry to 2009-01-16 and then select by ID

select my_field_A, my_field_B
FROM
  mytable
WHERE
  id <= ?

If that is not the case, try checking out partitioning in available from MySQL 5.1 and break down the table by years, that should increase speed dramatically.

michal kralik