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:
- Avoid OFFSET In LIMIT Clause (query line: 31): OFFSET clauses can be very slow when used with high offsets (e.g. with high page numbers when implementing paging). Instead, use the following \u003ca target\u003d"_blank" href\u003d"http://www.eversql.com/faster-pagination-in-mysql-why-order-by-with-limit-and-offset-is-slow/"\u003eseek method\u003c/a\u003e, which provides better and more stable response rates.
- 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. 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: 26): Referencing a numeric value (e.g. 1) 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: 27): 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.
Optimal indexes for this query:
ALTER TABLE `cs_annunci` ADD INDEX `cs_annunci_idx_attiv_idute_idreg_posit_data_ora` (`Attiva`,`IDUtente`,`IDRegione`,`Position`,`Data`,`Ora`);
ALTER TABLE `cs_annunci` ADD INDEX `cs_annunci_idx_position_data_ora` (`Position`,`Data`,`Ora`);
ALTER TABLE `cs_regioni` ADD INDEX `cs_regioni_idx_id` (`ID`);
ALTER TABLE `cs_utenti` ADD INDEX `cs_utenti_idx_sospeso_cancellato_id` (`Sospeso`,`Cancellato`,`ID`);
The optimized query:
SELECT
cs_annunci.ID,
cs_utenti.ID,
cs_annunci.Data,
cs_annunci.Tipologia,
cs_annunci.Foto1,
cs_annunci.Titolo,
cs_annunci.IDRazza,
cs_annunci.Cucciolo,
cs_utenti.Attivita,
cs_annunci.IDRubrica,
cs_annunci.IDSottorubrica,
cs_annunci.Position,
cs_annunci.Ora,
cs_annunci.Prezzo,
cs_annunci.Pedigree,
cs_annunci.IDProvincia
FROM
cs_annunci,
cs_utenti,
cs_regioni
WHERE
cs_annunci.IDUtente = cs_utenti.ID
AND cs_annunci.IDRegione = cs_regioni.ID
AND cs_utenti.Sospeso = '0'
AND cs_annunci.Attiva = '1'
AND cs_utenti.Cancellato = '0'
ORDER BY
cs_annunci.Position DESC,
cs_annunci.Data DESC,
cs_annunci.Ora DESC LIMIT 0,
20