Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why You Should Consider Implementing Indexed Views

Similar presentations


Presentation on theme: "Why You Should Consider Implementing Indexed Views"— Presentation transcript:

1 Why You Should Consider Implementing Indexed Views
Alex Fleming Why You Should Consider Implementing Indexed Views

2 About Me Data Developer at SUNZ Insurance Solutions SQL Developer (since June 2016) Guest Author at simpleprogrammer.com SQL Server (SSRS,SSIS,SSAS 2008R2 – 2016), OLTP + OLAP environments Azure SQL Database PowerShell Excel (VBA)

3

4 Should You Create Indexed Views?

5 No.

6 What is an Indexed View?

7 Indexed (or “Materialized”) view – a schemabound view in which data is persisted on disc in the format of a unique clustered index What does that mean? 1) Querying the view does not require base table access 2) Updates, Inserts, and Deletes require additional overhead

8 Demo #1

9 Why should you create Indexed Views?

10 Primary benefits: increase the performance of queries that contain:
Expensive aggregate calculations Expensive join logic Queries that join hierarchies Relatively fast implementation Performance tuning slow reports? Performance tuning an application?

11 Other benefits: Increase awareness of dependent objects during development (schemabinding requirement) Enforce uniqueness for known (i.e., not null) values while allowing nulls (without using triggers) They’re cool! Encourage some best-practices (more on this later)

12 Important Indexed View Considerations

13 Does the data change frequently in the base tables?
Numerous limitations (especially T-SQL) Are report tables an option? Is TempDB heavily utilized already? SQL Server Edition (Enterprise or Developer?) Express Edition requires WITH (NOEXPAND) hint

14 Indexed View Pre-requisites

15 2) Deterministic functions only 3) SET options
1) SCHEMABINDING 2) Deterministic functions only 3) SET options 4) Unique Clustered Index Two-part naming required (i.e., [schema].[table]) COUNT_BIG(*) How DML is tracked on underlying tables

16 SCHEMABINDING Prevent changes on parent objects from breaking child objects “Safety” feature – best practices? Slight Performance enhancement eliminate the need for Halloween Protection “Eager Table Spool” Plan operator

17 Deterministic Functions
Deterministic function – return the same output for any set of inputs Three types of functions: Deterministic functions Non-deterministic functions Functions that vary on determinism qualities depending on use

18

19 SET Options (Part 1) SET ANSI_NULLS ON; SET ANSI_PADDING ON;
SET ANSI_WARNINGS ON; Divide by 0 and arithmetic overflow cause rollback + warning warning when NULL appears in aggregate functions Parameters are unaffected (UDFs, Stored Procs, variables in batch statements) SET ARITHABORT ON; Implicitly turned on when ANSI_WARNINGS is on Performance issues arise when turned off SET CONCAT_NULL_YIELDS_NULL ON; SET NUMERIC_ROUNDABORT OFF; SET QUOTED_IDENTIFIER ON;

20 SET Options (Part 2)

21 SET Options (Part 3)

22 Alternatives Reporting Tables
- No T-SQL restrictions, SET options, SCHEMABINDING, etc. - Require ETL process to load & refresh the table - Other queries that don’t directly reference the table can’t benefit from the table’s index(es)

23 Demo #2

24 Indexed View Opportunity Recognition
Analyze existing view definitions Are any schemabound already? If so, are any indexable? select OBJECTPROPERTY(OBJECT_ID(‘[schema name].[view name]'),'IsIndexable’); Can you modify the view definition and remove T-SQL not supported by indexed views, and re-add the logic to a query referencing the indexed view?

25 Further Reading docs.microsoft.com -and-nondeterministic-functions?view=sql-server Darling, Erik. “Indexed Views and Data Modifications.” Ozar, Brent. “What You Can (And Can’t) Do With Indexed Views.” Hammer, Derik. “SQL Server Schemabinding.” Borland, Jes. “SQL Server Indexed Views: The Basics.” Ben-Gan, Itzik. “Views.” Inside Microsoft SQL Server (1-29). Korotkevitch, Dmitri. “Indexed (Materialized) Views.” Pro SQL Server Internals. ( ). StackOverflow Database download link and instructions (Thank you Brent!):


Download ppt "Why You Should Consider Implementing Indexed Views"

Similar presentations


Ads by Google