Functions Flashcards

1
Q

What are the 2 types of user-defined functions (UDFs) that SQL Server supports?

A

Scalar and Table-valued

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Can functions update or change a database?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is a scalar function?

A

Returns a single value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What does inlined mean?

A

The compiler will automatically incorporate the whole body of the function into the surrounding code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is interpreted code?

A

compiler invoking it from the main body of code requires a jump to a different code block to execute the function.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the biggest potential drawback of SQL Server Scalar functions?

A

SQL Server will execute the function once for every row in the result set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How many times are Table Valued Functions called?

A

SQL Server will call them only once, regardless of the number of rows in the result set

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is deterministic?

A

Given the same inputs will return the same result each time.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Give an example of a non-deterministic function?

A

GetDate()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How many times are non-deterministic functions called?

A

Once per statement

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many times are deterministic functions called

A

Once per row

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Why use schema binding?

A

Makes it more difficult to make changes to the underlying data structures that would break our functions

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How can you prevent function processing data when a null is provided for an input

A

Use the RETURNS NULL ON NULL INPUT option, which will cause SQL Server to immediately return NULL

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How can you tell if a scalar function is being called once for every row in dataset

A

Look at the query execution plan. if in the properties it references the function name (rather than the column name), then the function is being called once per row.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Are there performance implication to using a scalar function in the WHERE clause?

A

Yes, If you are using an indexed column within the scalar function then this can lead to a full table scan rather than use of the index

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Where can a function be used?

A

Almost anywhere that we would use a table or column

17
Q

How does using multiple statements in a table valued functions affect performance

A

It treats them rather like a table for which it has no available statistics. As a result, even a very simple multi-statement TVF can cause severe performance problems.

18
Q

How can you avoid the performance cost of row by row execution of a scalar user defined function

A

Convert it into a single statement table valued function

19
Q

How can you improve the performance of a multi statement table valued function

A

Use a CTE (Common table expression)