![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
What gives? Why is WITH SCHEMABINDING required? |
#3
| |||
| |||
|
|
On Wed, 05 Jan 2011 14:55:30 -0800, Gene Wirchenko <genew (AT) ocis (DOT) net wrote: (snip) What gives? Why is WITH SCHEMABINDING required? Consider this simple example: CREATE VIEW dbo.v1 WITH SCHEMABINDING AS SELECT CAST(1 AS int) AS a; go CREATE FUNCTION dbo.f2() RETURNS TABLE --WITH SCHEMABINDING AS RETURN (SELECT a FROM dbo.v1) ; go SELECT * FROM dbo.f2(); SELECT * FROM dbo.f2(); go SELECT OBJECTPROPERTYEX(OBJECT_ID('dbo.f2'), 'IsDeterministic') SELECT OBJECTPROPERTYEX(OBJECT_ID('dbo.f2'), 'IsPrecise') go ALTER VIEW dbo.v1 AS SELECT CAST(CURRENT_TIMESTAMP AS float) AS a; go SELECT * FROM dbo.f2(); SELECT * FROM dbo.f2(); go DROP FUNCTION dbo.f2; DROP VIEW dbo.v1; go Without schemabinding, there is no link between the function and the underlying view. When the view changes, the properties of the function would not change, but they would no longer be correct. |
#4
| |||
| |||
|
|
I thought that the determination was if the input parameters are the same and the database state is the same, then yes. To me, changing the database state would mean that all bets are off. create function dbo.contrived() returns int as return 1 The above function is supposedly not deterministic. It does not touch any database! The example that got me going was an interest calculation function that took rate, amount, from date, and to date. It did not touch a database either. |
#5
| |||
| |||
|
|
Why not? |
#6
| |||
| |||
|
|
Gene Wirchenko (genew (AT) ocis (DOT) net) writes: I thought that the determination was if the input parameters are the same and the database state is the same, then yes. To me, changing the database state would mean that all bets are off. create function dbo.contrived() returns int as return 1 The above function is supposedly not deterministic. It does not touch any database! The example that got me going was an interest calculation function that took rate, amount, from date, and to date. It did not touch a database either. I guess it is a limitation or a simplification. WITH SCHEMABINDING gives a required, but not sufficient condition, that the function is deterministic. That is simply what Microsoft has decided. They could maybe have designed it differently, but then they would have had to run more complex analysis when the function is used, and when the function is altered or dropped. |
|
To the price that we users have to add WITH SCHEMABINDING to make our function deterministic. A fairly small price, I would say. |
#7
| |||
| |||
|
#8
| |||
| |||
|
|
I think this is another MS versus ANSI/ISO. In the SQL/PSM language one of the options in a CREATE FUNCTION is DETERMINISTIC. That means the optimizer can replace a call with the same arguments (parameters are the formal place holders; arguments are the actual values in a CALL) with a constant result. This is a handy thing since a good optimizer will look across the sessions invoking the function and turn it into a look-up table. A JOIN replaces re-computing math, temporal or string functions. Some food for thought on the matter. |
![]() |
| Thread Tools | |
| Display Modes | |
| |