I'm trying to get a report down from the 20+ minute run time and I've tracked the bottleneck to this monster of a mysql query that uses 5 joins and 3 subqueries to return all the results in one data set. My big bottleneck so far seems to be: join the stock table on the payments table is the stock.id matches a payment.stock_id and the payment.type is this type and the payment.status is this status. I'm slowly breaking this apart into various queries and letting Java decide what records to keep and throw away but I'm wondering if there is an efficient way in mysql to get a couple of resultsets, perhaps as tables stored in memory and then query against those? Some of my result sets will be pretty big, a few hundred thousand entries in the worst cases.
Thanks in advance
You have some choices of create materialized view so that you can run this on a time interval, but you'll have basically a delay effect on the validity of your data. But sure is faster.
Second is index the joining columns.
Sub select are usually more expensive than a JOIN, so try to merge your subselect into a join instead.
There are a lot of SQL optimization book out there that is worth reading that deals with these questions. But without knowing your DB or schema or indexes and how the joins and subselects are. Its almost like catching a ball in the dark.