@snapey thanks a lot for those video links. Really interesting.
One thing that he did point out - when you achieve a ref type of const within the explain query, that is as good as you are going to get! In my case, I can still seem to make considerable improvements past this by using the compound index.
Something that I have realise - my slow queries are actually sums (with where's) and I now realise that indexes do not necessarily help out these kinds of queries.
I have been trying out different indexes across the whole operation and found some interesting things:
My original slow query (with indexes) ran at about 500ms, which compounded across the whole operation to give the 12 seconds run time:
select -SUM(picked) as picked, `product_id` from `stock_movements` where `stock_movements`.`location_id` = '700020245' and `stock_movements`.`location_id` is not null and `stock_movements`.`company_id` = '8b11050c-612c-4922-8b34-d04d579e02a9' group by `product_id`
If I remove all table indexes (other than PK), this runs considerably faster at about 33ms and explain shows there are no possible keys. However the whole operation across all the other queries is slow at 10 seconds.
Adding on just a single index to my company_id speeds up the whole operation to about 6 seconds, but the sum query above is now 200ms. This sum query runs 15 times in the whole operation, so accounts for half of the run total run time. In this case it seems any form of single column index kills the speed on the sum query.
I can add a combination of compound primary keys and get this back down to 2 seconds again, but this also has detrimental effects on other queries.
My previous understanding was that indexes do not help sums, however I can see from the above that they CAN help sums, but it seems very hard/impossible to make this work alongside all the other requirements.
I guess the easiest fix is to store the sum result direct into the DB so the calculations are not being run every time (even though the values do change quite often).