Je logue les requêtes lentes de MySQL et j'en ai une qui revient souvent :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
 
SELECT DISTINCT ski.products_id
                             FROM search_keyword sk
                             JOIN search_keyword_index ski
                                ON (ski.search_keyword_id = sk.id_search_keyword AND sk.blacklisted = 0)
                             JOIN products_stock ps
                                ON (ski.products_id=ps.products_id
                                AND ps.products_stock_quantity > 0)
                             WHERE ( (sk.search_keyword LIKE '%alles') OR (sk.search_keyword LIKE 'sand%') );
J'ai fait un explain qui me donne :
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE ps ALL stock_quantity,products_id,products_stock_size_id NULL NULL NULL 26282 Using where; Using temporary
1 SIMPLE ski ref search_keyword_id,products_id products_id 4 spartoo.ps.products_id 17 Using index
1 SIMPLE sk eq_ref PRIMARY,id_search_keyword id_search_keyword 4 spartoo.ski.search_keyword_id 1 Using where; Distinct

Mon problème est donc dans la première ligne.
Il y a des index ou des clefs sur ces champs : stock_quantity,products_id,products_stock_size_id
Cependant, MySQL n'utilise aucune clef pour faire le tri.

J'ai bien un index sur products_id et sur products_stock_quantity. J'ai également essayé d'en créé un sur ces 2 clefs en même temps.
Mais j'ai toujours une recherche de type ALL sur la première ligne.

Pourquoi les index ne sont-ils pas utilisés? Je sais que je peux les forcer, mais j'aimerais déjà savoir pourquoi ça ne fonctionne pas.

Existe-il un moyen de mieux tourner cette requête?

Merci!