It is free to, but I don't get the feeling that the optimizer would be smart
enough (or at least take the time) to deal with something along the lines
of:
SELECT (column1 * 10) + 20, column1 * 10, column1 * 20, power((column1 *
10),2)
FROM ...
and realize that column1 * 10 is a part of every calculation. I was just
suggesting that it might be a bit better performance wise to rewrite the
query as:
SELECT col1Times10 + 20, col1Times10, col1Times10 * 2, power(col1Times10,2)
FROM (SELECT column1 * 10 as col1Times10
FROM
Mathematically they are equivalent, and should be precisely the same as the
type of thing that the optimizer does, but I don't have that kind of faith
:) I should have been a bit more soft in my answer, stating that it might
have to be done this way, or at least it is easier to code. I know that I
once had to do the standard deviation calculation in 6.5 about 20 times in a
query once, and it took three lines of SQL expression to make it work
(dealing with nulls was a real pain.) They added it to 2000, so I won't be
going back there (and I have thankfully forgotten how I did it!)
When I executed the statements above, using the Northwind..orders tables, it
did some substituion:
SELECT (freight * 10) + 20, freight * 10, freight * 20, power((freight *
10),2)
FROM orders
was evaluated to:
Compute Scalar(DEFINE:([Expr1002]=[Orders].[Freight]*10.00+20.00,
[Expr1003]=[Orders].[Freight]*10.00, [Expr1004]=[Orders].[Freight]*20.00,
[Expr1005]=power([Orders].[Freight]*10.00, 2)))
while
SELECT col1Times10 + 20, col1Times10, col1Times10 * 2, power(col1Times10,2)
FROM (SELECT freight * 10 as col1Times10
FROM orders) as orders
was evaluated to:
Compute Scalar(DEFINE:([Expr1002]=[Orders].[Freight]*10.00+20.00,
[Expr1003]=[Orders].[Freight]*10.00,
[Expr1004]=[Orders].[Freight]*10.00*2.00,
[Expr1005]=power([Orders].[Freight]*10.00, 2)))
Which is pretty much the same thing. It was easier to write though, if
col1Times10 took three or four lines, or it was a non-deterministic function
call.
However, to allow this sort of thing:
select column1 * 10 as colTimes10, colTimes10 *2 ...
syntactically would not absolutely require column by column evaluation (not
doubting that is why Access allows it though :). It would require a bit
more intelligent of a compiler. As long as you evaluate this expression
from left to right, there is not a problem doing macro like expansion of
attributes. It would not make it any faster though.
--
----------------------------------------------------------------------------
-----------
Louis Davidson (drsql@hotmail.com)
Compass Technology Management
Pro SQL Server 2000 Database Design
http://www.apress.com/book/bookDisplay.html?bID=266 Note: Please reply to the newsgroups only unless you are
interested in consulting services. All other replies will be ignored :)
[quoted text, click to view] "Joe Celko" <joe.celko@northface.edu> wrote in message
news:%23tUGq3J2DHA.3216@TK2MSFTNGP11.phx.gbl...
> >> If you need to do something like this. It should perform fine,
> probably even better on SQL Server, since the values you will be using
> will already be calculated. If you repeat the calculation three times,
> it will have to be evaluated three times. <<
>
> Louis, you usually know better! The optimizer is free to do the
> calculation once and facotr out common subexpressions, but you cannot
> reference the calculation as if you were still in a procedural language.
> There are products that produce the result sets column by column,
> instead of row by row like SQL Server.
>
> --CELKO--
> ===========================
> Please post DDL, so that people do not have to guess what the keys,
> constraints, Declarative Referential Integrity, datatypes, etc. in your
> schema are.
>
> *** Sent via Developersdex
http://www.developersdex.com ***
> Don't just participate in USENET...get rewarded for it!