title: Slow query when using ORDER BY
body: <p>Here's the query (the largest table has about 40,000 rows)</p>
  
  <pre><code>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
  </code></pre>
  
  <p>If I run this, it executes very quickly (.05 seconds roughly). It returns 13 rows.</p>
  
  <p><strong>When I add an <code>ORDER BY</code> clause at the end of the query (ordering by any column) the query takes about 10 seconds.</strong> </p>
  
  <p>I'm using this database in production now, and everything is working fine. All my other queries are speedy. </p>
  
  <p>Any ideas of what it could be? I ran the query in MySQL's Query Browser, and from the command line. Both places it was dead slow with the <code>ORDER BY</code>. </p>
  
  <p><strong>EDIT:</strong> Tolgahan ALBAYRAK solution works, but can anyone explain why it works? </p>