Optimize SQL that uses between clause

前端 未结 19 1945
故里飘歌
故里飘歌 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条回答
  •  -上瘾入骨i
    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

提交回复
热议问题