views:

89

answers:

4

I have a query as below:

SELECT * FROM Members (NOLOCK) 
 WHERE Phone= dbo.FormatPhone(@Phone)

Now here I understand that formatting has to be applied on the variable on column. But should I apply it on variable to assign to some other local variable then use it (as below).

Set @SomeVar = dbo.FormatPhone(@Phone) 

SELECT * 
  FROM Members (NOLOCK) WHERE Phone= @SomeVar

Which way is better or both are good?

EDIT: And how is first query different from

SELECT * FROM Members (NOLOCK) 
 WHERE dbo.FormatPhone(Phone) = @Phone
+4  A: 

The second is definitely preferred. The first one will evaluate the function for each row in the table, whilst the other one will do the calculation only once.

Dan Sydner
In addition the first option most likely won't be able to use an index.
StarShip3000
The function evaluation for every row will be there if we apply function on table column or variable? Or both ways are same?
noob.spt
No, if you perform the calculation up front, you are essentially handing the query a constant.
Aaron Bertrand
You mean these two queries are same?SELECT * FROM Members (NOLOCK) WHERE Phone= dbo.FormatPhone(@Phone)SELECT * FROM Members (NOLOCK) WHERE dbo.FormatPhone(Phone) = @Phone
noob.spt
No, those two queries are not the same. Doing "WHERE Phone = dbo.FormatPhone(@Phone)" and "WHERE Phone = @FormattedPhone" should be the same though.
Tom H.
Dan - the optimizer should not evaluate the function for each row in the first query - "WHERE Phone = dbo.FormatPhone(@Phone)"
Tom H.
Thanks Tom. I agree with you, but not 100% sure.
noob.spt
Tom could you post your comment as a solution. Thanks.
noob.spt
+5  A: 

You'll almost certainly get better predictability if you assign to a variable first, lots of dependency in the optimizer around determinism vs. non-determinism.

chadhoc
+2  A: 

As usual with SQL, the query is largely irelevant without knowing the actual schema is used against.

Do you have an index on Members.Phone? If no, then it makes no difference how you write the query, they all gonna scan the whole table and performe the same (ie. perform badly). If you do have an index then the way you write the query makes all the difference:

SELECT * FROM Members WHERE Phone= @Phone;
SELECT * FROM Members WHERE Phone= dbo.FormatPhone(@Phone);
SELECT * FROM Members WHERE  dbo.FormatPhone(Phone)=@Phone;

First query is guaranteed optimal, will seek the phone on the index.
Second query depends on the characteristics of the dbo.FormatPhone. It may or may not use an optimal seek.
Last query is guaranteed to be bad. Will scan the table.

Also, I removed the NOLOCK hint, it seem the theme of the day... See http://stackoverflow.com/questions/1723910/syntax-for-nolock-in-sql/1724198#1724198. NOLOCK is always the wrong answer. Use snapshot isolation.

Remus Rusanu
I went through the post and it talks about not using NOLOCK with Update/Insert statements. I don't see why its a problem here?
noob.spt
Do you mean use with(nolock) instead of NOLOCK?
noob.spt
Do you *intent* to read uncommited data? Or did you add NOLOCK as a workaround for concurency issues?
Remus Rusanu
Concurrency is not an issue in this case, as long as I can finish the job in a snap.
noob.spt
A: 

SELECT * FROM Members (NOLOCK) WHERE Phone= dbo.FormatPhone(@Phone)

in the query above, function dbo.FormatPhone will be executed for every row in "Members" table.

when second query Set @SomeVar = dbo.FormatPhone(@Phone)

SELECT * FROM Members (NOLOCK) WHERE Phone= @SomeVar

it'll execute the function only once. so i think second option will be faster in case you haev large data in member table.

Radhi
Thanks vrunda. I am getting different responses and opinion here, though I don't believe that function will execute for every row in first query. DO you have any references which prove it?
noob.spt