If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Speed up query with REPEAT?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 11-24-04, 09:25
japhy1 japhy1 is offline
Registered User
 
Join Date: Nov 2004
Posts: 16
Speed up query with REPEAT?

Hi,

I'm using MySQL 4.1 to query a large table (>100,000 records). The table is ordered (asc) by timestamp...My query is a time comparison, basically i am looking for all records that possess timestamps closest to (within 5 seconds of) a given time that i input.

so, say i input a time like '2003-01-01 00:00:10'. I'm looking for an output like
-> '2003-01-01 00:00:08', '2003-01-01 00:00:14',etc. (along with the accompanying data from those records)

i am currently using the TIMEDIFF function to evaluate the time difference between my inputted value and every record, i.e.
ABS(TIME_TO_SEC(TIMEDIFF('my_input','tbl_value'))) < 5.

It works but is slow...can anyone help with a faster method? maybe using ORDER BY, and REPEAT somehow to not have to evaluate every record but stop the query when it passes the time window?

thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 11-24-04, 12:33
guelphdad guelphdad is offline
Registered User
 
Join Date: Mar 2004
Posts: 440
Is your table indexed? 100,000 rows is not all that large compared to what mysql can handle.
Reply With Quote
  #3 (permalink)  
Old 11-24-04, 13:20
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
applying a function to a column value usually means that the index for that column will be ignored

yes, you should have an index

but you should also re-write your query like this --
Code:
 where timestampcolumn
       between 'low input value'
           and 'high input value'
in other words, use whatever application scripting language you're using to "pre-calculate" the bounds that you want, and then the index can be used

you should see a marked improvement in query speed
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 11-24-04, 16:27
japhy1 japhy1 is offline
Registered User
 
Join Date: Nov 2004
Posts: 16
Thank you, this was helpful...
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On