We have a table containing 100 million records where we need to find all dates between date ranges present in each row of the table.
Tabl name: item_details
----------------------------------------
Item_no | item_type | active_from | active_to |
----------------------------------------
10001|AAC|2023-01-01|2099-12-31|
--------------------------------
10002|BOC|2024-06-03|2025-10-01
--------------------------------
10003|COD|2025-01-01|2025-01-01
---------------------------------
Output
Item_no|item_type|active_dates
----------------------------------------
10001|AAC|2023-01-01|
10001|AAC|2023-01-02|
10001|AAC|2023-01-03|
:
:
10001|AAC|2099-12-30|
10001|AAC|2099-12-31|
10002|BOC|2024-06-03|
10002|BOC|2024-06-04|
:
:
10002|BOC|2025-09-30|
10002|BOC|2025-10-01|
10003|COD|2025-01-01|
--------------------------------
I tried cross join and connect by level, but it is taking huge time to print result as date is almost 75 years +..so looping through each record, it gets hung.
tried connect by level by passing date from each row, but query got hang without any results...
We have a table containing 100 million records where we need to find all dates between date ranges present in each row of the table.
Tabl name: item_details
----------------------------------------
Item_no | item_type | active_from | active_to |
----------------------------------------
10001|AAC|2023-01-01|2099-12-31|
--------------------------------
10002|BOC|2024-06-03|2025-10-01
--------------------------------
10003|COD|2025-01-01|2025-01-01
---------------------------------
Output
Item_no|item_type|active_dates
----------------------------------------
10001|AAC|2023-01-01|
10001|AAC|2023-01-02|
10001|AAC|2023-01-03|
:
:
10001|AAC|2099-12-30|
10001|AAC|2099-12-31|
10002|BOC|2024-06-03|
10002|BOC|2024-06-04|
:
:
10002|BOC|2025-09-30|
10002|BOC|2025-10-01|
10003|COD|2025-01-01|
--------------------------------
I tried cross join and connect by level, but it is taking huge time to print result as date is almost 75 years +..so looping through each record, it gets hung.
tried connect by level by passing date from each row, but query got hang without any results...
Share Improve this question edited Jan 18 at 0:26 Narasimhan M asked Jan 17 at 17:50 Narasimhan MNarasimhan M 193 bronze badges 4 |1 Answer
Reset to default 0I'd tend to materialize the connect by level into a table
CREATE TABLE dates (t DATE) PCTFREE 0;
INSERT INTO dates (t)
SELECT DATE '2022-12-31' + level FROM dual CONNECT BY LEVEL < 365.25*77;
EXEC dbms_stats.gather_table_stats(null, 'dates');
and then join this table to your 100 million row table
SELECT i.item_no, i.item_type, d.t as active_dates
FROM item_details i
JOIN dates d ON d.t BETWEEN i.active_from AND i.active_to;
2099-12-31
then that average may be underestimating. At any rate, this is very solvable with a calendar table if you are prepared for the sizable result set. – JNevill Commented Jan 17 at 18:21