Www.regoconsulting.comPhone: 1-888-813-0444 Creating and Tuning SQL Queries that Engage Users Writing Efficient SQL.

Slides:



Advertisements
Similar presentations
Welcome to CODE SPREAD Simple Concepts of Coding | Programming.
Advertisements

Advanced SQL (part 1) CS263 Lecture 7.
© Abdou Illia MIS Spring 2014
Working with Tables 1 of 5. Working with Tables 2 of 5.
A Guide to SQL, Seventh Edition. Objectives Use joins to retrieve data from more than one table Use the IN and EXISTS operators to query multiple tables.
Relational Algebra, Join and QBE Yong Choi School of Business CSUB, Bakersfield.
Clarity Educational Community Portlet Creation and Management Getting Started with NSQL Presented by: Juan Ortega, David Zywiec.
Getting Started with Data Model
Introduction to SQL Session 2 Retrieving Data From Multiple Tables.
Clarity Educational Community Clarity Educational Community Creating and Tuning SQL Queries that Engage Users.
XP New Perspectives on Microsoft Office Excel 2003, Second Edition- Tutorial 5 1 Microsoft Office Excel 2003 Tutorial 5 – Working With Excel Lists.
Mgt 240 Lecture MS Excel and Access: Introduction to Databases September 23, 2004.
Chapter 3: Using SQL Queries to Insert, Update, Delete, and View Data
Database Systems More SQL Database Design -- More SQL1.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
Advanced SQL SMSU Computer Services Short Course.
Microsoft Access 2010 Chapter 7 Using SQL.
WRITING BASIC SQL SELECT STATEMENTS Lecture 7 1. Outlines  SQL SELECT statement  Capabilities of SELECT statements  Basic SELECT statement  Selecting.
XP 1 Microsoft Office Excel 2003 Tutorial 3 – Working With Excel Lists.
Introduction to SQL Structured Query Language Martin Egerhill.
Using SQL Queries to Insert, Update, Delete, and View Data Date Retrieval from a single table & Calculations © Abdou Illia MIS Spring 2015.
Chapter 3 Single-Table Queries
Analyzing Data For Effective Decision Making Chapter 3.
Improving Efficiency of I/O Bound Systems More Memory, Better Caching Newer and Faster Disk Drives Set Object Access (SETOBJACC) Reorganize (RGZPFM) w/
1 Agenda – 03/25/2014 Login to SQL Server 2012 Management Studio. Answer questions about HW#7 – display answers. Exam is 4/1/2014. It will be in the lab.
PHP and MySQL CS How Web Site Architectures Work  User’s browser sends HTTP request.  The request may be a form where the action is to call PHP.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
 Agenda 2/20/13 o Review quiz, answer questions o Review database design exercises from 2/13 o Create relationships through “Lookup tables” o Discuss.
1 Agenda – 10/24/2013 Answer questions from lab on 10/22. Present SQL View database object. Present SQL UNION statement.
Robin Mullinix Systems Analyst GeorgiaFIRST Financials PeopleSoft Query: The Next Step.
SQL Performance and Optimization l SQL Overview l Performance Tuning Process l SQL-Tuning –EXPLAIN PLANs –Tuning Tools –Optimizing Table Scans –Optimizing.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Microsoft Office XP Illustrated Introductory, Enhanced Tables and Queries Using.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Views. Objectives Create views Modify/Drop Views Insert/Delete/Update to/from views Retrieve data from views.
Module 4 Database SQL Tuning Section 3 Application Performance.
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
Indexes and Views Unit 7.
Chapter 13 Views Oracle 10g: SQL. Oracle 10g: SQL2 Objectives Create a view, using CREATE VIEW command or the CREATE OR REPLACE VIEW command Employ the.
1 Theory, Practice & Methodology of Relational Database Design and Programming Copyright © Ellis Cohen Subqueries These slides are licensed under.
1/18/00CSE 711 data mining1 What is SQL? Query language for structural databases (esp. RDB) Structured Query Language Originated from Sequel 2 by Chamberlin.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
1 Chapter 9 Tuning Table Access. 2 Overview Improve performance of access to single table Explain access methods – Full Table Scan – Index – Partition-level.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Manipulating Data Lesson 3. Objectives Queries The SELECT query to retrieve or extract data from one table, how to retrieve or extract data by using.
A Guide to MySQL 6. 2 Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT command.
1 Chapter 3 Single Table Queries. 2 Simple Queries Query - a question represented in a way that the DBMS can understand Basic format SELECT-FROM Optional.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
There’s a particular style to it… Rob Hatton
 CONACT UC:  Magnific training   
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
1 Agenda TMA02 M876 Block 4. 2 Model of database development data requirements conceptual data model logical schema schema and database establishing requirements.
IFS180 Intro. to Data Management Chapter 10 - Unions.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
SQL Query Getting to the data ……..
More SQL: Complex Queries,
MySQL Subquery Source: Dev.MySql.com
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
ITEC 313 Database Programming
Writing Basic SQL SELECT Statements
SQL – Entire Select.
Chapter 4 Summary Query.
Access: SQL Participation Project
SQL Fundamentals in Three Hours
Contents Preface I Introduction Lesson Objectives I-2
Topic 12 Lesson 2 – Retrieving Data with Queries
Presentation transcript:

Creating and Tuning SQL Queries that Engage Users Writing Efficient SQL

● Develop queries that perform well ● Develop queries that scale well ● Develop queries that are concise and easy to debug ● Develop queries that use the data model efficiently Goal

● Using table aliases ● Eliminate unnecessary joins ● Sub-queries ● Starting with most restrictive criteria ● IN vs EXISTS ● DISTICT vs EXISTS ● OBS Filtering ● UNION queries ● Inline views General tips

● Improves readability of sql ● Use meaningful table aliases SELECT A.ID FROM SRM_RESOURCES A JOIN ODF_CA_RESOURCE B ON A.ID = B.ID ● Allows queries to be modified more easily SELECT ID FROM SRM_RESOURCES JOIN ODF_CA_RESOURCE ON SRM_RESOURCES.ID = ODF_CA_RESOURCE.ID ● Help performance by eliminating the need for the database to search the tables for the referenced column Table Aliases

● Remove unnecessary tables from queries ● Avoid falling back on common tables ○ INV_INVESTMENTS ○ SRM_RESOURCES ● Query to find role allocation hours on a project SELECT SUM(TM.PRALLOCSUM / 3600) HOURS FROM INV_INVESTMENTS INVI JOIN PRTEAM TM ON INVI.ID = TM.PRPROJECTID JOIN SRM_RESOURCES SRMR ON TM.PRRESOURCEID = SRMR.ID JOIN PRJ_RESOURCES PRJR ON SRMR.ID = PRJR.PRID WHERE INVI.ID = AND PRJR.PRISROLE = 1 Unnecessary joins

● Sub-queries can be handy ● How many projects is a resource assigned to SELECT SRMR.FULL_NAME, (SELECT COUNT(*) FROM PRTEAM TM WHERE TM.PRRESOURCEID = SRMR.ID) TEAM_COUNT FROM SRM_RESOURCES SRMR ORDER BY SRMR.FULL_NAME ● Instead of a LEFT JOIN to a large inline view SELECT SRMR.FULL_NAME, TM.TEAM_COUNT FROM SRM_RESOURCES SRMR LEFT JOIN (SELECT TM.PRRESOURCEID, COUNT(*) TEAM_COUNT FROM PRTEAM TM GROUP BY TM.PRRESOURCEID) TM ON SRMR.ID = TM.PRRESOURCEID ORDER BY SRMR.FULL_NAME ● Avoid substituting sub-queries for table joins SELECT SRMR.FULL_NAME, (SELECT COUNT(*) FROM PRTEAM TM WHERE TM.PRRESOURCEID = SRMR.ID) TEAM_COUNT, (SELECT SUM(TM.PRALLOCSUM) / 3600 FROM PRTEAM TM WHERE TM.PRRESOURCEID = SRMR.ID) TOTAL_ALLOC, (SELECT SUM(TM.HARD_SUM) / 3600 FROM PRTEAM TM WHERE TM.PRRESOURCEID = SRMR.ID) TOTAL_HALLOC FROM SRM_RESOURCES SRMR ORDER BY SRMR.FULL_NAME Sub-queries

● Begin queries with the most restrictive conditions SELECT SRMR.FULL_NAME, AV.SLICE_DATE, AV.SLICE FROM SRM_RESOURCES SRMR JOIN PRJ_BLB_SLICES AV ON SRMR.ID = AV.PRJ_OBJECT_ID AND AV.SLICE_REQUEST_ID = 7 WHERE SRMR.ID = 1 ORDER BY SRMR.FULL_NAME, AV.SLICE_DATE ● Instead of SELECT SRMR.FULL_NAME, AV.SLICE_DATE, AV.SLICE FROM PRJ_BLB_SLICES AV JOIN SRM_RESOURCES SRMR ON AV.PRJ_OBJECT_ID = SRMR.ID WHERE AV.SLICE_REQUEST_ID = 7 AND SRMR.ID = 1 ORDER BY SRMR.FULL_NAME, AV.SLICE_DATE Restrictive conditions

● IN is typically better when the inner query contains a small result set SELECT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR WHERE SRMR.ID IN (SELECT TM.PRRESOURCEID FROM PRTEAM TM WHERE TM.PRPROJECTID IN ( , )) SELECT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR WHERE EXISTS (SELECT 1 FROM PRTEAM TM WHERE TM.PRPROJECTID IN ( , ) AND TM.PRRESOURCEID = SRMR.ID) ● EXISTS is typically better when the inner query contains a large result set SELECT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR WHERE SRMR.ID IN (SELECT TM.PRRESOURCEID FROM PRTEAM TM) SELECT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR WHERE EXISTS (SELECT 1 FROM PRTEAM TM WHERE TM.PRRESOURCEID = SRMR.ID) IN vs. EXISTS

● Both methods are commonly used to derive unique values for dimension keys of portlets ● EXISTS is preferable to DISTINCT ● DISTINCT produces the entire result set (including duplicates), sorts, and then filters out duplicates SELECT DISTINCT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR JOIN PRTEAM TM ON SRMR.ID = TM.PRRESOURCEID ● EXISTS proceeds with fetching rows immediately after the sub-query condition has been satisfied the first time SELECT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR WHERE EXISTS (SELECT 1 FROM PRTEAM TM WHERE TM.PRRESOURCEID = SRMR.ID) DISTINCT vs. EXISTS

● Seen many ways to filter based on OBS ● Many rely on complex logic, left joins to inline views, or multiple sub-queries ● Using EXISTS and the OBS_UNITS_FLAT_BY_MODE table provides an easy solution ● Filter by Unit Only, Unit and Descendants, or Units and Ancestors SELECT SRMR.FULL_NAME FROM SRM_RESOURCES SRMR WHERE (:OBS_ID IS NULL OR EXISTS (SELECT 1 FROM OBS_UNITS_FLAT_BY_MODE OBSM JOIN PRJ_OBS_ASSOCIATIONS OBSA ON OBSM.LINKED_UNIT_ID = OBSA.UNIT_ID AND OBSA.TABLE_NAME = 'SRM_RESOURCES' WHERE OBSM.UNIT_ID = :OBS_ID AND OBSM.UNIT_MODE = NVL(:OBS_MODE, 'OBS_UNIT_AND_CHILDREN') AND OBSA.RECORD_ID = SRMR.ID)) OBS Filtering

● UNION queries perform poorly as they scan through the same data multiple times ● Require any logic changes to be made in multiple locations SELECT CODE, NAME, SUM(FORECAST_COST) FORECAST_COST, SUM(BUDGET_COST) BUDGET_COST FROM (SELECT INVI.CODE, INVI.NAME, FP.TOTAL_COST FORECAST_COST, 0 BUDGET_COST FROM INV_INVESTMENTS INVI JOIN FIN_PLANS FP ON INVI.ID = FP.OBJECT_ID AND INVI.ODF_OBJECT_CODE = FP.OBJECT_CODE WHERE FP.IS_PLAN_OF_RECORD = 1 AND FP.PLAN_TYPE_CODE = 'FORECAST' UNION ALL SELECT INVI.CODE, INVI.NAME, 0 FORECAST_COST, FP.TOTAL_COST BUDGET_COST FROM INV_INVESTMENTS INVI JOIN FIN_PLANS FP ON INVI.ID = FP.OBJECT_ID AND INVI.ODF_OBJECT_CODE = FP.OBJECT_CODE WHERE FP.IS_PLAN_OF_RECORD = 1 AND FP.PLAN_TYPE_CODE = 'BUDGET') WHERE INVI.CODE = 'PROJ01763' GROUP BY CODE, NAM ● Most UNION queries can easily be replaced with logic SELECT INVI.CODE, INVI.NAME, SUM(CASE WHEN FP.PLAN_TYPE_CODE = 'FORECAST' THEN FP.TOTAL_COST END) FORECAST_COST, SUM(CASE WHEN FP.PLAN_TYPE_CODE = 'BUDGET' THEN FP.TOTAL_COST END) BUDGET_COST FROM INV_INVESTMENTS INVI JOIN FIN_PLANS FP ON INVI.ID = FP.OBJECT_ID AND INVI.ODF_OBJECT_CODE = FP.OBJECT_CODE WHERE FP.IS_PLAN_OF_RECORD = 1 AND INVI.CODE = 'PROJ01763' GROUP BY INVI.CODE, INVI.NAME ● Only use UNION when joining data from multiple tables UNION queries

● Inline views can be very beneficial but can severely affect performance ● LEFT JOINs to large inline views is typically not a good idea SELECT SRMR.FULL_NAME, SUM(AV.SLICE) AVAIL, AL.ALLOC FROM SRM_RESOURCES SRMR JOIN PRJ_BLB_SLICES AV ON SRMR.ID = AV.PRJ_OBJECT_ID AND AV.SLICE_REQUEST_ID = 7 LEFT JOIN (SELECT TM.PRRESOURCEID, SUM(AL.SLICE) ALLOC FROM PRTEAM TM JOIN PRJ_BLB_SLICES AL ON TM.PRID = AL.PRJ_OBJECT_ID WHERE AL.SLICE_REQUEST_ID = 6 AND AL.SLICE_DATE BETWEEN '01-JAN-14' AND '30-JUN-14' GROUP BY TM.PRRESOURCEID) AL ON SRMR.ID = AL.PRRESOURCEID WHERE AV.SLICE_DATE BETWEEN '01-JAN-14' AND '30-JUN-14' GROUP BY SRMR.FULL_NAME, AL.ALLOC ORDER BY SRMR.FULL_NAME ● Will work through some examples to demonstrate alternatives Inline views

● Resource ETC and Actuals by month ● Eliminating large LEFT JOINs ● Eliminating an unnecessary join ● Using EXISTS ● Summarizing data ● Running totals ● Selecting latest status report ● Concatenating multi-valued lookup values into a string Interactive Examples

Questions Contact US Contact Web Site