Frank DENIS random thoughts.

A MySQL UDF for top N sort

Avoid file sorts is really the thing to do with any database, and MySQL isn’t an exception. Although MySQL knows how to use an index when the sort only depends on it, a file sort (or a load-everything, then sort in the app) is still required for other cases. It’s rather inefficient especialy since you often only need the top N rows, not the whole table.

Kazuho Oku wrote a top N sort UDF for MySQL that solves that issue in an efficient (fast!) way.