Consider the following 2 tables:
Table A:
id
event_time
Table B
id
start_time
end_time
Every record in table A is mapped to exactly 1 reco
Personally if you havea one to many relationship and each record in table a only relates to one record in table b, I would store the table b id in table a and then do a regular join to get the data. What you currently have is a bad design that can never be truly efficient.
something like this?
SELECT A.id, B.id
FROM A
JOIN B ON A.id = B.id
WHERE A.event_time BETWEEN B.start_time AND B.end_time
I see that you are doing a cross join of two tables. That is not very good, and DBMS will take a lot of time to execute that operation. Cross join is the most exepensive operation in SQL. The reason of so long time of execution could be this.
Do on that way, it could resolve...
SELECT A.id, B.id FROM A, B WHERE A.id = B.id AND A.event_time BETWEEN B.start_time AND B.end_time
I hope this help you :)
If you can't change the schema -- in particular, if you can't add an index on a.event_time, I don't see much room for improvement at the SQL level.
I'd be more inclined to do it in code.
Notice that when running this query, you actually create 980x130000 records in memory before applying the condition. Such JOIN is not very recommended, and I can see why it'll give you performance issues.
Daremon, this answer is based on one of your comments where you said every record in table A maps to only one record in table B,
Can you add an additional table to your schema? If yes, you can pre-compute the result of this query and store it in another table. You will also have to keep this pre-computed table in sync with changes to tables A and B