T-SQL Basics: Coding for performance

Slides:



Advertisements
Similar presentations
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Advertisements

Physical Database Monitoring and Tuning the Operational System.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 11 Database Performance Tuning and Query Optimization.
Overview SQL Server 2008 Overview Presented by Tarek Ghazali IT Technical Specialist Microsoft SQL Server MVP, MCTS Microsoft Web Development MCP ITIL.
Troubleshooting SQL Server Enterprise Geodatabase Performance Issues
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 10 Database Performance Tuning and Query Optimization.
IT The Relational DBMS Section 06. Relational Database Theory Physical Database Design.
DBSQL 14-1 Copyright © Genetic Computer School 2009 Chapter 14 Microsoft SQL Server.
Module 7 Reading SQL Server® 2008 R2 Execution Plans.
SQL/Lesson 7/Slide 1 of 32 Implementing Indexes Objectives In this lesson, you will learn to: * Create a clustered index * Create a nonclustered index.
Physical Database Design Purpose- translate the logical description of data into the technical specifications for storing and retrieving data Goal - create.
Session 1 Module 1: Introduction to Data Integrity
Dave LinkedIn
How to kill SQL Server Performance Håkan Winther.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP,MCP. SQL SERVER Database Administration.
Scott Fallen Sales Engineer, SQL Sentry Blog: scottfallen.blogspot.com.
SQL Server Statistics DEMO SQL Server Statistics SREENI JULAKANTI,MCTS.MCITP SQL SERVER Database Administration.
3 Copyright © 2006, Oracle. All rights reserved. Designing and Developing for Performance.
Database Design: Solving Problems Before they Start! Ed Pollack Database Administrator CommerceHub.
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.
10/3/2017 Chapter 6 Index Structures.
SQL Server Statistics and its relationship with Query Optimizer
Parameter Sniffing in SQL Server Stored Procedures
Query Optimization Techniques
Indexes By Adrienne Watt.
Efficient data maintenance in GlusterFS using databases
Chapter 6 - Database Implementation and Use
Antonio Abalos Castillo
UFC #1433 In-Memory tables 2014 vs 2016
Informatica PowerCenter Performance Tuning Tips
Finding more space for your tight environment
Database Performance Tuning &
Parameter Sniffing in SQL Server Stored Procedures
SQL Server Monitoring Overview
Designing Database Solutions for SQL Server
Methodology – Physical Database Design for Relational Databases
Physical Database Design for Relational Databases Step 3 – Step 8
Software Architecture in Practice
Database Performance Tuning and Query Optimization
Hustle and Bustle of SQL Pages
Reading Execution Plans Successfully
CHAPTER 5: PHYSICAL DATABASE DESIGN AND PERFORMANCE
Introduction to Execution Plans
Power BI Performance …Tips and Techniques.
Marcos Freccia Stop everything! Top T-SQL tricks to a developer
Third Party Tools for SQL Server
The Key to the Database Engine
Agenda Database Development – Best Practices Why Performance Matters ?
Now where does THAT estimate come from?
Cardinality Estimator 2014/2016
國立臺北科技大學 課程:資料庫系統 fall Chapter 18
Predictive Performance
Database management concepts
Query Optimization Techniques
Physical Database Design
JULIE McLAIN-HARPER LINKEDIN: JM HARPER
Execution Plans Demystified
Statistics: What are they and How do I use them
Transactions, Locking and Query Optimisation
SQL Server Query Plans Journeyman and Beyond
Transact SQL Performance Tips
Database management concepts
Introduction to Execution Plans
Chapter 11 Database Performance Tuning and Query Optimization
Query Tuning Fundamentals
Diving into Query Execution Plans
Introduction to Execution Plans
Query Optimization Techniques
Introduction to Execution Plans
Presentation transcript:

T-SQL Basics: Coding for performance Eduardo Pivaral (MCSE, MCSA) – Feb 2019

Sponsors Gold Silver Geek

Eduardo Pivaral SQL Server Database Administrator/Developer www.sqlguatemala.com Regular technical author for different sources an developer/reviewer of Open Source tools for SQL Server database administration, development and productivity. Board member of PASS Guatemala SQL Server users group. Microsoft Certified Solutions Expert: Data Management and Analytics Microsoft Certified Solutions Associate: SQL 2016 Database Administration Microsoft Certified Solutions Associate: SQL Server 2012/2014 Microsoft Certified Professional MSSQLTips.com Rookie of the year 2018 Eduardo Pivaral @EduardoDBA Epivaral

Agenda Database programming mindset: Code for the future, not for the past Proper database design: Datatype selection, N-tier programming Indexes and statistics: Improve performance, but as alcohol, without abuse! Execution plans: Your first (and almost only) debugging tool DEMO: T-SQL tips and examples to improve performance

Database programming mindset: Code for the future, not for the past

Long Development life cycles can lead to releases on old systems Choose agile methodologies when possible Use databases for different modules Investigate new features that could improve existing code Design your application to support: High volumes of data and concurrency Implement data purging/historical movement processes Databases can grow uncontrollably due to message logs and historical data.

Implement reusable, scalable code On development and test machines, apply latest service packs/patches available on all your development software. Implement reusable, scalable code Comment and format your code: You won’t recognize your own code from time ago. Test your code trying to make it fail. Having issues? ask for help! Community is always willing to lend you a hand. Further reading: https://www.sqlguatemala.com/2018/02/considerations-for-dealing-with-big.html

Proper Database Design Fixing issues before they exists

N-Tier Programming Encapsulate Data operations on database tier It improves performance and security Code changes are transparent to application and presentation tier Minimize Locks by reducing network and IO usage Migrations are easier without database code on upper tiers

Avoid using SELECT * to send data to client application By explicitly defining columns, network and disk IO is reduced Security is improved by sending only required records ASYNC_NETWORK_IO wait can be avoided Filter data on the database tier Data sent to client is reduced even more SARGABLE: search +‎ argument +‎ -able Sort data on presentation tier Sorting is a resource-intensive operation, can negatively impact performance.

Proper Datatype Selection With millions of records, correct datatype make a lot of difference on performance and storage utilization Wrong datatypes will require data conversions (additional overhead) Filtering/Joining different datatypes will require implicit conversions (sub- optimal queries) Comparison between storage used for distinct datatypes storing the exact same data: Further reading: https://www.sqlguatemala.com/2018/07/saving-disk-space-by-choosing-correct.html

Indexes and Statistics Powerful allies if used correctly* *…but what is correct?

Indexes are special structures associated to tables or views that store information on a B-tree for quick data retrieval Clustered index: Stores the table ordered by a specific column (normally the primary key). There can be just one. Have faster performance on numeric, sequential values. Non-clustered index: A separate structure that stores the pointer to a row location on the original object, by a specific column. Performance depends on size, number of columns and datatypes. Heap: Table without clustered index. Only allows full table scans to locate records.

A good index is when values have a high selectivity (or cardinality) They improve query performance, but require additional disk storage and IO. Limit your index creation. A good index is when values have a high selectivity (or cardinality) High Selectivity Low Selectivity

A Covering index is when index is capable of provide all the data by itself, without accessing pointer to original object. Can be achieved by using indexed columns or by included columns. Included columns are not indexed, just are part of the index data Require extra storage, so use with caution Further reading: https://www.sqlguatemala.com/2018/02/performance-basics-key-lookup-operator.html

Statistics are used by Query Optimizer to create execution plans based on distribution of required values. Outdated or non-existent statistics can lead to poor performing queries, even with proper indexes created. Density is the number of duplicated values a column can have, is used to calculate selectivity and enhance cardinality estimates. High density variations on a given column can lead to parameter sniffing issues. Data with potential parameter sniffing issues

Updating statistics will cause related queries to recompile. With AUTO_CREATE_STATISTICS enabled, statistics are created by the query optimizer as necessary, with _WA prefix. Statistics automatically created, are strictly created on single columns. With AUTO_UPDATE_STATISTICS enabled, statistics are updated automatically by the engine when column changes reach a threshold*. Be aware that this threshold could not work for you. Updating statistics will cause related queries to recompile. Use DBCC SHOW_STATISTICS to show statistics information * https://docs.microsoft.com/en-us/sql/relational-databases/statistics/statistics?view=sql-server-2017#autoupdatestatistics-option

Your first (and almost only) debugging tool Execution Plans Your first (and almost only) debugging tool It seems you are trying to debug a query, do you need assistance?

Execution plans are the results of SQL declarative language It is a calculation of most efficient way to execute a query. Since this is an attempt, optimal way is not always the chosen one. If a query is slow, execution plan should be the first thing to check. Even when SSMS provides a built-in debugging feature, it is only useful for some cases. For example stored procedures, or queries with many lines of code. Further reading: https://www.sqlguatemala.com/2018/09/different-methods-to-generate-query.html https://sqlserverfast.com/epr/

Estimated vs Actual Execution Plan Estimated Plan is how a query will be executed based on existing indexes, statistics and server load at request moment. Actual plan include run time statistics (number of rows processed) to the execution plan information. For most cases, Estimated and Actual plan will be the same. Further reading: https://sqlserverfast.com/epr/common-properties/

T-SQL tips to improve performance Demos T-SQL tips to improve performance

Demos Compatibility level: be aware of deprecated features Filtering basics: SARGABLE arguments Table Scan vs Index Scan vs Index Seek Covering indexes Indexed views Implicit conversions Parameter Sniffing Memory Grants Joining records with NULL Database Collation: considerations when working with multiple databases DMOs to find top resource intensive queries

Want to know more? Having doubts about this presentation? Thank you! Want to know more? Having doubts about this presentation? Visit me at: www.SQLGuatemala.com Eduardo Pivaral @EduardoDBA Epivaral