For the query above, the following recommendations will be helpful as part of the SQL tuning process.
You'll find 3 sections below:
The optimization process and recommendations:
- Create Optimal Indexes (modified query below): The recommended indexes are an integral part of this optimization effort and should be created before testing the execution duration of the optimized query.
- Use Numeric Column Types For Numeric Values (query line: 25): Referencing a numeric value (e.g. 1234) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 26): Referencing a numeric value (e.g. 5678) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 15): Referencing a numeric value (e.g. 01) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
- Use Numeric Column Types For Numeric Values (query line: 19): Referencing a numeric value (e.g. 01) as a string in a WHERE clause might result in poor performance. Possible impacts of storing numbers as varchars: more space will be used, you won't be able to perform arithmetic operations, the data won't be self-validated, aggregation functions like SUM won't work, the output may sort incorrectly and more. If the column is numeric, remove the quotes from the constant value, to make sure a numeric comparison is done.
Optimal indexes for this query:
ALTER TABLE `customer` ADD INDEX `customer_idx_id1_id2` (`id1`,`id2`);
ALTER TABLE `order_entry` ADD INDEX `order_entry_idx_order_id1_id2_order` (`order_status`,`id1`,`id2`,`order_type`);
ALTER TABLE `order_history` ADD INDEX `order_history_idx_order_id1_id2_order` (`order_status`,`id1`,`id2`,`order_type`);
The optimized query:
SELECT
c.id1,
c.id2,
(SELECT
(sum(oe.revenue1) + sum(oe.revenue2) + sum(h.revenue1) + sum(h.revenue2)) * .01
FROM
order_entry oe,
order_history h
WHERE
c.id1 = oe.id1
AND c.id2 = oe.id2
AND c.id1 = h.id1
AND c.id2 = h.id2
AND oe.order_type IN (
'01', '02', '03', '04'
)
AND oe.order_status = 'CLOSED'
AND h.order_type IN (
'01', '02', '03', '04'
)
AND h.order_status = 'CLOSED') AS total_revenue
FROM
customer c
WHERE
c.id1 = '1234'
AND c.id2 = '5678'