Optimize SQL that uses between clause

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

    I wouldn't normally recommend a query like this, but...

    Since you've specified that table A only has about 980 rows and that each row maps to exactly one row in table B, then you could do the following and it will most likely be a lot faster than a cartesian join:

    SELECT A.id AS id_from_a,
        (
            SELECT B.id
            FROM B
            WHERE A.event_time BETWEEN B.start_time AND B.end_time
            LIMIT 0, 1
        ) AS id_from_b
    FROM A
    

提交回复
热议问题