How can I optimise a query that uses the same calculation for WHERE and ORDER BY?
For example:
SELECT a, b
FROM my_table
WHERE SIN(a) + COS(b) + TAN(5.5) < 10
ORDER BY SIN(a) + COS(b) + TAN(5.5)
5.5 and 10 in this example are user-supplied values that will change between queries. This is a simplification — the actual calculation is relatively complex, so it is not possible to store the result in another column.
Due to an application limitation, I cannot add the calculation to the SELECT statement. How do I prevent MySQL from having to perform the same calculation twice per row?
Finally, should I be using WHERE or HAVING to filter by the result of a calculation?
Many thanks.
I don’t have my development machine in front of me, does this work?
If it does the HAVING statement shouldn’t be recalculating the equation.