Create Indexed Views in SQL Server; SQL Equivalent of Oracle Materialized Views.

SQL Server supports creating indexes on a view. It is the same concept as Materialized Views in Oracle. The main difference in the syntax in normal views and indexed views is the clause “with schemabinding”

CREATE VIEW [dbo].[ViewName] with schemabinding
AS
SELECT id, activityID, userID, value, date
FROM dbo.TableName
WHERE (field = ‘activityID’)
Now you go ahead and create the index on the view you just created.
CREATE UNIQUE CLUSTERED INDEX [IDX_ActivityStatusID] ON [dbo].[ViewName]
(
[activityID] ASC,
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
GO

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s