I have 3 tables with a total of about 200k rows.
My tables:
warehouse_stock
id  |   barcode      |  quantity  | warehouse_id
-------------------------------------------------
1   | 2211345661     | 21         | 1
2   | 2211345661     | 2          | 2
3   | 3452543234     | 1          | 2
4   | 3452543235     | 1          | 1
5   | 3452543236     | 1          | 1
6   | 3452543242     | 1          | 1
7   | 3452543245     | 1          | 1
8   | 3452543245     | 1          | 3
9   | 3452543245     | 1          | 1
product_variants
id    | ean(barcode)     | product_id
-------------------------------------
1     | 3452543245       | 14
2     | 3452543234       | 15
3     | 3452543245       | 16
4     | 3452543245       | 17
5     | 2211345661       | 18
6     | 2211345661       | 19
products
id     |   name     |   color  | manufacturer_id
------------------------------------------------
14     | Sample 1   | Red      | 1
15     | Sample 2   | Black    | 1
16     | Sample 3   | White    | 1
17     | Sample 4   | White    | 1
18     | Sample 5   | Orange   | 2
19     | Sample 6   | Pink     | 2
I just want to display SUM(quantity), product.name, barcode, manufacturer_id and warehouse_id. There is no relationship except barcode numbers.
Here is my code:
SELECT 
    SUM(ws.quantity) AS total_q,
    p.name,
    ws.barcode,
    p.manufacturer_id,
    ws.warehouse_id
FROM warehouse_stock AS ws
    INNER JOIN product_variants AS pv ON pv.ean = ws.barcode
    INNER JOIN products AS p ON p.id = pv.product_id
WHERE ws.warehouse_id = 1 AND p.manufacturer_id = 1
GROUP BY ws.barcode
It works, but mostly because that query goes off. Then we have to restart it.
How can change this to use sub-queries?
PS: There are no indexes on sql. And no permission for index. So that, I want your suggests. Subquery might be better then JOIN for such a query.
 
    