I am writing a SQL with Oracle Client 12 driver. I have two tables simplified as appended, and I want to get a table with following logic. The "B.TIME_B <= A0.TIME_A" seems created massive joining and made the query very slow. Please help to find best solution.
WITH A0 AS (
SELECT *
FROM A
WHERE A.EVENT = 'a0'
)
SELECT * FROM (
SELECT
ROW_NUMBER() OVER (PARTITION BY A0.TIME_A0 ORDER BY B.TIME_B DESC) RN,
A0.*,
B.*
FROM
A0,B
WHERE
B.TIME_B <= A0.TIME_A) B0
WHERE B0.RN <= 3
Find TIME_A, where EVENT_A = 'a0', as TIME_A0,
Find TIME_B = TIME_A0, as EVENT_B0,
- And then get the row and previous 2 rows of table B, where EVENT_B0 found. N in this example is 3, and M is 2, but in real case both number are over 3000, so efficiency will be appreciated.
TableA
TIME_A EVENT_A
1 a1
2 a1
3 a1
4 a0
5 a2
6 a2
7 a3
8 a0
Table B
TIME_B EVENT_B
1 b1
2 b2
3 b3
4 b4
5 b5
6 b5
7 b6
8 b7
JOIN A_B
TIME_A EVENT_A TIME_B EVENT_B
4 a0 2 b2
4 a0 3 b3
4 a0 4 b4
8 a0 6 b5
8 a0 7 b6
8 a0 8 b7