Introduction:
In this article, i am going to explain about how to effectively use reular view,indexed view and
distributed partitioned view.
Main:
Views allow you to create a virtual representation of table data defined by a SELECT statement.
The defining SELECT statement can join one or more tables and can include one or more columns.
Once created, a view can be referenced in the FROM clause of a query,
Regular view –
This view is defined by a Transact-SQL query. No data is actually stored in the database, only the view definition.
Indexed view –
This view is first defined by a Transact-SQL query, and then, after certain requirements are met, a clustered index
is created on it in order to materialize the index data similar to table data. Once a clustered index is created,
multiple nonclustered indexes can be created on the indexed view as needed.
Distributed partitioned view –
This is a view that uses UNION ALL to combine multiple, smaller tables separated across two or more SQL
Server instances into a single, virtual table for performance purposes and scalability (expansion of table size on each
SQL Server instance, for example).
Regular view:
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH [ ENCRYPTION ] [ SCHEMABINDING ] [ VIEW_METADATA ] [ ,...n ] ]
AS select_statement
[ WITH CHECK OPTION ]
USE NetProgrammingHelp
GO
CREATE VIEW dbo.v_Product_TransactionHistory
AS
SELECT p.Name ProductName,
p.ProductNumber,
c.Name ProductCategory,
s.Name ProductSubCategory,
m.Name ProductModel,
t.TransactionID,
t.ReferenceOrderID,
t.ReferenceOrderLineID,
t.TransactionDate,
t.TransactionType,
t.Quantity,
t.ActualCost
FROM Production.TransactionHistory t
INNER JOIN Production.Product p ON
t.ProductID = p.ProductID
INNER JOIN Production.ProductModel m ON
m.ProductModelID = p.ProductModelID
INNER JOIN Production.ProductSubcategory s ON
s.ProductSubcategoryID = p.ProductSubcategoryID
INNER JOIN Production.ProductCategory c ON
c.ProductCategoryID = s.ProductCategoryID
WHERE c.Name = 'Bikes'
GO
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ] [ WITH [ ENCRYPTION ] [ SCHEMABINDING ] [ VIEW_METADATA ] [ ,...n ] ] AS select_statement [ WITH CHECK OPTION ] USE NetProgrammingHelp GO CREATE VIEW dbo.v_Product_TransactionHistory AS SELECT p.Name ProductName, p.ProductNumber, c.Name ProductCategory, s.Name ProductSubCategory, m.Name ProductModel, t.TransactionID, t.ReferenceOrderID, t.ReferenceOrderLineID, t.TransactionDate, t.TransactionType, t.Quantity, t.ActualCost FROM Production.TransactionHistory t INNER JOIN Production.Product p ON t.ProductID = p.ProductID INNER JOIN Production.ProductModel m ON m.ProductModelID = p.ProductModelID INNER JOIN Production.ProductSubcategory s ON s.ProductSubcategoryID = p.ProductSubcategoryID INNER JOIN Production.ProductCategory c ON c.ProductCategoryID = s.ProductCategoryID WHERE c.Name = 'Bikes' GO |
Displaying views and their structures in current database,
SELECT s.name SchemaName,
v.name ViewName
FROM sys.views v
INNER JOIN sys.schemas s ON
v.schema_id = s.schema_id
ORDER BY s.name,
v.name
SELECT s.name SchemaName, v.name ViewName FROM sys.views v INNER JOIN sys.schemas s ON v.schema_id = s.schema_id ORDER BY s.name, v.name |
Encrypting a view,
CREATE VIEW dbo.v_Product_TopTenListPrice
WITH ENCRYPTION
AS
SELECT TOP 10
p.Name,
p.ProductNumber,
p.ListPrice
FROM Production.Product p
ORDER BY p.ListPrice DESC
GO
CREATE VIEW dbo.v_Product_TopTenListPrice WITH ENCRYPTION AS SELECT TOP 10 p.Name, p.ProductNumber, p.ListPrice FROM Production.Product p ORDER BY p.ListPrice DESC GO |
Indexed views,
CREATE VIEW dbo.v_Product_Sales_By_LineTotal
WITH SCHEMABINDING
AS
SELECT p.ProductID, p.Name ProductName,
SUM(LineTotal) LineTotalByProduct,
COUNT_BIG(*) LineItems
FROM Sales.SalesOrderDetail s
INNER JOIN Production.Product p ON
s.ProductID = p.ProductID
GROUP BY p.ProductID, p.Name
GO
CREATE VIEW dbo.v_Product_Sales_By_LineTotal WITH SCHEMABINDING AS SELECT p.ProductID, p.Name ProductName, SUM(LineTotal) LineTotalByProduct, COUNT_BIG(*) LineItems FROM Sales.SalesOrderDetail s INNER JOIN Production.Product p ON s.ProductID = p.ProductID GROUP BY p.ProductID, p.Name GO |
Partitioned views,
CREATE VIEW dbo.v_WebHits AS
SELECT WebHitID,
WebSite,
HitDT
FROM TSQLRecipeTest.dbo.WebHits_MegaCorp
UNION ALL
SELECT WebHitID,
WebSite,
HitDT
FROM JOEPROD.TSQLRecipeTest.dbo.WebHits_MiniCorp
GO
CREATE VIEW dbo.v_WebHits AS SELECT WebHitID, WebSite, HitDT FROM TSQLRecipeTest.dbo.WebHits_MegaCorp UNION ALL SELECT WebHitID, WebSite, HitDT FROM JOEPROD.TSQLRecipeTest.dbo.WebHits_MiniCorp GO |
Conclusion:
Hope this helps,
Happy coding.
Dear admin, thnx for sharing this blog post. I found it wonderful. Best regards, Victoria…
Hey admin, very informative blog post! Pleasee continue this awesome work..
Hey! Your blog entries are absolutely moving. I just bookmarked your website, and want to come back here often!
Sound good. Its also my favorite topic.That’s great andthanks for the fine sharring.