Skip to content
Advertisement

left join taking too long

I have an issue with query performance and cant find out how to fix it.

Both tables have 20k rows

table 1. prod_id, month, year, volume
1.       1        5      2020   4
2.       2        5      2020   5
...
    
table 2. prod_id, wh, share, month, year
1.       1        1   10%    5      2020
2.       1        2   10%    5      2020
3.       1        3   40%    5      2020
4.       1        4   40%    5      2020
...

Prod_id like indexes but not primary key because table 1 has duplicates with different month, year etc I need to get table like this (table1 * wh = 80k rows):

table 3. prod_id, wh, month, year, new_volume
1.       1        1   5      2020  volume*share
2.       1        2   5      2020
3.       1        3   5      2020
4.       1        4   5      2020
5.       2        1   5      2020
6.       2        2   5      2020
...

I’m using LEFT JOIN to do this but takes too long 15+ mins, any ideas how to improve this query?

SELECT table1.prod_id, table2.wh, table1.month, table1.year, volume*share AS new_volume
FROM table1
LEFT JOIN table2 ON (table1.prod_id = table2.prod_id) AND (table1.month = table2.month) AND (table1.year = table2.year)

Advertisement

Answer

For this query:

SELECT table1.prod_id, table2.wh, table1.month, table1.year, volume*share AS new_volume
FROM table1 LEFT JOIN
     table2
     ON (table1.prod_id = table2.prod_id) AND
        (table1.month = table2.month) AND
        (table1.year = table2.year)

You want an index on table2:

create index idx_table2_prod_month_year on table2(prod_id, month, year)

You can also include wh2 and any other columns in the select that come from table2.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement