优化慢速MySQL查询

时间:2013-12-13 07:32:41

标签: php mysql database query-optimization

我正在使用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

EXPLAIN:

enter image description here

需要大约0.30 - 0.40秒

有没有办法优化它?

2 个答案:

答案 0 :(得分:1)

查询:

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

答案 1 :(得分: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