⟩ How To Create an Index on a View?
If you need to search and sort data in a view with a large number of row, you may want to create an index on the view to speed up your search process.
The tutorial exercise below shows you how to create a unique clustered index on a view.
DROP VIEW ggl_links_view;
GO
CREATE VIEW ggl_links_view (ID, UrlReversed)
AS SELECT id, REVERSE(url)
FROM ggl_links_copy WHERE counts > 1000;
GO
CREATE UNIQUE CLUSTERED INDEX date_string
ON ggl_links_view (ID);
GO
Cannot create index on view 'ggl_links_view'
because the view is not schema bound.
ALTER VIEW ggl_links_view (ID, UrlReversed)
WITH SCHEMABINDING
AS SELECT id, REVERSE(url)
FROM dbo.ggl_links_copy WHERE counts > 1000;
GO
CREATE UNIQUE CLUSTERED INDEX date_string
ON ggl_links_view (ID);
GO
EXEC SP_HELP ggl_links_view;
GO
index_name index_description index_keys----------- ------------------------------------ ----------
date_string clustered, unique located on PRIMARY ID