Добрый день, есть запрос выводы около миллиона строк
$sql = "select
i.item_id,
i.article,
i.name,
i.price,
b.brand_name,
i.count,
s.sale_id,
i.storage_id,
sa.uslovie,
i.brand_id,
i.original_article,
i.min_zakaz,
i.items_id
from items as i
join storage as s on s.storage_id = i.storage_id
left join sale as sa on sa.sale_id = s.sale_id
left join category as c on c.category_id = i.category_id
join brand as b on b.brand_id = i.brand_id
where ( DATE_FORMAT(i.up_date,'%Y%m%d%H%i')>= DATE_FORMAT(s.price_update,'%Y%m%d%H%i') and s.price_update IS NOT NULL) and i.count>0 and (i.storage_id IN (1,2,34,4,5,6,7) ) and (i.items_id NOT IN (1,2,3,4,5,6) ) ";
and b.brand_name!='' and i.article!='' and (i.brand_id NOT IN (1,2,3,4,5) ) ";
$sql = $sql." and i.price=(SELECT min(i2.price)
FROM items i2
join storage as s on s.storage_id = i2.storage_id
left join sale as sa on sa.sale_id = s.sale_id
left join category as c on c.category_id = i2.category_id
join brand as b on b.brand_id = i2.brand_id
WHERE i.item_id = i2.item_id and ( DATE_FORMAT(i2.up_date,'%Y%m%d%H%i')>= DATE_FORMAT(s.price_update,'%Y%m%d%H%i') and s.price_update IS NOT NULL) and i2.count>0
and (i2.storage_id IN (1,2,34,4,5,6,7) ) and i.storage_id NOT IN (23,24,25) ) and (i2.items_id NOT IN (1,2,3,4,5,6) ) and (i2.brand_id NOT IN (1,2,3,4,5) )
((i2.category_id NOT IN (8,9,21,22) and i2.brand_id!=9309) and i2.brand_id NOT IN(9308,9303,9305,9310,9306,9500,9589,10493)) ) order by s.priority,b.brand_name,i.article,i.price limit 750000 ";
суть запроса вывод минимальных значений по цене из группированных, можно ли как то оптимизировать этот запрос?