Markus
Новичок
Оптимизация запроса, одним или нескольким, как лучше?
Есть такой sql запрос http://www.pastebin.ru/2553 но при большом колличестве записей он стал работать очень медленно, можно ли как то оптимизировать.
Вот сам запрос
Есть такой sql запрос http://www.pastebin.ru/2553 но при большом колличестве записей он стал работать очень медленно, можно ли как то оптимизировать.
Вот сам запрос
PHP:
SELECT p.*,
COUNT(k.ProjectKeywordID) AS Count,
COUNT(s.ProjectStatPosition) AS YandexStat,
COUNT(s2.ProjectStatPosition) AS RamblerStat,
COUNT(s3.ProjectStatPosition) AS YandexStat2,
COUNT(s4.ProjectStatPosition) AS RamblerStat2,
if(COUNT(s.ProjectStatPosition) = 0,3,
if(COUNT(k.ProjectKeywordID) > COUNT(s.ProjectStatPosition),2,
if(COUNT(k.ProjectKeywordID) = COUNT(s.ProjectStatPosition),1,0))) AS Stat,
if(COUNT(s.ProjectStatPosition) > COUNT(s3.ProjectStatPosition),1,
if(COUNT(s.ProjectStatPosition) < COUNT(s3.ProjectStatPosition),2,
if(COUNT(s.ProjectStatPosition) = COUNT(s3.ProjectStatPosition),3,0))) AS NumYandex,
if(COUNT(s2.ProjectStatPosition) > COUNT(s4.ProjectStatPosition),1,
if(COUNT(s2.ProjectStatPosition) < COUNT(s4.ProjectStatPosition),2,
if(COUNT(s2.ProjectStatPosition) = COUNT(s4.ProjectStatPosition),3,0))) AS NumRambler
FROM ea_project AS p
LEFT JOIN ea_project_keyword as k ON p.ProjectID = k.ProjectID
LEFT JOIN ea_project_stat AS s ON k.ProjectKeywordID = s.ProjectKeywordID
AND s.ProjectStatDate BETWEEN '1146168000'
AND '1146254399'
AND s.ProjectDomen = 'yandex'
AND s.ProjectStatPosition <= 10
LEFT JOIN ea_project_stat AS s2 ON k.ProjectKeywordID = s2.ProjectKeywordID AND s2.ProjectStatDate BETWEEN '1146168000' AND '1146254399'
AND s2.ProjectDomen = 'rambler'
AND s2.ProjectStatPosition <= 10
LEFT JOIN ea_project_stat AS s3 ON k.ProjectKeywordID = s3.ProjectKeywordID AND s3.ProjectStatDate BETWEEN '1146081600' AND '1146167999'
AND s3.ProjectDomen = 'yandex'
AND s3.ProjectStatPosition <= 10
LEFT JOIN ea_project_stat AS s4 ON k.ProjectKeywordID = s4.ProjectKeywordID AND s4.ProjectStatDate BETWEEN '1146081600' AND '1146167999'
AND s4.ProjectDomen = 'rambler'
AND s4.ProjectStatPosition <= 10
WHERE p.ProjectActive = 'y'
GROUP BY k.ProjectID
ORDER BY p.ProjectName