I have a couple of tables in a DB2 database. Table_1 looks like this (the actual table is 9.2 million rows):
| Customer_ID | Offer | Item_list |
|---|---|---|
| A | X | 1 |
| A | Y | 2 |
| B | Y | 2 |
Table_2 looks like this (the actual table is 83k rows):
| Item_list | Item_ID |
|---|---|
| 1 | 111 |
| 1 | 222 |
| 1 | 333 |
| 2 | 111 |
| 2 | 444 |
I want to join the tables to return a list of items for each customer. So for the example above, the result should look like this:
| Customer_ID | Item_ID |
|---|---|
| A | 111 |
| A | 222 |
| A | 333 |
| A | 444 |
| B | 111 |
| B | 444 |
I have written the code below to do this. However, it seems to take forever to run (I finally killed it after 25 minutes with no results). Is there a more efficient way of getting the results I'm looking for please?
SELECT A.CUSTOMER_ID, B.ITEM_ID FROM TABLE_1 A LEFT JOIN TABLE_2 B ON A.ITEM_LIST = B.ITEM_LIST GROUP BY A.CUSTOMER_ID, B.ITEM_ID ;
group by?WHEREclause. @SJRCoding may want to separate query time and fetch time in the analysis. With that much data, I'd bet the fetch time is the problem.