Reputation: 774
In PHP
I've learned that How I call a function I made affects the performance, Like for example this:
function SUM() {
return 1+2+3+4;
}
If I did something like
print(SUM());
print(SUM());
print(SUM());
It is same as doing print(1+2+3+4)
three times, Which had to calculate the 1+2+3+4
, But If I assigned it to a variable for example;
$value = SUM();
print($value);
print($value);
print($value);
It is as doing print(10)
, But the value
is calculated once and assigned to a variable.
In SQL
I can make aliases
to columns and calculations and can use aliases or not, Like for example:
SELECT (A+B+C+D)
FROM table
WHERE (A+B+C+D) > 0
ORDER BY (A+B+C+D)
Doesn't use aliases and uses (A+B+C+D)
twice, What I wonder is, If this Query is the same as
SELECT (A+B+C+D) AS value
FROM table
WHERE (A+B+C+D) > 0
ORDER BY value
in same aspect of the PHP
calls.
Does the value
calculate the (A+B+C+D)
again? or is it assigned with the alias?
Upvotes: 0
Views: 23
Reputation: 61378
How exactly does the database evaluate SQL is an implementation detail. It's fairly safe to assume that the calculated value is stored somewhere in a temporary variable during execution, but we don't get to see that. And if the ORDER BY expression happens to match one of the calculated fields and the query can be sped up by not evaluating it again, the database might just be smart enough to notice that.
If performance is your concern, try both and compare execution timing. I bet you won't be able to tell the difference.
Upvotes: 3