使用ORDER BY时查询速度慢

这是查询(最大的表约有40,000行)

SELECT

Course.CourseID,

Course.Description,

UserCourse.UserID,

UserCourse.TimeAllowed,

UserCourse.CreatedOn,

UserCourse.PassedOn,

UserCourse.IssuedOn,

C.LessonCnt

FROM

UserCourse

INNER JOIN

Course

USING(CourseID)

INNER JOIN

(

SELECT CourseID, COUNT(*) AS LessonCnt FROM CourseSection GROUP BY CourseID

) C

USING(CourseID)

WHERE

UserCourse.UserID = 8810

如果运行此命令,它将很快执行(大约.05秒)。它返回13行。

我现在正在生产中使用此数据库,并且一切正常。我所有其他查询都很快。

有什么想法吗?我在MySQL的查询浏览器中并从命令行运行了查询。两个地方都死了ORDER BY

Tolgahan ALBAYRAK解决方案有效,但是谁能解释为什么它有效?

回答:

也许这会有所帮助:

SELECT * FROM (    

SELECT

Course.CourseID,

Course.Description,

UserCourse.UserID,

UserCourse.TimeAllowed,

UserCourse.CreatedOn,

UserCourse.PassedOn,

UserCourse.IssuedOn,

C.LessonCnt

FROM

UserCourse

INNER JOIN

Course

USING(CourseID)

INNER JOIN

(

SELECT CourseID, COUNT(*) AS LessonCnt FROM CourseSection GROUP BY CourseID

) C

USING(CourseID)

WHERE

UserCourse.UserID = 8810

) ORDER BY CourseID

以上是 使用ORDER BY时查询速度慢 的全部内容, 来源链接: utcz.com/qa/428188.html

回到顶部