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: 16): Referencing a numeric value (e.g. 1435755600) 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: 17): Referencing a numeric value (e.g. 1433163600) 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 `table_name` ADD INDEX `table_name_idx_model_country_e_tual` (`model`,`country`,`E_TUAL`);
The optimized query:
SELECT
table_name.E_TUAL,
table_name.ECHS,
table_name.DEVID,
table_name.MODEL,
table_name.COUNTRY,
table_name.REGION,
table_name.COUNTRY_CODE,
table_name.NETWORK,
table_name.SOURCE
FROM
table_name
WHERE
table_name.model = 'fox | s453'
AND table_name.country = 'india'
AND table_name.E_TUAL <= '1435755600'
AND table_name.E_TUAL >= '1433163600'