Views with SchemaBinding
...
Pointless no? Or am I missing the point?
(More properly, indexed views, schemabinding is a means to an end here, and the rest of the text is more talking about indexed views)
There can be (at least) two reasons for creating an indexed view. Without seeing your database, it's impossible to tell which of those reasons apply.
The first is to compute intermediate results which are expensive to compute from the base table. In order to benefit from that computation, you need to ensure your query uses the indexes. To use the indexes you either need to be querying the view and specifying NOEXPAND
, or be using Enterprise or Developer edition (On Ent/Dev editions the index might be used even if the base table is queried and the view isn't mentioned)
The second reason is to enforce a constraint that isn't enforceable in a simpler manner, by implementing e.g. a unique
constraint on the view, this may be enforcing some form of conditional uniqueness on the base table.
An example of the second - say you want table T to be able to contain multiple rows with the same U
value - but of those rows, only one may be marked as the Default
. Before filtered indexes were available, this was commonly achieved as:
CREATE VIEW DRI_T_OneDefault
WITH SCHEMABINDING
AS
SELECT U
FROM S.T
WHERE Default = 1
GO
CREATE UNIQUE CLUSTERED INDEX IX_DRI_T_OneDefault on DRI_T_OneDefault (U)
The point is that these indexes enforce a constraint. It doesn't matter (in such a case) whether any query every actually uses the index. In the same way that any unique constraint may be declared on a base table but never actually used in any queries.