I have this query:
Explain
select SQL_NO_CACHE p.id as product_id,
p.price_amount,
p.is_male,
p.is_female,
p.is_accessories,
p.is_contact_lens,
c.id as color_id,
b.name as brand,
p.name,
c.color,
c.code,
c.pohoda_sku,
c.on_stock,
c.not_send_to_kiosks,
(p.is_active && !p.is_archive && c.is_active && !c.is_archive) as is_active,
c.is_visible,
c.abbr
from product_colors as c
left join products as p
on p.id = c.product_id
left join brands as b
on p.brand_id = b.id
order by c.on_stock desc
Why the query does not use index in first table? In phpmyadmin it durate 0.0708s, but on the web it is more than 1s. I do not want limit it by where or limit. Can I optimize this query or it is cause with some memory limit in php.ini or elsewhere?
2
Answers
MySQL can only use the index with
ORDER BY c.on_stock DESC
if it was created that way.
try this:
You can also change the * in the select to the fields you are using.
A other case
There are also other possibilities why the optimiter decides not to take an index. This is the case, for example, if the sort_buffer_size is configured too small.
You can query these like this
and then set it temporarily. When enlarging, make sure that there is enough memory available.
If this is successful, you can also make these changes in my.ini as follows.
SQL_NO_CACHE are disables caching, potentially decreasing execution time.
try not using SQL_NO_CACHE.
also try increasing the
memory_limit
in php.ini configuration because the hosting server architecture is different from your local server