2

我有这个 MySQL 问题。它工作正常,但速度极慢。运行最多需要 30 秒。

SELECT t.Id, Subject, k.info
FROM tip t
LEFT JOIN comments k ON t.Id = k.Tipid
WHERE event = 1
AND k.Id
IN (
SELECT Max(Id) FROM comments GROUP BY Tipid
)

如果我只运行子查询 ( SELECT Max(Id) FROM comments GROUP BY Tipid),运行大约需要 0.02 秒。

子查询有问题。你有什么想法?

4

3 回答 3

2

如果您选择 max(id),则不需要 group by 试试这个

    SELECT t.Id, Subject, k.info
    FROM tip t
    LEFT JOIN comments k ON t.Id = k.Tipid
    WHERE event = 1
    AND k.Id in (select Max(Id) from comments)
于 2013-02-13T09:58:57.573 回答
1

试试这个:

SELECT t.Id, t.Subject, c.info
FROM tip t
LEFT JOIN
(
   SELECT Tipid, MAX(Id) AS MaxId
   FROM comments 
   GROUP BY Tipid
) AS c  ON t.Id = c.Tipid
       AND t.Id = c.MaxId
WHERE t.event = 1;
于 2013-02-13T09:52:08.497 回答
0

这有很多原因......我想你试试这个......

SELECT t.Id, Subject, k.info
FROM tip t
INNER JOIN comments k ON t.Id = k.Tipid
WHERE event = 1
  AND k.Id IN ( SELECT Max(Id) FROM comments GROUP BY Tipid )

因为 LEFT JOIN 在 50 条记录后变得太慢。

于 2013-02-13T13:25:30.717 回答