我有这个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秒。
子查询有问题。你有什么想法吗?
答案 0 :(得分:2)
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)
答案 1 :(得分: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;
答案 2 :(得分: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条记录后变得太慢了。