Using Indexed Views & Computed Columns for Performance !

Slides:



Advertisements
Similar presentations
Yukon – What is New Rajesh Gala. Yukon – What is new.NET Framework Programming Data Types Exception Handling Batches Databases Database Engine Administration.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
AN OVERVIEW & INTRODUCTION TO USING DACPACS D ata-Tier Applications {Application and Multi-Server Management} Neil Hambly York Oct 2010.
Dos and don’ts of Columnstore indexes The basis of xVelocity in-memory technology What’s it all about The compression methods (RLE / Dictionary encoding)
Module 6 Implementing Table Structures in SQL Server ®2008 R2.
Introduction to Structured Query Language (SQL)
Database Systems More SQL Database Design -- More SQL1.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
HAP 709 – Healthcare Databases SQL Data Manipulation Language (DML) Updated Fall, 2009.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
1 Optimizing Your ColdFusion Applications for Oracle Justin Fidler, CNA, CPS, CCFD Chief Technology Officer Bantu, Inc. 8 May 2001.
SQL Server Indexes Indexes. Overview Indexes are used to help speed search results in a database. A careful use of indexes can greatly improve search.
Views Lesson 7.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Module 4 Designing and Implementing Views. Module Overview Introduction to Views Creating and Managing Views Performance Considerations for Views.
Indexes / Session 2/ 1 of 36 Session 2 Module 3: Types of Indexes Module 4: Maintaining Indexes.
SQL SeQueL -Structured Query Language SQL SQL better support for Algebraic operations SQL Post-Relational row and column types,
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Indexes and Views Unit 7.
Working with SQL Server Database Objects Faculty: Nguyen Ngoc Tu.
Session id: Darrell Hilliard Senior Delivery Manager Oracle University Oracle Corporation.
Virtual techdays INDIA │ august 2010 Filtered Indexes – The unexplored index … Vinod Kumar M │ Microsoft India Technology Evangelist – DB and BI.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Database Systems, 8 th Edition SQL Performance Tuning Evaluated from client perspective –Most current relational DBMSs perform automatic query optimization.
Retele de senzori Curs 2 - 1st edition UNIVERSITATEA „ TRANSILVANIA ” DIN BRAŞOV FACULTATEA DE INGINERIE ELECTRICĂ ŞI ŞTIINŢA CALCULATOARELOR.
SQL Triggers, Functions & Stored Procedures Programming Operations.
--A Gem of SQL Server 2012, particularly for Data Warehousing-- Present By Steven Wang.
Execution Plans Detail From Zero to Hero İsmail Adar.
Module 6: Creating and Maintaining Indexes. Overview Creating Indexes Understanding Index Creation Options Maintaining Indexes Introducing Statistics.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
Load Testing with SQL Server Tools Neil Hambly SQL Server Practice Lead Northdoor PLC.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Doing fast! Optimizing Query performance with ColumnStore Indexes in SQL Server 2012 Margarita Naumova | SQL Master Academy.
2 Copyright © 2008, Oracle. All rights reserved. Building the Physical Layer of a Repository.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Server Statistics and its relationship with Query Optimizer
SQL Query Getting to the data ……..
Temporal Databases Microsoft SQL Server 2016
Implementing Views Advanced Database Dr. AlaaEddin Almabhouh.
MySQL Subquery Source: Dev.MySql.com
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
Temporal Databases Microsoft SQL Server 2016
SQL Implementation & Administration
T-SQL: Simple Changes That Go a Long Way
Database Systems: Design, Implementation, and Management Tenth Edition
Module 7: Implementing Views
Database Performance Tuning and Query Optimization
Four Rules For Columnstore Query Performance
Blazing-Fast Performance:
Cardinality Estimator 2014/2016
Introduction to partitioning
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Statistics: What are they and How do I use them
Steve Hood SimpleSQLServer.com
A Guide to SQL, Eighth Edition
SQL: Structured Query Language
Four Rules For Columnstore Query Performance
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Chapter 11 Database Performance Tuning and Query Optimization
Score a (row) goal and beat a query optimizer
Diving into Query Execution Plans
Database Systems: Design, Implementation, and Management Tenth Edition
Why You Should Consider Implementing Indexed Views
IST 318 Database Administration
Why You Should Consider Implementing Indexed Views
Chapter 8 Views and Indexes
All about Indexes Gail Shaw.
Why You Should Consider Implementing Indexed Views
Presentation transcript:

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

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

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)

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.

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 may @ times reduce inefficient execution plans.  

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.

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.

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.

Indexes & Statistics

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

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(*)

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.

References Improving Performance with SQL Server 2008 Indexed Views http://technet.microsoft.com/en-us/library/dd171921.aspx Improving Performance with SQL Server 2005 Indexed Views http://technet.microsoft.com/en-gb/library/cc917715.aspx Improving Performance with SQL Server 2000 Indexed Views http://technet.microsoft.com/en-us/library/cc917717.aspx Inline Functions over indexed views http://technet.microsoft.com/en-us/library/ms189294.aspx

(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 emails to Neil.Hambly@hotmail.co.uk Thank You (A Special mention to our Sponsor’s)

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.

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.

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