Why You Should Consider Implementing Indexed Views Alex Fleming, Data Developer
Alex Fleming, Data Developer About me Data Developer at SUNZ Insurance Solutions SQL Developer (since June 2016) Guest Author at simpleprogrammer.com https://simpleprogrammer.com/vba-data-analysis- automation/ https://simpleprogrammer.com/maximizing-efficiency- vba/ https://simpleprogrammer.com/mastering-sql/ https://simpleprogrammer.com/microsoft-sql-server- metadata-developers/ SQL Server (SSRS,SSIS,SSAS 2008R2 – 2016), OLTP + OLAP environments PowerShell Excel (VBA) Alex Fleming, Data Developer
https://github.com/SqlBroveloper Twitter: @SQLBroveloper Alex Fleming, Data Developer
Should you create indexed views? Alex Fleming, Data Developer
Alex Fleming, Data Developer No. Alex Fleming, Data Developer
Alex Fleming, Data Developer Further Reading Alex Fleming, Data Developer
Alex Fleming, Data Developer What is an Indexed View? Indexed (or “Materialized”) view – a schemabound view in which data is persisted on disc in the format of a unique clustered index Alex Fleming, Data Developer
Should you create indexed views? It depends on the following: What is the problem? Performance tuning slow reports? Performance tuning an application? Does the data change frequently in the base tables (i.e., update, insert, and delete statements)? Is TempDB heavily utilized already? Are report tables an option? Alex Fleming, Data Developer
Alex Fleming, Data Developer Why should you use them? Increase the performance of queries that contain: Expensive aggregate calculations Expensive join logic 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 Alex Fleming, Data Developer
Why should you not use them? High resource consumption in Tempdb High maintenance costs for Inserts and Updates against base tables Volume and complexity of requirements and limitations Alex Fleming, Data Developer
Indexed View Pre-Requisites 1) SCHEMABINDING 2) Deterministic functions only 3) SET options 4) TempDB dependency 5) Unique Clustered Index Two-part naming required (i.e., [schema].[table]) COUNT_BIG(*) How DML is tracked on underlying tables Alex Fleming, Data Developer
Alex Fleming, Data Developer 1) 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 Alex Fleming, Data Developer
2) Prohibition of Non-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 Alex Fleming, Data Developer
Alex Fleming, Data Developer
Alex Fleming, Data Developer 3) SET Options (Part ¼) 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) Alex Fleming, Data Developer
Alex Fleming, Data Developer 3) SET Options (Part 2/4) SET ARITHABORT ON; Implicitly turned on when ANSI_WARNINGS is on Performance issues arise when turned off SET CONCAT_NULL_YIELDS_NULL ON; Alex Fleming, Data Developer
Alex Fleming, Data Developer 3) SET Options (Part ¾) SET NUMERIC_ROUNDABORT OFF; Alex Fleming, Data Developer
Alex Fleming, Data Developer 3) SET Options (Part 4/4) SET QUOTED_IDENTIFIER ON; Alex Fleming, Data Developer
Alex Fleming, Data Developer 4) TempDB Space Prevent changes on parent objects from breaking child objects “Safety” feature – best practices? Slight Performance enhancement – eliminate the need for Halloween Protection Alex Fleming, Data Developer
Alex Fleming, Data Developer Demos! Alex Fleming, Data Developer
Further Reading & References docs.microsoft.com https://docs.microsoft.com/en-us/sql/relational-databases/views/create-indexed-views?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/relational-databases/user-defined-functions/deterministic -and-nondeterministic-functions?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/statements/set-ansi-nulls-transact-sql?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/statements/set-ansi-warnings-transact-sql?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/statements/set-arithabort-transact-sql?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/statements/set-concat-null-yields-null-transact-sql?view=sql-server-2017 https://docs.microsoft.com/en-us/sql/t-sql/statements/set-quoted-identifier-transact-sql?view=sql-server-2017 Darling, Erik. “Indexed Views and Data Modifications.” https://www.brentozar.com/archive/2017/03/indexed-views-data-modifications/ Ozar, Brent. “What You Can (And Can’t) Do With Indexed Views.” https://www.brentozar.com/archive/2013/11/what-you-can-and-cant-do-with-indexed-views/ Hammer, Derik. “SQL Server Schemabinding.” https://www.sqlhammer.com/sql-server-schemabinding/ Borland, Jes. “SQL Server Indexed Views: The Basics.” https://www.red-gate.com/simple-talk/sql/learn-sql-server/sql-server-indexed-views-the-basics/ Ben-Gan, Itzik. “Views.” Inside Microsoft SQL Server 2008. (1-29). Korotkevitch, Dmitri. “Indexed (Materialized) Views.” Pro SQL Server Internals. (219-225). StackOverflow Database download link and instructions (Thank you Brent!): https://www.brentozar.com/archive/2015/10/how-to-download-the-stack-overflow-database-via-bittorrent/ Alex Fleming, Data Developer