I am trying to combine two relatively simple tables together, but my query is experiencing severe freezes. I'm not sure why, but I think this may have something to do with the between function. My first table looks something like this (with lots of other columns, but this will be the only column I pull out):
RowNumber 1 2 3 4 5 6 7 8
My second table "groups" my rows into "blocks" and has the following layout:
BlockID RowNumberStart RowNumberStop 1 1 3 2 4 7 3 8 8
The desired result I want is to associate a RowNumber with a BlockID, as shown below, with the same number of rows with the first table. Thus, the result will look like this:
RowNumber BlockID 1 1 2 1 3 1 4 2 5 2 6 2 7 2 8 3
To get this, I used the following query, writing the results to the temp table:
select A.RowNumber, B.BlockID into TEMP_TABLE from TABLE_1 A left join TABLE_2 B on A.RowNumber between B.RowNumberStart and B.RowNumberStop
TABLE_1 and TABLE_2 are actually very large tables. Table 1 is about 122M rows, and TABLE_2 is about 65M rows. In TABLE_1, RowNumber is defined as "bigint", and in TABLE_2 the values โโof BlockID, RowNumberStart and RowNumberStop are defined as "int". Not sure if this matters, but just need to include this information.
The request was hung for eight hours. Similar queries for this type and amount of data are not getting anywhere. Therefore, I am wondering if this could be the "between" expression that hangs this request.
Would definitely welcome any suggestions on how to make this more efficient.