Fast page retrieval in MySQL, index usage?
-
03-07-2019 - |
Question
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?
Solution
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.
OTHER TIPS
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')
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.