I wrote a statement that takes almost an hour to run so I am asking help so I can get to do this faster. So here we go:
I am making an inner join of two tables :
<
Not knowing what database system and version, I'd say that (lack of) indexing and the join clause could be causing the problem.
For every record in the measure table, you can have multiple records in the interval table (intervals.entry_time<=measures.time
), and for every record in the interval table, you can have multiple records in measure (measures.time <=intervals.exit_time
). the resulting one-to-many and many-to one relationships cause by the join means multiple table scans for each record. I doubt that Cartesian Product is the correct term, but it's pretty close.
Indexing would definitely help, but it would help even more if you could find a better key to join the two tables. having the one-to-many relationships going in one direction only would definitely speed up the processing as it wouldn't have to scan each table/index twice for each record.