Optimize SQL that uses between clause

前端 未结 19 1942
故里飘歌
故里飘歌 2021-01-11 18:03

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

相关标签:
19条回答
  • 2021-01-11 18:40

    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.

    0 讨论(0)
  • 2021-01-11 18:41

    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
    
    0 讨论(0)
  • 2021-01-11 18:44

    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 :)

    0 讨论(0)
  • 2021-01-11 18:45

    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.

    • read all B start/end/id tuples into a list, sorted on start time
    • read all A events
    • for each A event
      • find the largest start time <= event time (binary search will do fine)
      • if the event time is <= end time, add A to this B's list of events
      • else this B has no home
    0 讨论(0)
  • 2021-01-11 18:46

    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.

    0 讨论(0)
  • 2021-01-11 18:49

    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

    0 讨论(0)
提交回复
热议问题