Why You Should Consider Implementing Indexed Views Alex Fleming Why You Should Consider Implementing Indexed Views
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 Azure SQL Database PowerShell Excel (VBA)
Should You Create Indexed Views?
No.
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 What does that mean? 1) Querying the view does not require base table access 2) Updates, Inserts, and Deletes require additional overhead
Demo #1
Why should you create Indexed Views?
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?
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)
Important Indexed View Considerations
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
Indexed View Pre-requisites
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
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
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
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;
SET Options (Part 2)
SET Options (Part 3)
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)
Demo #2
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?
Further Reading 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/