php - 优化缓慢的 MySQL 查询

标签 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

解释:

enter image description here

大约需要 0.30 - 0.40 秒

有什么办法可以优化吗?

最佳答案

查询:

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

关于php - 优化缓慢的 MySQL 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20561311/

相关文章:

php - 如何在 PHP 中转换 MySQL INNER JOIN 结果?

mysql - Rails:通过迁移添加唯一索引会抛出 Mysql2::Error: BLOB/TEXT

mysql - 完全唯一行的表的主键?

database - AWS RDS - 降级数据库实例类会导致现有数据丢失吗?

php - Google Maps API 3 - 地理编码无法正常工作

php - 所有玩家标签的数据库超链接?

php - 使用 PHP 获取 IP 地址所在的国家/地区

php - Laravel 构造函数重定向不起作用

php - 如何从 Javascript 执行外部 php?

php - 从 <option> 中选择多个字段并将它们添加到数据库