Thor
Thor

Reputation: 10068

Does calling a user defined function twice in select query lead to inefficiency?

I have a simple table where people's birthday are been stored. I also have a simple scalar user defined function where the function takes a "Date" object (birthday) and returns an int (Age). I wrote a query which tries to select every entry that has an age of 17.

In the "select" query, "dbo.calculateAge(Birthday)" were written twice, so I was just wondering, does this mean that "calculateAge" function will be called twice? Does this leads to inefficiency? If yes, is there a better way to write this query?

My Query:

enter image description here

My Table:

enter image description here

I choose not to include implementation of the "calculateAge" function here because I don't think it is useful to the question I'm asking.

Upvotes: 1

Views: 190

Answers (1)

O. Jones
O. Jones

Reputation: 108806

does this mean that "calculateAge" function will be called twice?

No, just once. But to be sure, make sure your function is declared as deterministic.

Does this leads to inefficiency?

No.

If yes, is there a better way to write this query?

It's fine.

Upvotes: 1

Related Questions