views:

152

answers:

6

Hi

I was playing around with SQL Queries and this one came to my mind - select x,y from table T.

Here x and y can be arithmetic expressions like 2.0/5.0 OR 1+2, the result has the number of rows of table T, with the first column having value 2.0/5.0 i.e. 0.444444 and the second column having value 3.

I am curious to know as to how this query is executed. I have learned that in the SQL query engine there is a mapping from SQL representation of a query to an internal representation (like relational algebra) which is then optimized in order to yield an optimal query execution plan.

Please correct my understanding if its wrong - For the query which I have stated, there would be an internal mapping from my query Q to an internal representation which is equivalent to that of select * from table T. Then for all the rows returned, n number of columns are projected out where n is the number of arithmetic expressions stated in the Q. And all rows for the n columns would have their respective evaluations of the arithmetic expressions.

cheers

A: 

It most likely depends on the database.

If the expressions you use for x and y were constant then a decent database ought to recognize that and perform the calculation only once and just putting the value in the result set.

If the expressions included some other function then it would have to execute the expression each time in case the function had side effects.

In general you are correct, calculate the virtual table described in the from clause, then I believe the filters from the where clause are applied followed by the select clause.

Tom Hubbard
+1  A: 

No, the expressions are only evaluated once (at least in MS SQL Server).

You can easily see this if you do:

select rand() from tableT

All rows will have the same random number.

Guffa
+1  A: 

it will scan because there is no where clause. here is sql server's set showplan_all on

select 2.0/5.0,1+2 from YourTable                                                            
  |--Compute Scalar(DEFINE:([Expr1003]=(0.400000), [Expr1004]=(3)))                           
       |--Index Scan(OBJECT:([wppusdev].[dbo].[AP_Voucher].[YourTable_Index]))
KM
A: 

From a MS SQL point of view...

It might not evaluate the same as SELECT * FROM Table because the engine should realize that it doesn't actually need any of the columns from the table, so it will most likely select the thinnest index on the table and scan that.

The actual equations should only be evaluated once and from then on used as a constant.

Tom H.
A: 

PostgreSQL will evaluate the expression first, then most likely perform a sequential scan on the table. Also, as mentioned elsewhere, many SQL flavors have an EXPLAIN keyword, which will show you how, given your current implementation, a particular query will be evaluated.

rpkelly
Postgresql allows functions to be marked as "stable" or "volatile": stable functions may be moved to an "initplan" to be evaluated just once ("select * from t where length('xyzzy') = 4" produces a different plan to "select * from t where random() > 0.5"; and different plans for "length('xyzzy') = 4" and "length('xyzzy') = 5")
araqnid
A: 

Whether the database evaluates the function once for each row is an implementation decision. In Oracle, you can select dbms_random.random() from a table and get different numbers back for each row. This is helpful if you want to return the rows in random order, for example.

Plasmer