I have a query that selects any information from a db. This information I will use for defining the article price. The problem is that the query takes 3 seconds to execute. This time must to be multiply for many article.
How can I optimize this query? In the articoli table there are 16 330 rows.
This is the query
SELECT ts_scon, AI_CODICIVA,
AI_LIS_EURO1, AI_LIS_EURO2,
AI_LIS_EURO3, AI_LIS_EURO4,
AI_LIS_EURO5, cl_tipocl
FROM tabscon, articoli, clienti
WHERE ts_azienda = 'SRL'
AND AI_AZIENDA = 'SRL'
AND AI_CODIREST = $cod_articolo
AND cl_azienda = 'SRL'
AND cl_codice = $cod_cliente
AND ts_codice IN (
SELECT cl_tabsco
FROM clienti
WHERE cl_codice = $cod_cliente
AND CL_AZIENDA = 'SRL')
AND ts_grusco IN (
SELECT ai_grupscon
FROM articoli
WHERE ai_codirest = $cod_articolo
AND AI_AZIENDA = 'SRL')
This is the EXPLAIN result. The results are the same with indices that without indices.
id|select_type|table|type|possible_keys|key|key_len|ref|rows|Extra
1|PRIMARY|clienti|const|PRIMARY|PRIMARY|25|const,const|1
1|PRIMARY|tabscon|ref|PRIMARY,azie_grsco_codice|PRIMARY|15|const|505|Using where
1|PRIMARY|articoli|ALL|PRIMARY,barcodeidx,StatoAidx,Statoidx|NULL|NULL|NULL|16333|Using where; Using join buffer
3|DEPENDENT SUBQUERY|articoli|ref|PRIMARY,barcodeidx,StatoAidx,Statoidx StatoAidx|15|const|7311|Using where
2|DEPENDENT|SUBQUERY|clienti|const|PRIMARY
Sorry for the little order but not yet practical.
Query-wise the best way to do this would be.
That said, I’m not sure exactly what information you’re trying to get here; you may not need to join onto those tables twice, but without understanding the data I can’t be sure.