sql-server

2 Post

oracle

2 Post

postgresql

2 Post

my-sql

2 Post

News

5 News

Materialized View
Materialized View

Materialized view in SQL Server is an index view.

Basically, all you need to do is:

·         create a schema bind view

The first index created on a view must be a unique clustered index. Creating a unique clustered index on a view improves query performance, because the view is stored in the database in the same way a table with a clustered index is stored. The query optimizer can use indexed views to speed up the query execution. The view doesn't have to be referenced in the query for the optimizer to consider that view for a substitution.

·         create a clustered index on that view

CREATE TABLE students

(id INTEGER PRIMARY KEY,

name varchar(40),

grade INTEGER,

gpa FLOAT,

tardies INTEGER);

 

INSERT INTO students (id, name, grade, gpa, tardies)

   VALUES (1,'Jake', 12, 3.5, 4);

INSERT INTO students (id, name, grade, gpa, tardies)

   VALUES (2,'Emily', 10, 3.7, 2);

INSERT INTO students (id, name, grade, gpa, tardies)

   VALUES (3,'Sam', 11, 3.5, 3);

INSERT INTO students (id, name, grade, gpa, tardies)

   VALUES (4,'Jordan', 10, 3.6, 2);

 

INSERT INTO students (id, name, grade, gpa, tardies)

   VALUES (6,'Antony', 19, 3.2, 3);

 

create view v_students with schemabinding

as

select name, grade, gpa, tardies

from dbo.students

 

CREATE UNIQUE CLUSTERED INDEX CX_v_students

ON dbo.v_students(name, grade, gpa, tardies);

Select * from v_students WITH (INDEX(CX_v_students))