2013-12-13 163 views
0

我使用MySQL和我有一些性能問題絲毫此查詢:優化慢MySQL查詢

SELECT s.ID, headline, uId, teaser, hide_image_archiv, image, image_caption, date_format(cDate, '%d.%m.%Y | %H:%i Uhr') as date, channelid, channel_article.name FROM 
     (SELECT article.ID, uId, headline, teaser, cDate, image, image_caption, channelid, hide_image_archiv 
FROM article 
     inner join article_cCountry on article.ID = ID1 and ID2 = 1 
WHERE sDate < now() 
     and (eDate > now() or eDate = 0) 
     and released = 'TRUE' 
     and (uId in (select ID from user where released = 1) or uId = 0) 
ORDER BY cDate DESC) AS s, channel_article where channelid = channel_article.ID AND channelid = 8 order by cDate desc LIMIT 3 

說明:

enter image description here

它需要大約0.30 - 0.40秒

有什麼方法可以優化它嗎?

回答

1

查詢:;! 「AND渠道ID = 8」

SELECT article.ID, 
      uId, 
      headline, 
      teaser, 
      cDate, 
      image, 
      image_caption, 
      channelid, 
      hide_image_archiv, 
      channel_article.name 
    FROM article 
    INNER JOIN article_cCountry ON article.ID = ID1 
    AND ID2 = 1 
    INNER JOIN channel_article 
    ON channelid = channel_article.ID 
    AND channelid = 8 
    WHERE sDate < now() 
    AND (eDate > now() 
      OR eDate = 0) 
    AND released = 'TRUE' 
    AND (uId IN 
      (SELECT ID 
      FROM USER 
      WHERE released = 1) 
      OR uId = 0) 
    ORDER BY cDate DESC LIMIT 3 
+0

驚人)0.0019秒 – grundig

+0

我試着與其他查詢,而不是「AND的channelID = 8 「(0.3秒而不是0.0019) 爲什麼!=慢於=? – grundig

0

你可以試試這個,

SELECT * 
FROM 
(
SELECT s.ID, headline, uId, teaser, hide_image_archiv, image, image_caption, date_format(cDate, '%d.%m.%Y | %H:%i Uhr') as date, channelid, channel_A.name, 
case when eDate = 0 the 1 when eDate > now() then 1 else 0 end as FILTER_IND1, 
CASE WHEN sDate < now() THEN 1 ELSE 0 END AS FILTER_IND2 
FROM article 
inner join article_cCountry on article.ID = ID1 and ID2 = 1 AND released = 'TRUE' AND CHANNELID = 8 
INNER JOIN 
(
    SELECT 0 FROM DUAL 
    UNION 
    SELECT ID FROM USER WHERE RELEASED = 1 
) AS U 
ON U.ID = UID 
INNER JOIN CHANNEL_ARTICLE AS A 
ON A.ID = CHANNELID 
) A 
WHERE FILTER_IND = 1 
ORDER BY CDATE DESC 
LIMIT 3