Skip navigation links

MySQL Forums


Advanced Search

Slow query: First record outside a time interval
Posted by: Daniel Abrahams ()
Date: November 07, 2009 05:30AM

For a certain operation I need to find the first record outside a certain time interval for a certain sensorid. My first guess for a query was:

SELECT * FROM samples WHERE sensorid=XXX AND timestamp >= 'XXXX-XX-XX XX:XX:XX' ORDER BY timestamp ASC LIMIT 0,1

SELECT * FROM samples WHERE sensorid=XXX AND timestamp <= 'XXXX-XX-XX XX:XX:XX' ORDER BY timestamp DESC LIMIT 0,1


However, for certain values of sensorid, these queries take a *very* long time to execute and shows up in the slow query log. It says in this log: 'Rows_examined: [almost the size of my table]'. It looks like MySQL doesn't use the indexes for sensorid and timestamp. Any idea what goes wrong here???



The table looks like this:

sampleid (primary key)
sensorid (bigint, indexed)
timestamp (datetime, indexed)
value (float)

It has over 20 million rows. The # of rows for a certain sensorid can be over 50k. The storage engine is InnoDB and the indexes are BTREEs. The database is running on a dedicated machine with 4GB RAM and serves no other databases.

Options: ReplyQuote


Subject Written By Posted
Slow query: First record outside a time interval Daniel Abrahams 11/07/2009 05:30AM
Re: Slow query: First record outside a time interval Pavel Bazanov 11/07/2009 09:07AM
Re: Slow query: First record outside a time interval Rick James 11/07/2009 04:14PM
Re: Slow query: First record outside a time interval Daniel Abrahams 11/07/2009 10:35PM
Re: Slow query: First record outside a time interval Daniel Abrahams 11/08/2009 07:40AM
Re: Slow query: First record outside a time interval Daniel Abrahams 11/08/2009 07:35AM


Sorry, only registered users may post in this forum.