WRITING EFFICIENT SQL. By Selene Bainum

Similar documents
Chapter 9 Joining Data from Multiple Tables. Oracle 10g: SQL

MOC 20461C: Querying Microsoft SQL Server. Course Overview

50439B: Basics of Transact SQL with SQL Server 2008 R2

Querying Microsoft SQL Server (20461) H8N61S

Introduction to Querying & Reporting with SQL Server

Course 20461C: Querying Microsoft SQL Server Duration: 35 hours

Querying Microsoft SQL Server 2012

Querying Microsoft SQL Server

Course ID#: W 35 Hrs. Course Content

Querying Microsoft SQL Server 20461C; 5 days

Talking to Databases: SQL for Designers

Database Administration with MySQL

Module 1: Getting Started with Databases and Transact-SQL in SQL Server 2008

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

SQL Server. 1. What is RDBMS?

Oracle SQL. Course Summary. Duration. Objectives

Join Example. Join Example Cart Prod Comprehensive Consulting Solutions, Inc.All rights reserved.

MOC QUERYING MICROSOFT SQL SERVER

Introducing Microsoft SQL Server 2012 Getting Started with SQL Server Management Studio

How To Use A Microsoft Sql Server 2005

SQL SELECT Query: Intermediate

Writing Queries Using Microsoft SQL Server 2008 Transact-SQL

Querying Microsoft SQL Server Course M Day(s) 30:00 Hours

D61830GC30. MySQL for Developers. Summary. Introduction. Prerequisites. At Course completion After completing this course, students will be able to:

Duration Vendor Audience 5 Days Oracle End Users, Developers, Technical Consultants and Support Staff

Oracle Database: SQL and PL/SQL Fundamentals

Querying Microsoft SQL Server 2012

How To Improve Performance In A Database

DBMS / Business Intelligence, SQL Server

Course 10774A: Querying Microsoft SQL Server 2012

Course 10774A: Querying Microsoft SQL Server 2012 Length: 5 Days Published: May 25, 2012 Language(s): English Audience(s): IT Professionals

AV-005: Administering and Implementing a Data Warehouse with SQL Server 2014

SQL Query Performance Tuning: Tips and Best Practices

SQL Server for developers. murach's TRAINING & REFERENCE. Bryan Syverson. Mike Murach & Associates, Inc. Joel Murach

Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification

LearnFromGuru Polish your knowledge

ICAB4136B Use structured query language to create database structures and manipulate data

Paper TU_09. Proc SQL Tips and Techniques - How to get the most out of your queries

Advanced Query for Query Developers

Oracle Database: SQL and PL/SQL Fundamentals NEW

FHE DEFINITIVE GUIDE. ^phihri^^lv JEFFREY GARBUS. Joe Celko. Alvin Chang. PLAMEN ratchev JONES & BARTLETT LEARN IN G. y ti rvrrtuttnrr i t i r

Programming with SQL

Effective Use of SQL in SAS Programming

Oracle Database: SQL and PL/SQL Fundamentals

Oracle 10g PL/SQL Training

1 Structured Query Language: Again. 2 Joining Tables

Instant SQL Programming

A Comparison of Database Query Languages: SQL, SPARQL, CQL, DMX

Lab # 5. Retreiving Data from Multiple Tables. Eng. Alaa O Shama

Information Systems SQL. Nikolaj Popov

Oracle Database 10g: Introduction to SQL

Optimizing the Performance of the Oracle BI Applications using Oracle Datawarehousing Features and Oracle DAC

MySQL for Beginners Ed 3

Introduction to SQL for Data Scientists

Database Management Systems Comparative Study: Performances of Microsoft SQL Server Versus Oracle

Universe Best Practices Session Code: 806

Querying Microsoft SQL Server 2012

Define terms Write single and multiple table SQL queries Write noncorrelated and correlated subqueries Define and use three types of joins

Toad for Data Analysts, Tips n Tricks

Relational Division and SQL

Optimizing Your Data Warehouse Design for Superior Performance

SQL Server 2008 Core Skills. Gary Young 2011

Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM

Creating Reports Crystal Clear

Oracle Database 12c: Introduction to SQL Ed 1.1

Outline. SAS-seminar Proc SQL, the pass-through facility. What is SQL? What is a database? What is Proc SQL? What is SQL and what is a database

Optimizing Your Database Performance the Easy Way

Using SQL Queries to Insert, Update, Delete, and View Data: Joining Multiple Tables. Lesson C Objectives. Joining Multiple Tables

TrendWorX32 SQL Query Engine V9.2 Beta III

In this session, we use the table ZZTELE with approx. 115,000 records for the examples. The primary key is defined on the columns NAME,VORNAME,STR

SQL Server 200x Optimizing Stored Procedure Performance

Tuning Tableau and Your Database for Great Performance PRESENT ED BY

Relational Database: Additional Operations on Relations; SQL

Introduction to Microsoft Jet SQL

Optimizing Performance. Training Division New Delhi

SQL Server Query Tuning

Where? Originating Table Employees Departments

SQL Server Database Coding Standards and Guidelines

Oracle. Brief Course Content This course can be done in modular form as per the detail below. ORA-1 Oracle Database 10g: SQL 4 Weeks 4000/-

Physical DB design and tuning: outline

Lecture Slides 4. SQL Summary. presented by Timothy Heron. Indexes. Creating an index. Mathematical functions, for single values and groups of values.

David Dye. Extract, Transform, Load

SQL Server Query Tuning

Performance in the Infragistics WebDataGrid for Microsoft ASP.NET AJAX. Contents. Performance and User Experience... 2

Response Time Analysis

Performance Tuning for the Teradata Database

SQL Databases Course. by Applied Technology Research Center. This course provides training for MySQL, Oracle, SQL Server and PostgreSQL databases.

SQL Server 2016 New Features!

PERFORMANCE TUNING FOR PEOPLESOFT APPLICATIONS

Transaction Monitoring Version for AIX, Linux, and Windows. Reference IBM

Beginning C# 5.0. Databases. Vidya Vrat Agarwal. Second Edition

The Arts & Science of Tuning HANA models for Performance. Abani Pattanayak, SAP HANA CoE Nov 12, 2015

The process of database development. Logical model: relational DBMS. Relation

Displaying Data from Multiple Tables

Using AS/400 Database Monitor To Identify and Tune SQL Queries

Consulting. Personal Attention, Expert Assistance

Displaying Data from Multiple Tables. Copyright 2006, Oracle. All rights reserved.

Enhancing SQL Server Performance

DATABASE DESIGN AND IMPLEMENTATION II SAULT COLLEGE OF APPLIED ARTS AND TECHNOLOGY SAULT STE. MARIE, ONTARIO. Sault College

2874CD1EssentialSQL.qxd 6/25/01 3:06 PM Page 1 Essential SQL Copyright 2001 SYBEX, Inc., Alameda, CA

Transcription:

WRITING EFFICIENT SQL By Selene Bainum

About Me Extensive SQL & database development since 1995 ColdFusion Developer since 1996 Author & Speaker Co-Founder RiteTech LLC IT & Web Company in Washington, DC Area Created ModernHOA.com Web Site Platform for HOAs and Small Businesses

Overview Why Improve SQL Efficiency Design Considerations Worst Technique Joins & Subqueries Other Techniques & Tips Conclusion / Q&A

Efficiency, Who Cares? System Administrators Memory costs money and resources They hate being paged at night Site Visitors Slow site = lost customers Your Boss! You personal satisfaction of writing good code

Database Efficiency Decreases total page processing time Reduces load on the database Allows more time for other processes: Code logic HTML/JavaScript returned to browser Site overhead tasks

Sample Database USDA National Nutrient Database 13 tables Thousands of rows Available at: http://ndb.nal.usda.gov/

Design Considerations Keys/Indexes Primary Keys Foreign Keys Indexes Create index for each foreign key field in your table Create index for other fields commonly used for filters Speed up queries significantly Can increase time to insert/update

Worst Technique

Avoid: SELECT * Database must query column names from system table Takes more time than selecting column names even if all columns are needed Does not allow for column aliasing ColdFusion queries may break if columns added to table Better Yet: query only columns you need!

ColdFusion Metrics Food_Des table; 8194 Rows; Avg. over 10 queries SELECT * SELECT * FROM Food_Des Avg Time: 60.8 ms SELECT [All Column Names] SELECT NDB_No, FdGrp_Cd, Long_Desc, Shrt_Desc, ComName, ManufacName, Survey, Ref_Desc, Refuse, SciName, N_Factor, Pro_Factor, Fat_Factor, CHO_Factor FROM FOOD_DES Avg Time: 56.4 ms SELECT [Needed Columns] SELECT NDB_No, FdGrp_Cd FROM Food_Des Avg Time: 6.1 ms

Joins & Subqueries

Joins & Subqueries Are absolutely necessary and useful Increase processing time Can be optimized Don t use more than necessary

Join Types Inner Join Only returns rows where values exist and match in both tables Outer joins Returns rows from one table even if no matching value exists in other table Cross Join Returns Cartesian product of rows: every combination of values in left table for every value in right table Don t Do! Self Join Joining a table to itself either as inner or outer join

Join Options WHERE Clause INNER JOIN FROM T1, T2 WHERE T1.ID = T2.ID LEFT OUTER JOIN FROM T1, T2 WHERE T1.ID *= T2.ID RIGHT OUTER JOIN FROM T1, T2 WHERE T1.ID =* T2.ID

Join Options FROM Clause INNER JOIN FROM T1 INNER JOIN T2 ON T1.ID = T2.ID LEFT OUTER JOIN FROM T1 LEFT OUTER JOIN T2 ON T1.ID = T2.ID RIGHT OUTER JOIN FROM T1 RIGHT OUTER JOIN T2 ON T1.ID = T2.ID

Join Issues Standard SQL encourages joins in FROM clause FROM clause processed before WHERE clause Every database different for WHERE clause Check DB documentation for specific examples Microsoft SQL Server WHERE Clause Joins Future versions may not support at all

WHERE vs. FROM WHERE Clause: SELECT FD.NDB_No, FD.FdGrp_Cd, FD.Long_Desc, FD.Shrt_Desc, FG.FdGrp_Desc FROM Food_Des FD, Fd_Group FG WHERE FD.FdGrp_Cd = FG.FdGrp_Cd Average time: 121 ms FROM Clause: SELECT FD.NDB_No, FD.FdGrp_Cd, FD.Long_Desc, FD.Shrt_Desc, FG.FdGrp_Desc FROM Food_Des FD INNER JOIN Fd_Group FG ON FD.FdGrp_Cd = FG.FdGrp_Cd Average time: 110 ms

LEFT OUTER Joins - Filtering Goal: return all foods and their level of alcohol Tables Food_Des food definitions Nut_Data nutritional levels for foods Nutr_Def nutritional definitions Not all foods will have a nutritional record for alcohol

LEFT OUTER Joins - Filtering Add filter to the WHERE clause: SELECT FD.NDB_No, FD.Long_Desc, ND.Nutr_Val FROM Food_Des FD LEFT OUTER JOIN ( Nut_Data ND INNER JOIN Nutr_Def NR ON ND.Nutr_No = NR.Nutr_No ) ON FD.NDB_No = ND.NDB_No WHERE NR.NutrDesc LIKE '%alcohol%' ORDER BY FD.Long_Desc Returns 4887 records (not 8194) WHERE clause filter turns LEFT OUTER join into INNER join

LEFT OUTER Joins - Filtering Add filter to the FROM clause: SELECT FD.NDB_No, FD.Long_Desc, ND.Nutr_Val FROM Food_Des FD LEFT OUTER JOIN ( Nut_Data ND INNER JOIN Nutr_Def NR ON ND.Nutr_No = NR.Nutr_No ) ON FD.NDB_No = ND.NDB_No AND NR.NutrDesc LIKE '%alcohol%' ORDER BY FD.Long_Desc Returns 8194 records All foods returned, some with NULL for level of alcohol

Join vs. Subquery Joins Typically used when you want to return the data from the tables included Subqueries Typically used when you want to return data from one or more tables Results is dependent on data in other tables Data from those other tables is not desired

Subquery Goal: Return all food groups that contain foods with alcohol Tables: Fd_Group distinct food groups Food_Desc food definitions Nut_Data nutritional levels for foods Nutr_Def nutritional definitions Will only return 2 rows

WHERE IN SELECT FG.FdGrp_Cd, FG.FdGrp_Desc FROM Fd_Group FG WHERE FG.FdGrp_Cd IN ( SELECT FD.FdGrp_Cd FROM Food_Des FD INNER JOIN Nut_Data ND ON FD.NDB_No = ND.NDB_No INNER JOIN Nutr_Def NR ON ND.Nutr_No = NR.Nutr_No WHERE NR.NutrDesc LIKE '%alcohol% AND ND.Nutr_Val > 0 ) Returns entire sub-query for each row Very inefficient Avg. time: 465 ms

WHERE EXISTS SELECT FG.FdGrp_Cd, FG.FdGrp_Desc FROM Fd_Group FG WHERE EXISTS ( SELECT 1 FROM Food_Des FD INNER JOIN Nut_Data ND ON FD.NDB_No = ND.NDB_No INNER JOIN Nutr_Def NR ON ND.Nutr_No = NR.Nutr_No WHERE NR.NutrDesc LIKE '%alcohol%' AND FG.FdGrp_Cd = FD.FdGrp_Cd AND ND.Nutr_Val > 0 ) Returns much smaller subset for each row Much faster than WHERE IN Columns in sub-select are irrelevant Avg. time: 449 ms - 3.44% more efficient than WHERE IN

Why Not JOIN? SELECT DISTINCT FG.FdGrp_Cd, FG.FdGrp_Desc FROM Fd_Group FG INNER JOIN Food_Des FD ON FG.FdGrp_Cd = FD.FdGrp_Cd INNER JOIN Nut_Data ND ON FD.NDB_No = ND.NDB_No INNER JOIN Nutr_Def NR ON ND.Nutr_No = NR.Nutr_No AND NR.NutrDesc LIKE '%alcohol%' AND ND.Nutr_Val > 0 DISTINCT must be used to limit rows Much slower than both WHERE IN and WHERE EXISTS Avg. time: 2786 ms - 499% less efficient than WHERE IN

Other Techniques & Tips

Views Saved query Great for repeatable logic Removes need to join multiple tables SLOW!

Eliminate Views Create tables that hold desired data Complete with primary key, foreign keys and indexes Create a script or procedure to query the data used in the view Truncate table and insert new data Repeat as necessary Refresh rate based on dynamic nature of data Even repopulating table every 5 minutes can be more efficient

Unions The Good: Combine results of multiple queries Allows multiple result sets to be merged into one query The Bad: Multiple unions in the same query can be inefficient The more rows returned, the slower it can be Results could take minutes

Slow Union Workaround Create a stored procedure Create a temporary table that contains all the columns returned Have each query insert its records into the table Query all the columns (not using *) Drop the temporary table

Excessive Joins & Columns The culprits: Joining many tables Returning many columns Thousands of rows The solution: Create a stored procedure Break one query into multiple queries that create temporary tables One query to join the temporary tables

Troubleshooting Tips Run directly in database Comment out sections and add logic in line by line Helps isolate which portion or line is causing issues Try creative and unique ideas Just because it doesn t seem like it would be faster or better doesn t mean it isn t

One Size Does Not Fit All A best practice may not always be best for your database Data types, table sizes, etc all play a role Try different techniques Select what works best Queries may need to be rewritten as database grows

Questions & Answers

Thank You Selene Bainum http://www.ritetech.net sbainum@ritetech.net