Is MySQL LIMIT applied before or after ORDER BY?

这一生的挚爱 提交于 2019-12-02 20:10:06

Yes, it's after the ORDER BY. For your query, you'd get the record with the highest publishedOn, since you're ordering DESC, making the largest value first in the result set, of which you pick out the first one.

The limit is always applied at the end of result gathering, therefore after order by.

Given all your clauses, the order of processing will be

  • FROM
  • WHERE
  • SELECT
  • ORDER BY
  • LIMIT

So you will get the closest record <= publishedOn matching all the conditions in the WHERE clause.

Just wanted to point out the in case of MySQL ordering is applied before limiting the results. But this is not true for other DB.

For example Oracle first limits results and applies ordering on said results. It makes sense when you think about it from a performance point of view. In MySQL you are actually ordering the entire DB(> 1000 records) to get 2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!