Presentation is loading. Please wait.

Presentation is loading. Please wait.

Using Indexed Views & Computed Columns for Performance !

Similar presentations


Presentation on theme: "Using Indexed Views & Computed Columns for Performance !"— Presentation transcript:

1 Using Indexed Views & Computed Columns for Performance !
SQL Server 2005 & 2008, 2000 (EE only) Neil Hambly (MDSL) UG Meeting Kent- August 2010

2 Neil Hambly MS SQL Server Database Specialist (12+ Yrs)
Permanent Roles MDSL (London) July 2010 – Present DB Architect Editions: SQL 2008 (R2), SQL 2005 Key Features: Mirroring, Service Broker, SSRS,SSAS iProfile (London) Oct 2004 – June 2010 DB Architect | DBA | DB Developer (Database Lead ) Editions: SQL 2008 (R2), SQL 2005, SQL 2000 Key Features: Clustering, Service Broker, Replication, SSRS Accenture (Dublin) June 1999– Oct 2004 DBA | DB Developer (Global BI - Team Leader) Editions: SQL 2000, SQL 7, SQL6.5 Key Features: Replication, DTS, OLAP Contractor Roles BBC 14 Months (DB Support ) ABN AMRO 16 Months (DB Developer ) Editions: SQL7, SQL6.5 UK SQL User Group (London) Organiser / Presenter

3 Agenda Requirement s for indexing a Computed column
Computed columns & Indexed Views Requirement s for indexing a Computed column (Base Tables, Indexed Views) The Query Optimizer and Computed Columns Types of Views, Querying indexed Views Prerequisites , Prerequisites , Prerequisites .. Tools that could help. DDL Syntax / Query Optimizer Hints (EXPAND; NOEXPAND) Compatibility levels, Partitions. Demo Home-work (Blog to be posted soon...) Disabling indexed Views, maintenance (like other indexes) Inline functions against Indexed Views. Filtered Indexes vs. Indexed Views. (2008) Partitioned Aligned Indexed Views (2008)

4 You can define indexes on computed columns as long as the following requirements are met:
Ownership Determinism Precision & Data type SET Options An imprecise expression that forms the value of an index key column must reference a stored column in a base table underlying the view. This column may be a regular stored column or a persisted computed column. No other imprecise expressions can be part of the key column of an indexed view.

5 The Query Optimizer and Computed Columns
One of the steps performed during Query Optimization (QO) is the automatic matching of computed columns. Although computed columns have been available in previous versions of SQL Server, the automatic matching feature was introduced in SQL Server 2005. A common problem are queries with scalar expressions As these are unable to benefit from using column statistics. Statistics provide cardinality estimation to help the QO to create better execution plans. Without statistics, the QO will use a 30% selectivity on inequality comparisons which times reduce inefficient execution plans.

6 A solution to this problem is the use of computed columns.
SQL Server can create and update statistics on these columns. The great benefit of this solution is that you do not need to specify the name of the computed column in your queries for SQL Server to use its statistics. The Query Optimizer automatically matches the computed column definition to an existing scalar expression in a query, so your applications do not need to be changed.

7 What Types of Views exist
Indexed Views: The focus of this presentation (AKA materialized views). Standard Views: A Virtual table because the data in the view does not exist until the view is executed. Partitioned Views: Join horizontally partitioned data, making the data appear as if it comes from a single table.

8 Querying Indexed views
-- Getting the calculated view. SELECT <columns> FROM MyViewsName OPTION(EXPAND VIEWS) -- Getting the stored view. FROM MyViewsName WITH(NOEXPAND) In SQL Server Enterprise editions, the query optimizer Automatically Considers the indexed view. To use an indexed view in all other Editions,we need to explicitly use the NOEXPAND table hint.

9 Indexes & Statistics

10 Session Options Session options that must be ON
ANSI_NULLS ANSI_PADDING ANSI_WARNINGS ARITHABORT CONCAT_NULL_YEILDS_NULL QUOTED_IDENTIFIERS Session options that must be OFF NUMERIC_ROUNDABORT

11 Requirements for Indexed views
View definition must always return the same results from the same underlying data, first index on a View must be a clustered UNIQUE index. View definition Not allowed to contain any of the following TOP, DISTINCT, ORDER BY Non-Deterministic Functions. Text, ntext or image columns MIN, MAX, COUNT, STDEV, VARIANCE, AVG SUM (on Nullable expressions) Reference Other Views, derived table, Rowset function UNION, Subqueries, Outer Joins, Self Joins Full-Text predicates like CONTAIN or FREETEXT COMPUTE or COMPUTE BY Group By, requires COUNT_BIG(*)

12 Any Tools to assist Creating Indexed Views?
The Database Tuning Advisor (DTA) was introduced is SQL Server 2000 as a feature that helps database to tune their physical database designs. DTA is able to recommend indexed views in addition to Recommending Indexes on base tables, as well as table and index partitioning strategies. Using DTA aids the administrator's ability to determine the Combination Of Indexes, indexed views, and partitioning strategies that optimize the Performance of the typical mix of queries executed against a Database, DTA can recommend a wide variety of indexed views. DTA will not eliminate the need for good judgment when designing physical storage structures. However, it can simplify the physical database design process. DTA operates in cooperation with the cost based query optimizer by proposing a set of hypothetical index, indexed view, and partition structures. DTA uses the optimizer to estimate the cost of your workload with and without these structures, and recommends structures that provide low overall cost. Because the Database Tuning Advisor forces all the required SET options (to ensure the result set is correct), its indexed view creation will succeed. However, your application may not be able to take advantage of the views if its option settings are not set as required. Inserts, updates, or deletes may fail on tables that Participate in indexed view definitions if the SET options aren't specified as required. Consequently, the maintenance of an indexed view may be more expensive than maintaining an index on the table. Conversely, the maintenance of an indexed view with a highly selective condition may be much less expensive than maintaining an index on a table because most inserts, deletes and updates to base tables the view references will not affect the view. These operations can be filtered out with respect to the indexed view without accessing other database data.

13 References Improving Performance with SQL Server 2008 Indexed Views
Improving Performance with SQL Server 2005 Indexed Views Improving Performance with SQL Server 2000 Indexed Views Inline Functions over indexed views

14 (A Special mention to our Sponsor’s)
Q & A Please ask your questions clearly and loudly. If you don’t get your questions answered now PLEASE ask me after the session, or s to Thank You (A Special mention to our Sponsor’s)

15 Disabling indexes (Views)
Disabling a clustered index on a view physically deletes the index data. The following additional guidelines apply to disabling indexes on views: Disabling a clustered index on a view does not prevent modifications to the underlying table. Disabling a clustered index on a view also disables any non-clustered indexes on that view. The index data rows for the clustered and non-clustered indexes are deleted. However, the view and index definitions remain in metadata and can be re-created by rebuilding the index or indexes. The ALTER INDEX ALL REBUILD statement rebuilds and enables all disabled indexes on the table, except for disabled indexes on views. Indexes on views must be enabled in a separate ALTER INDEX ALL REBUILD statement. Rebuilding the clustered index on a view does not automatically enable non-clustered indexes on the view. The non-clustered indexes must be manually enabled by rebuilding them after rebuilding the clustered index.

16 Inline Functions and Indexed Views
Inline functions can also be used to increase the power of indexed views. The indexed view itself cannot use parameters in its WHERE clause search conditions to tailor the stored result set to specific users. You can, however, define an indexed view that stores the complete set of data that matches the view, and then define an inline function over the indexed view that contains parameterized search conditions that allow users to tailor their results. If the view definition is complex, most of the work performed to build a result set involves operations such as building aggregates or joining several tables when the clustered index is created on the view. If you then create an inline function that references the indexed view, the function can apply the user's parameterized filters to return specific rows from the materialized result set of the indexed view.

17 Partition-Aligned Indexed Views
In SQL Server 2008, partition-aligned indexed views allow you to create and manage summary aggregates in your relational data warehouse more efficiently and use them in scenarios where you couldn't effectively use them before, this improves query performance. In a typical scenario, you have a fact table that is partitioned by date, Indexed views (or summary aggregates) are defined on this table to help speed up queries, when you switch in a new table partition, the matching partitions of the partition aligned indexed views defined on the partitioned table switch, too, and do so automatically. This is a significant improvement over SQL Server 2005, where you must drop any indexed views defined on a partitioned table before using the ALTER TABLE SWITCH operation to switch a partition in or out. The partition-aligned indexed views feature in SQL Server 2008 gives you the benefits of indexed views on large partitioned tables while avoiding the cost of rebuilding aggregates on an entire partitioned table, these benefits include automatic maintenance of aggregates and indexed view matching


Download ppt "Using Indexed Views & Computed Columns for Performance !"

Similar presentations


Ads by Google