Mark Inman U.S. Navy (Naval Sea Logistics Center) Session #213 Analytic SQL for Beginners.

Slides:



Advertisements
Similar presentations
Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
Advertisements

Tuning Oracle SQL The Basics of Efficient SQLThe Basics of Efficient SQL Common Sense Indexing The Optimizer –Making SQL Efficient Finding Problem Queries.
Tuning: overview Rewrite SQL (Leccotech)Leccotech Create Index Redefine Main memory structures (SGA in Oracle) Change the Block Size Materialized Views,
Overview of performance tuning strategies Oracle Performance Tuning Allan Young June 2008.
INTRODUCTION TO ORACLE Lynnwood Brown System Managers LLC Performance And Tuning – Lecture 7 Copyright System Managers LLC 2007 all rights reserved.
Chapter 11 Group Functions
Introduction to Oracle9i: SQL1 SQL Group Functions.
SQL Tuning Ohio Oracle User’s Group October 2002 © Copyright, Kris T. Mason, 2002.
A few things about the Optimizer Thomas Kyte
Chapter 6 Group Functions. Chapter Objectives  Differentiate between single-row and multiple-row functions  Use the SUM and AVG functions for numeric.
Chapter 3 Single-Table Queries
Introduction to Databases Chapter 7: Data Access and Manipulation.
CERN IT Department CH-1211 Genève 23 Switzerland t Database tools for developers Jacek Wojcieszuk, IT-DM Database Developers’ Workshop July.
ADVANCE T-SQL: WINDOW FUNCTIONS Rahman Wehelie 7/16/2013 ITC 226.
Oracle 10g Database Administrator: Implementation and Administration Chapter 6 The Basics of Querying a Database.
Oracle Database Administration Lecture 6 Indexes, Optimizer, Hints.
Physical Database Design & Performance. Optimizing for Query Performance For DBs with high retrieval traffic as compared to maintenance traffic, optimizing.
Oracle Database Administration Lecture 3  Transactions  SQL Language: Additional information  SQL Language: Analytic Functions.
Oracle Database Administration Lecture 2 SQL language.
BACS--485 SQL 11 BACS 485 Structured Query Language.
The Model Clause explained Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
Materialized Views. 2 Materialized Views – Agenda What is a Materialized View? – Advantages and Disadvantages How Materialized Views Work – Parameter.
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
11-1 Improve response time of interactive programs. Improve batch throughput. To ensure scalability of applications load vs. performance. Reduce system.
SQL Performance and Optimization l SQL Overview l Performance Tuning Process l SQL-Tuning –EXPLAIN PLANs –Tuning Tools –Optimizing Table Scans –Optimizing.
BACS 287 Structured Query Language 1. BACS 287 Visual Basic Table Access Visual Basic provides 2 mechanisms to access data in tables: – Record-at-a-time.
Oracle tuning: a tutorial Saikat Chakraborty. Introduction In this session we will try to learn how to write optimized SQL statements in Oracle 8i We.
T-SQL: Simple Changes That Go a Long Way DAVE ingeniousSQL.com linkedin.com/in/ingenioussql.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
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.
SQL Aggregation Oracle and ANSI Standard SQL Lecture 9.
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.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Random Query Generator for Hive November 2015 Hive Contributor Meetup Szehon Ho.
Ad Hoc User or Application Cost-based Data Dictionary Statistics Rule-based Execution Plan Asks the question: All people and their grades in a list giving.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Optimization and Administartion of a Database Management Systems Krystian Zieja.
Copyright © 2009 Rolta International, Inc., All Rights Reserved Michael R. Messina, Management Consultant Rolta-TUSC, Oracle Open World 2009 (60 min) ID#:
Sorting data and Other selection Techniques Ordering data results Allows us to view our data in a more meaningful way. Rather than just a list of raw.
Aggregating Data Using Group Functions. What Are Group Functions? Group functions operate on sets of rows to give one result per group.
A Glance at the Window Functions. Window Functions Introduced in SQL 2005 Enhanced in SQL 2012 So-called because they operate on a defined portion of.
Simple Queries DBS301 – Week 1. Objectives Basic SELECT statement Computed columns Aliases Concatenation operator Use of DISTINCT to eliminate duplicates.
1 Ch. 11: Grouping Things Together  ANSI standard SQL Group functions: AVG, COUNT, MAX, MIN, STDDEV, SUM, VARIANCE  Others: 8i: GROUPING (used with CUBE.
1 11g NEW FEATURES ByVIJAY. 2 AGENDA  RESULT CACHE  INVISIBLE INDEXES  READ ONLY TABLES  DDL WAIT OPTION  ADDING COLUMN TO A TABLE WITH DEFAULT VALUE.
 CONACT UC:  Magnific training   
Same Plan Different Performance Mauro Pagano. Consultant/Developer/Analyst Oracle  Enkitec  Accenture DBPerf and SQL Tuning Training Tools (SQLT, SQLd360,
LAB: Web-scale Data Management on a Cloud Lab 11. Query Execution Plan 2011/05/27.
IFS180 Intro. to Data Management Chapter 10 - Unions.
EXPLAIN and AUTOTRACE.
SQL in Oracle.
T-SQL: Simple Changes That Go a Long Way
T-SQL: Simple Changes That Go a Long Way
I WANT TO HOLD YOUR HAND 1ST TOP 100 SINGLE
Lecture#7: Fun with SQL (Part 2)
(SQL) Aggregating Data Using Group Functions
Working on the Chain Gang
Oracle8i Analytical SQL Features
SQL – Entire Select.
Chapter 4 Summary Query.
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Data Analysis with SQL Window Functions
Reporting Aggregated Data Using the Group Functions
Contents Preface I Introduction Lesson Objectives I-2
Reporting Aggregated Data Using the Group Functions
A – Pre Join Indexes.
Reporting Aggregated Data Using the Group Functions
T-SQL: Simple Changes That Go a Long Way
Presentation transcript:

Mark Inman U.S. Navy (Naval Sea Logistics Center) Session #213 Analytic SQL for Beginners

Speaker Qualifications Mark Inman – IT Specialist – U.S. Navy Oracle Certified Professional Database Administrator 9i Presented a similar presentation to coworkers.

Background Analytic SQL was introduced in Oracle 8i. Where in Oracle Documentation? –Data Warehousing Guide “SQL for Analysis” or “SQL for Analysis and Reporting”. good for everyday use

Analytic Syntax Column List Only Form Of –Function –Partition Clause –Order by Clause –Windowing Clause

Analytic Universe Non- Analytic Available PARTITION BY ORDER BY WINDOWING Ranking Windowing Aggregate Reporting Aggregate RATIO_TO_REPORT LAG/LEAD FIRST/LAST Linear Regression Inverse Percentile

Objective 1 To get aggregate and detail data in the same query – without selecting the same table twice.

Simple Example Analytic select object_id, owner, max(object_id) over () as max_object_id /* ANALYTIC EXPRESSION */ from thing where rownum <= 5; OBJECT_ID OWNER MAX_OBJECT_ID SYS SYS SYS SYS SYS 44

Simple Example Analytic Query Plan Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 WINDOW (BUFFER) 2 1 COUNT (STOPKEY) 3 2 TABLE ACCESS (FULL) OF 'THING' (TABLE)

Duplicate Non-Analytic Attempt 1 select object_id, owner, max(object_id) from thing where rownum <= 5 group by object_id;, owner * ERROR at line 3: ORA-00979: not a GROUP BY expression Oops!

Duplicate Non-Analytic Attempt 2 select object_id, owner, max(object_id) max_object_id from thing where rownum <= 5 group by object_id, owner; OBJECT_ID OWNER MAX_OBJECT_ID SYS SYS SYS SYS SYS 44

Duplicate Non-Analytic Success in 3 select object_id, owner, z.max_object_id from thing, ( select max(object_id) max_object_id from thing where rownum <= 5 ) z where rownum <= 5; OBJECT_ID OWNER MAX_OBJECT_ID SYS SYS SYS SYS SYS 44

Non-Analytic Query Plan Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 COUNT (STOPKEY) 2 1 NESTED LOOPS 3 2 VIEW 4 3 SORT (AGGREGATE) 5 4 COUNT (STOPKEY) 6 5 TABLE ACCESS (FULL) OF 'THING' (TABLE) 7 2 TABLE ACCESS (FULL) OF 'THING' (TABLE) Two table scans – but so what – it is fast!

SET AUTOTRACE Statistics recursive calls db block gets consistent gets physical reads redo size bytes sent via SQL*Net to client bytes received via SQL*Net from client SQL*Net roundtrips to/from client sorts (memory) sorts (disk) rows processed

SET AUTOTRACE Emphasis on … –db block gets (current tkprof) –consistent gets (query tkprof) –sorts (memory) –table scans from “Execution Plan” No emphasis on … –physical reads –elapsed time (SET TIMING ON)

Statistics StatAnalyticNon-Analytic recursive calls 1 0 db block gets 0 0 consistent gets 4 9 physical reads 0 0 redo size 0 0 bytes sent …605 bytes received …508 SQL*Net roundtrips 2 2 sorts (memory) 1 0 sorts (disk) 0 0

SET AUTOTRACE Options –traceonly (no rows) –statistics –explain SQL*Plus command PLUSTRACE role required – not a default Documentation –SQL*Plus® User's Guide and Reference –Effective Oracle by Design by Thomas Kyte

Statistics - Scaling Non- Analytic Buffer Gets Analytic Memory Sorts Analytic Buffer Gets Analytic Memory Sorts all

Statistics - Scaling

Objective 1 - To get aggregate and detail data in the same query – without selecting the same table twice. Better Performance Scales Better Smaller Query (Less Lines of Code)

Objective 1 - To get aggregate and detail data in the same query – without selecting the same table twice. MAX OVER () Reporting Aggregate Function {SUM | AVG | MAX | MIN | COUNT | STDDEV | VARIANCE... } ([ALL | DISTINCT] {value expression1 | *}) OVER ([PARTITION BY value expression2[,...]])

Objective 2 To compare traditional ranking and analytic ranking and show why analytic ranking is better.

Non-Analytic Top-1 Query select owner, object_name, object_type, last_ddl_time, rownum from ( select * from minman_dba.thing ORDER BY LAST_DDL_TIME ASC NULLS FIRST ) where rownum = 1;

Analytic Top-1 Query select owner, object_name, subobject_name, object_type, MY_ROWNUM from ( select x.*, ROW_NUMBER() OVER ( ORDER BY LAST_DDL_TIME ASC NULLS FIRST ) AS MY_ROWNUM from minman_dba.thing x ) where MY_ROWNUM = 1;

Top-1 Queries select owner, object_name, subobject_name, object_type, MY_ROWNUM from ( select x.*, ROW_NUMBER() OVER ( ORDER BY LAST_DDL_TIME ASC NULLS FIRST ) AS MY_ROWNUM from minman_dba.thing x ) where MY_ROWNUM = 1; select owner, object_name, object_type, last_ddl_time, ROWNUM from ( select * from minman_dba.thing ORDER BY LAST_DDL_TIME ASC NULLS FIRST ) where ROWNUM = 1; SQL Keyword Alias in Column List

Top-1 Queries - Plans Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 COUNT (STOPKEY) 2 1 VIEW 3 2 SORT (ORDER BY STOPKEY) 4 3 TABLE ACCESS (FULL) OF 'THING' (TABLE) Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 VIEW 2 1 WINDOW (SORT PUSHED RANK) 3 2 TABLE ACCESS (FULL) OF 'THING' (TABLE)

Top-1 Queries - Statistics Statistics db block gets 642 consistent gets 0 physical reads 1 sorts (memory) 0 sorts (disk) Statistics db block gets 642 consistent gets 0 physical reads 1 sorts (memory) 0 sorts (disk)

Top-1 Queries – Two Object Types – Non-Analytic select owner, object_name, object_type, last_ddl_time, rownum from ( select * from minman_dba.thing where object_type = 'TABLE' order by last_ddl_time ) where rownum = 1 union all … select owner, object_name, object_type, last_ddl_time, rownum from ( select * from minman_dba.thing where object_type = 'PROCEDURE' order by last_ddl_time ) where rownum = 1

Top-1 Queries – Two Object Types – Non-Analytic OWNER OBJECT_NAME OBJECT_TYP LAST_DDL_ ROWNUM SYS UNDO$ TABLE 03-FEB-06 1 SYS PSTUBT PROCEDURE 03-FEB-06 1

Top-1 Queries – Two Object Types – Non-Analytic select y.owner, y.object_name, y.object_type, y.last_ddl_time from ( select object_type, min(last_ddl_time) min_last_ddl_time from minman_dba.thing where object_type in ('TABLE','PROCEDURE') group by object_type ) x inner join minman_dba.thing y on x.min_last_ddl_time = y.last_ddl_time; 30 rows selected

Top-1 Queries – Two Object Types - Analytic select owner, object_name, object_type, last_ddl_time, my_rownum from ( select t.*, row_number() over ( PARTITION BY OBJECT_TYPE order by last_ddl_time ) my_rownum from minman_dba.thing t WHERE OBJECT_TYPE IN ('TABLE','PROCEDURE') ) where my_rownum = 1

Top-1 Queries – Two Object Types - Analytic OWNER OBJECT_NAME OBJECT_TYP LAST_DDL_ ROWNUM SYS UNDO$ TABLE 03-FEB-06 1 SYS PSTUBT PROCEDURE 03-FEB-06 1

Top-1 Queries – Two Object Types – Query Plans Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 UNION-ALL 2 1 COUNT (STOPKEY) 3 2 VIEW 4 3 SORT (ORDER BY STOPKEY) 5 4 TABLE ACCESS (FULL) OF 'THING' (TABLE) 6 1 COUNT (STOPKEY) 7 6 VIEW 8 7 SORT (ORDER BY STOPKEY) 9 8 TABLE ACCESS (FULL) OF 'THING' (TABLE) Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 VIEW 2 1 WINDOW (SORT PUSHED RANK) 3 2 TABLE ACCESS (FULL) OF 'THING' (TABLE)

Top-1 Queries – Two Object Types – Statistics Statistics db block gets 1284 consistent gets 0 physical reads 2 sorts (memory) 0 sorts (disk) Statistics db block gets 642 consistent gets 0 physical reads 1 sorts (memory) 0 sorts (disk)

Top-1 Queries – All Object Types - Analytic select owner, object_name, object_type, last_ddl_time, my_rownum from ( select t.*, row_number() over ( PARTITION BY OBJECT_TYPE order by last_ddl_time ) my_rownum from minman_dba.thing t ) where my_rownum = 1

Top-1 Queries – Two Object Types - Analytic OWNER OBJECT_NAME OBJECT_TYPE LAST_DDL_ MY_ROWNUM SYS C_OBJ# CLUSTER 03-FEB-06 1 SYS LOW_GROUP CONSUMER GROUP 03-FEB-06 1 SYS REGISTRY$CTX CONTEXT 03-FEB-06 1 SH CUSTOMERS_DIM DIMENSION 25-FEB-07 1 SYS DATA_FILE_DIR DIRECTORY 25-FEB-07 1 SYS AQ$_SCHEDULER$_JOBQTAB_V EVALUATION CONTEXT 03-FEB-06 1 SYS GETTVOID FUNCTION 03-FEB-06 1 SYS I_OBJ# INDEX 03-FEB-06 1 SYSTEM LOGMNRC_GTCS_PK INDEX PARTITION 03-FEB-06 1 EXFSYS EXPFILTER INDEXTYPE 03-FEB-06 1 SYS /cc11c9d8_SerialVerFrame JAVA CLASS 03-FEB-06 1 … we are not showing the full result

RANK and DENSE_RANK select owner, object_name, object_type, last_ddl_time, rn, r, dr from ( select t.*, row_number() over (partition by object_type order by last_ddl_time) RN, rank() over (partition by object_type order by last_ddl_time) R, dense_rank() over (partition by object_type order by last_ddl_time) DR from minman_dba.thing t where object_Type in ('PROCEDURE') ) where rn between 1 and 10;

RANK and DENSE_RANK ownerobject_namelast ddl timeRNRDR SYSPSTUBT SYSPSTUB SYSSUBPTXT SYSSUBPTXT SYSODCIINDEXINFOFLAGSDUMP SYSODCIINDEXINFODUMP SYSODCIPREDINFODUMP SYSODCIQUERYINFODUMP SYSODCICOLINFODUMP SYSODCISTATSOPTIONSDUMP

Objective 2- To compare traditional ranking and analytic ranking and show why analytic ranking is better. Better Performance Scales Better Smaller Query (Less Lines of Code) PARTITION BY

Objective 2- To compare traditional ranking and analytic ranking and show why analytic ranking is better. ROW_NUMBER ( ) OVER ( [query_partition_clause] order_by_clause ) RANK ( ) OVER ( [query_partition_clause] order_by_clause ) DENSE_RANK ( ) OVER ( [query_partition_clause] order_by_clause )

Objective 3 - To show additional flexibility of analytic expressions. create table another_thing ( first_col char(1), second_col number ) / insert into another_thing values ('A', ); insert into another_thing values ('A', ); insert into another_thing values ('A', ); insert into another_thing values ('A', ); insert into another_thing values ('B', ); insert into another_thing values ('B',34231); insert into another_thing values ('B', );

Additional Flexibility select first_col, second_col, row_number() over (partition by first_col order by second_col asc) my_1st_rownum from another_thing; F SECOND_COL MY_1ST_ROWNUM A E+10 1 A E+10 2 A E+14 3 A E+14 4 B B B E+14 3

Additional Flexibility select first_col, second_col, row_number() over (partition by first_col order by second_col asc) my_1st_rownum, ROW_NUMBER() OVER (PARTITION BY FIRST_COL ORDER BY SECOND_COL DESC) MY_2ND_ROWNUM from another_thing; F SECOND_COL MY_1ST_ROWNUM MY_2ND_ROWNUM A E A E A E A E B B B E

Additional Flexibility select first_col, second_col, row_number() over (partition by first_col order by second_col) my_1st_rownum, row_number() over (partition by first_col order by second_col desc) my_2nd_rownum, ROW_NUMBER() OVER ( PARTITION BY FIRST_COL ORDER BY MOD(SECOND_COL,10) ASC NULLS FIRST ) MY_3RD_ROWNUM from another_thing;

Additional Flexibility first col second colmy 1 st rownum my 2 nd rownum my 3 rd rownum A3.4897E A5.7865E A1.7823E A3.2486E B B B4.3330E+14312

Additional Flexibility – Query Plan Execution Plan SELECT STATEMENT Optimizer=CHOOSE 1 0 WINDOW (SORT) 2 1 WINDOW (SORT) 3 2 WINDOW (SORT) 4 3 TABLE ACCESS (FULL) OF 'ANOTHER_THING'

Additional Flexibility select first_col, second_col, row_number() over (partition by first_col order by second_col) my_1st_rownum, row_number() over (partition by first_col order by second_col desc) my_2nd_rownum, row_number() over ( partition by first_col order by mod(second_col,10) asc nulls first ) my_3rd_rownum from another_thing order by second_col;

Additional Flexibility first col second colmy 1 st rownum my 2 nd rownum my 3 rd rownum B B A3.4897E A5.7865E A1.7823E A3.2486E B4.3330E+14312

Additional Flexibility – Query Plan Execution Plan SELECT STATEMENT Optimizer=ALL_ROWS 1 0 SORT (ORDER BY) 2 1 WINDOW (SORT) 3 2 WINDOW (SORT) 4 3 WINDOW (SORT) 5 4 TABLE ACCESS (FULL) OF 'ANOTHER_THING' (TABLE)

Objective 3 - To show additional flexibility of analytic expressions. Better Performance Scales Better Smaller Query (Less Lines of Code) PARTITION BY Multiple ORDER BY – Single Select Multiple PARTITION – Single Select

Items Learned in this Session To get aggregate and detail data in the same query – without selecting the same table twice. To compare traditional ranking and analytic ranking and show why analytic ranking is better. To show additional flexibility of analytic expressions.

Questions?

Thank You Please fill out the evaluation. Speaker: Mark Inman Session Name: Analytic SQL for Beginners Session Number: 213 Mark Inman