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 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 Thank you

7 What is an Indexed View?

8 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

9 Demo #1

10 Why should you create Indexed Views?

11 Primary benefits: increase the performance of queries that contain:
Expensive aggregate calculations Expensive join logic Queries that join hierarchies Relatively fast implementation Ancillary benefits: referential integrity enforcement manage code complexity Performance tuning slow reports? Performance tuning an application?

12 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)

13 Important Indexed View Considerations

14 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

15 Indexed View Pre-requisites

16 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

17 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

18 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

19

20 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;

21 SET Options (Part 2)

22 SET Options (Part 3)

23 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)

24 Demo #2

25 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?

26 Bugs Availability Groups Indexed View bug
Brent Ozar - May 2018 SQL Server 2017 Memory dumps for queries on replicas that reference a dropped index on the indexed view Azure SQL DB – secondary replicate restart on 1st query failure Solution: DBCC FREESYSTEMCACHE('ALL'); Scalar Aggregates Indexed View bug Paul White - April 2018 Insert/deletes on base table Only affects scalar (not vector) aggregates

27 Monitoring Scalar Aggregate Bug
DBCC CHECKDB (‘IndexedViewName’); is not good enough for scalar aggregate bug detection Compatibility level 100+ Instead: Create a backup Run this on the backup: DBCC CHECKDB(‘IndexedViewName’) WITH EXTENDED_LOGICAL_CHECKS; (Thank you Kendra Little!)

28 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.” Ozar, Brent. “Availability Groups Bug with Indexed Views.” Hammer, Derik. “SQL Server Schemabinding.” Little, Kendra. “Find Corrupted Indexed Views with DBCC CHECKDB.” 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. ( ). White, Paul. “An Indexed View Bug with Scalar Aggregates.” StackOverflow Database download link and instructions (Thank you Brent!): Shaw, Gail. “Gail Shaw’s SQL Server Howlers.”


Download ppt "Why You Should Consider Implementing Indexed Views"

Similar presentations


Ads by Google