Optimize SQL that uses between clause

前端 未结 19 1944
故里飘歌
故里飘歌 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:24

    You may want to try something like this

    Select A.ID,
    (SELECT B.ID FROM B
    WHERE A.EventTime BETWEEN B.start_time AND B.end_time LIMIT 1) AS B_ID
    FROM A
    

    If you have an index on the Start_Time,End_Time fields for B, then this should work quite well.

提交回复
热议问题