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: 14): Referencing a numeric value (e.g. 00) 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. 0) 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: 24): Referencing a numeric value (e.g. 1100) 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:
CREATE INDEX clwh_data_idx_tran_date ON CLWH_COMMON_DATA (TRAN_DATE);
CREATE INDEX clwh_data_idx_netsw_acqeref ON CLWH_COMMON_DATA (NETSW_ACQEREF);
The optimized query:
SELECT
"c"."NETSW_ACQEREF" AS "BANK",
count("c"."NETSW_ACQEREF") AS "QTY",
sum("c"."TRAN_AMNT") / 100 AS "AMOUNT",
count(DISTINCT "c"."TERM_ID") AS "terminals"
FROM
"CSCLWH"."CLWH_COMMON_DATA" "c"
WHERE
(
"c"."TRAN_DATE" BETWEEN 20201101 AND 20201111
)
AND (
"c"."TRAN_TYPE" IN (
'00', '01', '10', '12', '19', '20', '26', '29', '50', '51', '52'
)
)
AND (
"c"."RESP_CODE" IN (
'0', '00', '000', '400'
)
)
AND (
"c"."MTI" IN (
'1100', '1200', '1240', '1400', '1420'
)
)
GROUP BY
"c"."NETSW_ACQEREF"
ORDER BY
"BANK"