Why You Should Consider Implementing Indexed Views

Slides:



Advertisements
Similar presentations
Keys, Referential Integrity and PHP One to Many on the Web.
Advertisements

SQL Server Replication
Chapter Physical Database Design Methodology Software & Hardware Mapping Logical Design to DBMS Physical Implementation Security Implementation Monitoring.
Fundamentals, Design, and Implementation, 9/e Chapter 11 Managing Databases with SQL Server 2000.
Topics Views Stored Procedures User Defined Functions Triggers.
ASP.NET Programming with C# and SQL Server First Edition
Physical Database Design Chapter 6. Physical Design and implementation 1.Translate global logical data model for target DBMS  1.1Design base relations.
Agenda for Today Chapter 5 –Skip Lesson 2 Review questions Midterm Chapter 6 Review questions.
SQL Server 7.0 Maintaining Referential Integrity.
Views Lesson 7.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Server 2005 Implementation and Maintenance Chapter 3: Tables and Views.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Learningcomputer.com SQL Server 2008 –Views, Functions and Stored Procedures.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
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.
SQL Triggers, Functions & Stored Procedures Programming Operations.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
SQL Basics Review Reviewing what we’ve learned so far…….
7.5 Using Stored-Procedure and Triggers NAME MATRIC NUM GROUP Muhammad Azwan Bin Khairul Anwar CS2305A Muhammad Faiz Bin Badrol Shah CS2305B.
Oracle Announced New In- Memory Database G1 Emre Eftelioglu, Fen Liu [09/27/13] 1 [1]
Presented by: Aaron Stanley King.  Benefits of SQL Azure  Features of SQL Azure  Demos, Demos, Demos!  How to query in SQL Azure  More Demos!  Recent.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
SQL IMPLEMENTATION & ADMINISTRATION Indexing & Views.
Chris Index Feng Shui Chris
SQL Query Getting to the data ……..
In-Memory Capabilities
Katowice,
Temporal Databases Microsoft SQL Server 2016
Relational Database Design
Record Storage, File Organization, and Indexes
Temporal Databases Microsoft SQL Server 2016
Antonio Abalos Castillo
MongoDB Er. Shiva K. Shrestha ME Computer, NCIT
SQL Implementation & Administration
SQL Creating and Managing Tables
Cameron Blashka| Informer Implementation Specialist
A Technical Overview of Microsoft® SQL Server™ 2005 High Availability Beta 2 Matthew Stephen IT Pro Evangelist (SQL Server)
Views, Stored Procedures, Functions, and Triggers
Query Tuning without Production Data
Designing Database Solutions for SQL Server
loooooooooooooong Introduction!
Isolation Levels Understanding Transaction Temper Tantrums
Purpose, Pitfalls and Performance Implications
Using Indexed Views & Computed Columns for Performance !
SQL Creating and Managing Tables
Traveling in time with SQL Server 2017
Physical Join Operators
Real world In-Memory OLTP
SQL 2014 In-Memory OLTP What, Why, and How
Purpose, Pitfalls and Performance Implications
SQL Creating and Managing Tables
20 Questions with Azure SQL Data Warehouse
Steve Hood SimpleSQLServer.com
Cloud Data Replication with SQL Data Sync
Four Rules For Columnstore Query Performance
Chapter 8 Advanced SQL.
Diving into Query Execution Plans
Chapter 11 Managing Databases with SQL Server 2000
Why You Should Consider Implementing Indexed Views
IST 318 Database Administration
Understanding Core Database Concepts
Isolation Levels Understanding Transaction Temper Tantrums
Michelle Haarhues Keeping up with SSMS.
Why You Should Consider Implementing Indexed Views
INTRODUCTION A Database system is basically a computer based record keeping system. The collection of data, usually referred to as the database, contains.
Responding to Data Manipulation Via Triggers
Presentation transcript:

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

About Me 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.

Thank you

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 Ancillary benefits: referential integrity enforcement manage code complexity 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?

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

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

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/ Ozar, Brent. “Availability Groups Bug with Indexed Views.” https://www.brentozar.com/archive/2018/05/availability-groups-bug-with-indexed-views/ Hammer, Derik. “SQL Server Schemabinding.” https://www.sqlhammer.com/sql-server-schemabinding/ Little, Kendra. “Find Corrupted Indexed Views with DBCC CHECKDB.” https://www.brentozar.com/archive/2015/04/find-corruption-indexed-view-dbcc-checkdb/ 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). White, Paul. “An Indexed View Bug with Scalar Aggregates.” https://sqlperformance.com/2015/04/sql-indexes/an-indexed-view-bug-with-scalar-aggregates 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/ Shaw, Gail. “Gail Shaw’s SQL Server Howlers.” https://www.red-gate.com/simple-talk/sql/database-administration/gail-shaws-sql-server-howlers/