Q1 = SELECT [Project9].[ID] AS [ID],[Project9].[C1] AS [C1],[Project9].[C2] AS [C2],[Project9].[ID1] AS [ID1],[Project9].[SalesOrderID] AS [SalesOrderID],

Slides:



Advertisements
Similar presentations
Advanced SQL Topics Edward Wu.
Advertisements

10 Copyright © 2005, Oracle. All rights reserved. Dimensions.
12 Copyright © 2005, Oracle. All rights reserved. Query Rewrite.
SQL: The Query Language Part 2
Haas MFE SAS Workshop Lecture 3:
Advanced SQL (part 1) CS263 Lecture 7.
Introduction to SQL Tuning Brown Bag Three essential concepts.
SQL Tuning Briefing Null is not equal to null but null is null.
SearchSpaceAnalyzer Anisoara Nica, Daniel S. Brotherston, David W. Hillis Extreme Visualization of Query Optimizer Search Spaces, ACM SIGMOD International.
CS 3630 Database Design and Implementation. Where Clause and Aggregate Functions -- List all rooms whose price is greater than the -- average room price.
© Abdou Illia MIS Spring 2014
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Joins and Sub-queries in SQL.
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.
Chapter 7 © 2013 Pearson Education, Inc. Publishing as Prentice Hall 1 Modern Database Management 11 th Edition Jeffrey A. Hoffer, V. Ramesh, Heikki Topi.
Displaying Data from Multiple Tables
ACM CIKM 2008, Oct , Napa Valley 1 Mining Term Association Patterns from Search Logs for Effective Query Reformulation Xuanhui Wang and ChengXiang.
Glenn Paulley, Director, Engineering Panel: Grand Challenges in Testing Data- intensive Computing Systems.
Query Optimization Reserves Sailors sid=sid bid=100 rating > 5 sname (Simple Nested Loops) Imperative query execution plan: SELECT S.sname FROM Reserves.
View (virtual table). View A VIEW is a virtual table A view contains rows and columns, just like a real table. The fields in a view are fields from one.
1 Copyright © 2011, Oracle and/or its affiliates. All rights reserved.
Query Optimization CS634 Lecture 12, Mar 12, 2014 Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Introduction to Oracle9i: SQL1 Subqueries. Introduction to Oracle9i: SQL2 Chapter Objectives Determine when it is appropriate to use a subquery Identify.
CSEN 5314 Quiz What type of join is needed when you wish to include rows that do not have matching values? A. Equi-joinB. Natural join C. Outer.
March 2010ACS-4904 Ron McFadyen1 Indexes B-tree index Bitmapped index Bitmapped join index A data warehousing DBMS will likely provide these, or variations,
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 8 Advanced SQL.
A Guide to SQL, Seventh Edition. Objectives Retrieve data from a database using SQL commands Use compound conditions Use computed columns Use the SQL.
02 | Advanced SELECT Statements Brian Alderman | MCT, CEO / Founder of MicroTechPoint Tobias Ternstrom | Microsoft SQL Server Program Manager.
Query Optimization, part 2 CS634 Lecture 13, Mar Slides based on “Database Management Systems” 3 rd ed, Ramakrishnan and Gehrke.
Chapter 3 Single-Table Queries
Access Path Selection in a Relational Database Management System Selinger et al.
The Model Clause explained Tony Hasler, UKOUG Birmingham 2012 Tony Hasler, Anvil Computer Services Ltd.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
Massive Stochastic Testing of SQL Don Slutz Microsoft Research Presented By Manan Shah.
Database Development Tr ươ ng Quý Quỳnh. References UDEMY: SQL Database MasterClass: Go From Pupil To Master! Database Systems - A Practical Approach.
CpSc 3220 The Language of SQL The Language of SQL Chapters
M Taimoor Khan Course Objectives 1) Basic Concepts 2) Tools 3) Database architecture and design 4) Flow of data (DFDs)
From Relational Algebra to SQL CS 157B Enrique Tang.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Programming in R SQL in R. Running SQL in R In this session I will show you how to: Run basic SQL commands within R.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
SQL: Sub-queries Single-value sub-queries Single-column sub-queries Sub-queries that produce tables Correlated sub-queries D. Christozov / G.Tuparov INF.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Query Processing – Implementing Set Operations and Joins Chap. 19.
Lecture 15: Query Optimization. Very Big Picture Usually, there are many possible query execution plans. The optimizer is trying to chose a good one.
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.
1 Indexes ► Sort data logically to improve the speed of searching and sorting operations. ► Provide rapid retrieval of specified rows from the table without.
Chapter 7 Subqueries. Chapter Objectives  Determine when it is appropriate to use a subquery  Identify which clauses can contain subqueries  Distinguish.
CS 440 Database Management Systems Stored procedures & OR mapping 1.
1 Conference Highlights More than 700 technical sessions More than 110 hands-on labs Industry-focused business and IT leadership sessions Approximately.
Copyright © 2016 Pearson Education, Inc. CHAPTER 7: ADVANCED SQL (PART I) Modern Database Management 12 th Edition Jeff Hoffer, Ramesh Venkataraman, Heikki.
CSC314 DAY 9 Intermediate SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall USING AND DEFINING VIEWS  Views provide users controlled.
Concepts of Database Management, Fifth Edition Chapter 3: The Relational Model 2: SQL.
Query Optimization. overview Application Programmer (e.g., business analyst, Data architect) Sophisticated Application Programmer (e.g., SAP admin) DBA,
IFS180 Intro. to Data Management Chapter 10 - Unions.
02 | Advanced SELECT Statements
Subqueries.
Subqueries Schedule: Timing Topic 25 minutes Lecture
Data Engineering Query Optimization (Cost-based optimization)
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Lecture 3 Finishing SQL
Relational Database Design
Subqueries Schedule: Timing Topic 25 minutes Lecture
CS222P: Principles of Data Management Notes #13 Set operations, Aggregation, Query Plans Instructor: Chen Li.
Using Subqueries to Solve Queries
Using Subqueries to Solve Queries
Subqueries Schedule: Timing Topic 25 minutes Lecture
Subqueries Schedule: Timing Topic 25 minutes Lecture
Query Transformations
Presentation transcript:

Q1 = SELECT [Project9].[ID] AS [ID],[Project9].[C1] AS [C1],[Project9].[C2] AS [C2],[Project9].[ID1] AS [ID1],[Project9].[SalesOrderID] AS [SalesOrderID], [Project9].[TotalDue] AS [TotalDue] FROM ( SELECT [Distinct1].[ID] AS [ID], 1 AS [C1], [Project8].[ID] AS [ID1], [Project8].[SalesOrderID] AS [SalesOrderID], [Project8].[TotalDue] AS [TotalDue], [Project8].[C1] AS [C2] FROM (SELECT DISTINCT [Extent1].[ID] AS [ID] (SELECT DISTINCT [Extent1].[ID] AS [ID] FROM [DBA].[Person] AS [Extent1] FROM [DBA].[Person] AS [Extent1] INNER JOIN [DBA].[Sales] AS [Extent2] INNER JOIN [DBA].[Sales] AS [Extent2] ON EXISTS (SELECT cast(1 as bit) AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] LEFT OUTER JOIN (SELECT [Extent3].[ID] AS [ID] LEFT OUTER JOIN (SELECT [Extent3].[ID] AS [ID] FROM [DBA].[Person] AS [Extent3] WHERE [Extent2].[ID] = [Extent3].[ID] )AS [Project1] ON cast(1 as bit) = cast(1 as bit) FROM [DBA].[Person] AS [Extent3] WHERE [Extent2].[ID] = [Extent3].[ID] )AS [Project1] ON cast(1 as bit) = cast(1 as bit) LEFT OUTER JOIN (SELECT [Extent4].[ID] AS [ID] LEFT OUTER JOIN (SELECT [Extent4].[ID] AS [ID] FROM [DBA].[Person] AS [Extent4] WHERE [Extent2].[ID] = [Extent4].[ID] ) AS [Project2] ON cast(1 as bit) = cast(1 as bit) FROM [DBA].[Person] AS [Extent4] WHERE [Extent2].[ID] = [Extent4].[ID] ) AS [Project2] ON cast(1 as bit) = cast(1 as bit) WHERE ([Extent1].[ID] = [Project1].[ID]) OR (([Extent1].[ID] IS NULL) AND ([Project2].[ID] IS NULL)) ) WHERE ([Extent1].[ID] = [Project1].[ID]) OR (([Extent1].[ID] IS NULL) AND ([Project2].[ID] IS NULL)) ) ) AS [Distinct1] LEFT OUTER JOIN (SELECT [Extent5].[ID] AS [ID], [Extent6].[SalesOrderID] AS [SalesOrderID], [Extent6].[TotalDue] AS [TotalDue], 1 AS [C1] FROM [DBA].[Person] AS [Extent5] FROM [DBA].[Person] AS [Extent5] INNER JOIN [DBA].[Sales] AS [Extent6] INNER JOIN [DBA].[Sales] AS [Extent6] ON EXISTS (SELECT cast(1 as bit) AS [C1] ON EXISTS (SELECT cast(1 as bit) AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable2] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable2] LEFT OUTER JOIN (SELECT [Extent9].[ID] AS [ID] LEFT OUTER JOIN (SELECT [Extent9].[ID] AS [ID] FROM [DBA].[Person] AS [Extent9] WHERE [Extent6].[ID] = [Extent9].[ID] )AS [Project5] ON cast(1 as bit) = cast(1 as bit) FROM [DBA].[Person] AS [Extent9] WHERE [Extent6].[ID] = [Extent9].[ID] )AS [Project5] ON cast(1 as bit) = cast(1 as bit) LEFT OUTER JOIN (SELECT [Extent8].[ID] AS [ID] LEFT OUTER JOIN (SELECT [Extent8].[ID] AS [ID] FROM [DBA].[Person] AS [Extent8] WHERE [Extent6].[ID] = [Extent8].[ID] )AS [Project6] ON cast(1 as bit) = cast(1 as bit) FROM [DBA].[Person] AS [Extent8] WHERE [Extent6].[ID] = [Extent8].[ID] )AS [Project6] ON cast(1 as bit) = cast(1 as bit) WHERE ([Extent5].[ID] = [Project5].[ID]) OR (([Extent5].[ID] IS NULL) AND ([Project6].[ID] IS NULL)) WHERE ([Extent5].[ID] = [Project5].[ID]) OR (([Extent5].[ID] IS NULL) AND ([Project6].[ID] IS NULL)) ) ) AS [Project8] ON ([Project8].[ID] = [Distinct1].[ID]) OR (([Project8].[ID] IS NULL) AND ([Distinct1].[ID] IS NULL)) ) AS [Project9] ORDER BY [Project9].[ID] ASC, [Project9].[C2] ASC Generated Queries: Object Relational Mapping (ORM)

W = SELECT DISTINCT [Extent1].[ID] AS [ID] W = SELECT DISTINCT [Extent1].[ID] AS [ID] FROM [DBA].[Person] AS [Extent1] FROM [DBA].[Person] AS [Extent1] INNER JOIN [DBA].[Sales] AS [Extent2] INNER JOIN [DBA].[Sales] AS [Extent2] ON EXISTS (SELECT cast(1 as bit) AS [C1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] FROM ( SELECT cast(1 as bit) AS X ) AS [SingleRowTable1] LEFT OUTER JOIN (SELECT [Extent3].[ID] AS [ID] LEFT OUTER JOIN (SELECT [Extent3].[ID] AS [ID] FROM [DBA].[Person] AS [Extent3] WHERE [Extent2].[ID] = [Extent3].[ID] )AS [Project1] ON cast(1 as bit) = cast(1 as bit) FROM [DBA].[Person] AS [Extent3] WHERE [Extent2].[ID] = [Extent3].[ID] )AS [Project1] ON cast(1 as bit) = cast(1 as bit) LEFT OUTER JOIN (SELECT [Extent4].[ID] AS [ID] LEFT OUTER JOIN (SELECT [Extent4].[ID] AS [ID] FROM [DBA].[Person] AS [Extent4] WHERE [Extent2].[ID] = [Extent4].[ID] ) AS [Project2] ON cast(1 as bit) = cast(1 as bit) FROM [DBA].[Person] AS [Extent4] WHERE [Extent2].[ID] = [Extent4].[ID] ) AS [Project2] ON cast(1 as bit) = cast(1 as bit) WHERE ([Extent1].[ID] = [Project1].[ID]) OR (([Extent1].[ID] IS NULL) AND ([Project2].[ID] IS NULL)) ) WHERE ([Extent1].[ID] = [Project1].[ID]) OR (([Extent1].[ID] IS NULL) AND ([Project2].[ID] IS NULL)) ) Q1 = SELECT [Project9].[ID] AS [ID],[Project9].[C1] AS [C1],[Project9].[C2] AS [C2],[Project9].[ID1] AS [ID1],[Project9].[SalesOrderID] AS [SalesOrderID],[Project9].[TotalDue] AS [TotalDue] FROM ( SELECT [Distinct1].[ID] AS [ID], 1 AS [C1], [Project8].[ID] AS [ID1], [Project8].[SalesOrderID] AS [SalesOrderID], [Project8].[TotalDue] AS [TotalDue], [Project8].[C1] AS [C2] ( FROM ( W ) AS [Distinct1] LEFT OUTER JOIN ( W ) AS [Project8] ON ([Project8].[ID] = [Distinct1].[ID]) OR (([Project8].[ID] IS NULL) AND ([Distinct1].[ID] IS NULL)) ) AS [Project9] ORDER BY [Project9].[ID] ASC, [Project9].[C2] ASC Generated Queries: ORM (cont.)

Q1 = select Extent6.ID as, 1 as C1, 1 as C2, Extent6.ID as ID1, Extent6.SalesOrderID as SalesOrderID, Extent6.TotalDue as TotalDue from DBA.Sales as Extent6 order by Extent6.ID as ID asc

ORM Very unique features different than human-written SQL queries: EXISTS(subquery) predicates in the ON clause Similar query blocks used in many FROM clauses Cannot change the application to simplify or generate better queries LOJ with exactly 1 row in the preserved side The join condition is inside the correlated derived table ON conditions of the form 1=1 Semantic transformations: 7 new semantic transformations implemented 1 patent select Distinct1.ID as ID as ID, 1 as C1 as C1, 1 as C1 as C2 as C2, Extent6.ID as ID as ID1 as ID1, Extent6.SalesOrderID as SalesOrderID as SalesOrderID as SalesOrderID, Extent6.TotalDue as TotalDue as TotalDue as TotalDue from(select distinct Extent2.ID as ID from DBA.Sales as Extent2) as Distinct1(ID) left outer join DBA.Sales as Extent6, on Extent6.ID as ID = Distinct1.ID order by Distinct1.ID as ID asc

Generated Queries: Hibernate Q2 = SELECT DISTINCT Device.ID,Device.Name FROM Device WHERE Device.ID IN (SELECT DISTINCT Device.ID FROM Device WHERE Device.ID IN (SELECT DISTINCT Device.ID FROM Device WHERE Device.ID IN (SELECT DISTINCT Device.ID FROM Device WHERE Device.ID IN (SELECT DISTINCT Device.ID FROM Device WHERE Device.ID IN (SELECT DISTINCT Device.ID FROM Device WHERE Device.ID IN (SELECT DISTINCT Device.ID FROM Device WHERE Device.ID IN (SELECT Device.ID FROM Device,Entry WHERE Device.dbId = Entry.dd AND UPPER(Entry.ss) LIKE '% %') OR Device.ID IN ( ( SELECT ID FROM Device WHERE ( UPPER(Device.sName) LIKE '%SSMC%' ) ) ) ) OR Device.ID IN ( ( SELECT ID FROM Device WHERE ( UPPER(Device.sName) LIKE '%SSTB%' ) ) ) ) OR Device.ID IN ( ( SELECT dcrDeviceId FROM Device WHERE ( UPPER(Device.sName) LIKE '%XHFH%' ) ) ) ) OR Device.ID IN ( ( SELECT ID FROM Device WHERE ( UPPER(Device.Name) LIKE '%SSTB%' ) ) ) ) OR Device.ID IN ( ( SELECT ID FROM Device WHERE ( UPPER(Device.Name) LIKE '%XHFH%' ) ) ) ) OR Device.ID IN ( ( SELECT Device.ID FROM Device,Entry WHERE ( ( Device.dbId = Entry.dd ) ) AND ( UPPER(Entry.ss) LIKE '% %' ) ) ) )

sq(c0,c1): select DISTINCT T.X from T where T.X IN (select T.X from T JOIN R where R.Y like c0) or T.X IN (select T.X from T where T.Y like c1) sq(co,c1,…,cn): select DISTINCT T.X from T where T.X IN ( sub(c0,c1,..,c(n-1)) ) or T.X IN (select T.X from T where T.Y like cn) sq('% %, '%SSMC%, '%SSTB%, '%XHFH%, '%SSTB%, '%XHFH%, '% %) Generated Queries: Hibernate (cont.)

Q2 = SELECT DISTINCT W0.ID,W0.Name FROM Device W0 WHERE ( W0.ID IS NOT NULL and ( W0.ID IN (SELECT W9.ID FROM Device W9,Entry WHERE W9.dbId = Entry.dd AND UPPER(Entry.ss) LIKE '% %') ) OR ( UPPER(W0.sName) LIKE '%SSMC%' ) OR ( UPPER(W0.sName) LIKE '%SSTB%' ) OR ( UPPER(W0.sName) LIKE '%XHFH%' ) OR (UPPER(W0.Name) LIKE '%SSTB%' ) OR (UPPER(W0.Name) LIKE '%XHFH%' ) OR W0.ID IN ( ( SELECT W12.ID FROM Device W12,Entry WHERE W12.dbId = Entry.dd AND UPPER(Entry.ss) LIKE '% %' ) Generated Queries: Hibernate (cont.)

8 New Optimizations WorkloadPlatfor m Comm# StmtsUnique Stmts DB Size Customer 1SolarisjConnect TCP/IP 30K1520 MB Customer 2CEESQL ShMem MB Customer 3Win32ODBC ShMem 13K37165 MB New optimizations cheap queries could be 2-3 times slower Cheap requests doesnt mean simple queries: {cheap, expensive} X {simple, complex}

Cardinality estimation errors Errors in cardinality estimation for first joins: ,000,000 rows vs. 1,000 estimated rows 15min vs. 3s

SearchSpaceAnalyzer Anisoara Nica, Daniel S. Brotherston, David W. Hillis Extreme Visualization of Query Optimizer Search Spaces, ACM SIGMOD International Conference on Management of Data, SIGMOD 2009,

Anisoara Nica, Daniel S. Brotherston, David W. Hillis Extreme Visualization of Query Optimizer Search Spaces, ACM SIGMOD International Conference on Management of Data, SIGMOD 2009,