I created a query, that collects data from two tables, summs them up and shows the count of the cases and the total summ:
SELECT
count(ut.id) AS total
, ( SUM(internal_account) - SUM(( SELECT
SUM( ub.bill_summs )
FROM
u_billing ub
WHERE
ub.bill_types = 'correction'
AND ub.contract_id = ut.contract_id
)) ) AS summ
FROM
u_transactions ut
WHERE
ut.nulled = 0
AND ut.type = 'comission'
AND ut._status = 'not_paid'
AND DATE( ut.add_timestamp ) = DATE( '2012-05-11' );
but it is really slow. On test cases it gave this result:
+-------+-------+
| total | summ |
+-------+-------+
| 182 | 15105 |
+-------+-------+
1 row in set (4.13 sec)
It is 4.13 seconds on 182 cases and for only 1 day, but my live server has over 600k cases, so this will be extremely slow.
Any ideas, how I can rewrite the query for better performance?
Solution with remade query(ies):
DELETE FROM tmpContractSums;
INSERT INTO tmpContractSums
SELECT
ub.contract_id
, SUM( ub.bill_summs ) AS bill_summs
FROM
u_billing ub
WHERE
ub.bill_types = 'correction'
GROUP BY ub.contract_id;
SELECT
count(ut.id) AS total
, ( SUM(internal_account) - SUM(bill_summs) )
FROM
u_transactions ut
LEFT JOIN tmpContractSums t ON ut.contract_id = t.contract_id
WHERE
ut.nulled = 0
AND ut.type = 'comission'
AND ut._status = 'not_paid'
AND ut.add_timestamp BETWEEN '2012-05-11 00:00:00' AND '2012-05-11 23:59:59';
Execution time: 500ms
PS: Since I can’t drop tables with webuser I just created the table:
CREATE TABLE tmpContractSums AS SELECT contract_id, bill_summs FROM u_billing WHERE 1 = 0;
and the I’m deleting the records. Not as fast as drop, but still way faster then original.
How about simply using a “temporary” table?
This should be faster and if you want you can add indexes to the “temporary table” or make it a table with engine=memory if you have enough space.
Or: