-1

Here is the simplified version of my query:

SELECT myfunc(u.id) score FROM users u
ORDER BY score
LIMIT 20

As you see, I've used score (which is the result of myfunc() function) in the ORDER BY clause. Now I want to know, will that function be recalled when I use it in ORDER BY clause? If yes, then how can I avoid that? Because that function needs lots of processing and recalling it twice would be like a nightmare.

Martin AJ
  • 6,261
  • 8
  • 53
  • 111

1 Answers1

0

Probably not, because the data has already been prepared by the time the ordering is done. If you still have a doubt, test it by making your function do something visible between runs (increment a value in a table)

What you may find though, is that it runs a million times, once for each of a million rows, before limit 20 can be applied..

If your function is really slow, and its sensible to store the result upon insert rather than calculate it every time the query is run, do that

Caius Jard
  • 72,509
  • 5
  • 49
  • 80