SlideShare a Scribd company logo
1 of 27
Discover the power of Recursive SQL and
query transformation with IBM Informix 12.1
Ajaykumar Gupte
IBM
Agenda
• Overview
• ANSI JOIN View Folding
• ANSI to IFMX JOIN cases
• OLAP & SETOPS cases
• HASH JOIN for “IS NULL” Expression
• TEMP Table Optimization
• Recursive SQL (Hierarchical Query Rewrite )
Overview

Query transformation (rewrite)

Simple way to execute query

Optimizer -

select better plan

More choice for table join order

Avoid internal temp table
Views with ANSI joins
• create view v1(vc1, vc2) as
• select t1.a , t2.a from t1 left join t2 on t1.a = t2.a
where t1.b = 5;
• create view v2(vc1, vc2) as
• select v1.vc1 , t3.a from (v1 left join t3 on v1.vc1 =
t3.a);
Simple case
select * from v1 left join t3 on v1.vc1 = t3.a where v1.vc2 = 10
1) informix.t1: INDEX PATH
Filters: informix.t1.b = 5
(1) Index Name: informix.ind1
Index Keys: a (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = 10
2) informix.t2: INDEX PATH
(1) Index Name: informix.ind2
Index Keys: a (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t2.a
NESTED LOOP JOIN
3) informix.t3: INDEX PATH
(1) Index Name: informix.ind3
Index Keys: a (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t3.a
NESTED LOOP JOIN
V1 → t1 left join t2
Table map
V1 T2 T3 T4T1
1 2 3 4 5
T2 T3 T4T1 T5 T6 T7
Table order before
View folding
Table order after
View folding
1 2 3 4 5 6 7
T5 LEFT JOIN T6 LEFT JOIN T7
Nested ANSI join views
select * from v2
1) informix.t1: SEQUENTIAL SCAN
Filters: informix.t1.b = 5
2) informix.t2: INDEX PATH
(1) Index Name: informix.ind2
Index Keys: a (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t2.a
NESTED LOOP JOIN
3) informix.t3: INDEX PATH
(1) Index Name: informix.ind3
Index Keys: a (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t3.a
NESTED LOOP JOIN
V2 → V1 left join t3
V1 → t1 left join t2
ANSI to IFMX JOIN cases

Transform ANSI OUTER JOIN --> ANSI INNER JOIN

Transform ANSI INNER JOIN -->INFORMIX INNER JOIN

Transform ANSI OUTER JOIN --> INFORMIX OUTER
JOIN

Applicable to tables inside view or derived table definition
ANSI to IFMX JOIN case
select * from ((t1 left join t2 on t1.a = t2.b) left join t3 on t1.a = t3.b)
Estimated Cost: 8
Estimated # of Rows Returned: 3
1) informix.t1: SEQUENTIAL SCAN
2) informix.t3: SEQUENTIAL SCAN
DYNAMIC HASH JOIN
Dynamic Hash Filters: informix.t1.a = informix.t3.b
3) informix.t2: SEQUENTIAL SCAN
DYNAMIC HASH JOIN
Dynamic Hash Filters: informix.t1.a = informix.t2.b
ANSI OUTER JOIN Transformation
Sub-query case
select * from v1 where vc1 in (select t5.a from t5 where t5.b > 10)
Estimated Cost: 4
Estimated # of Rows Returned: 1
1) informix.t1: INDEX PATH
Filters: informix.t1.b = 5
(1) Index Name: informix.ind1
Index Keys: a (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = ANY <subquery>
2) informix.t2: INDEX PATH
(1) Index Name: informix.ind2
Index Keys: a (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t2.a
NESTED LOOP JOIN
OLAP case
select vc1, sum(vc2) over (partition by vc2 order by vc1) from v1
Estimated Cost: 3
Estimated # of Rows Returned: 1
1) informix.t1: SEQUENTIAL SCAN
Filters: informix.t1.b = 5
2) informix.t2: INDEX PATH
(1) Index Name: informix.ind2
Index Keys: a (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t2.a
NESTED LOOP JOIN
V1 → t1 left join t2
type rows_sort est_rows rows_cons
time
-------------------------------------------------
sort 1 0 1 00:00.00
type it_count time
----------------------------
olap 1 00:00.00
Setops case
select vc1, vc2 from v1 intersect select t3.a, t3.b from t3
1) informix.t1: SEQUENTIAL SCAN
Filters: informix.t1.b = 5
2) informix.t2: INDEX PATH
(1) Index Name: informix.ind2
Index Keys: a (Key-Only) (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t2.a
NESTED LOOP JOIN
1) (Temp Table For Collection Subquery): SEQUENTIAL SCAN
2) informix.t3: INDEX PATH (First Row)
Filters: (Temp Table For Collection Subquery).vc2 == informix.t3.b
(1) Index Name: informix.ind3
Index Keys: a (Serial, fragments: ALL)
Lower Index Filter: (Temp Table For Collection Subquery).vc1 == informix.t3.a
NESTED LOOP JOIN (Semi Join)
V1 → t1 left join t2
Temp table for view
When Temp table is used ?
• ANSI OUTER JOIN view Restrictions
– Full join
– Multiple views
– View on subservient side
– Select * from t1 left join v1 on t1.a = v1.vc1;
– Union all views with OUTER JOIN
– Complex cases - Sub-query inside view
ANSI JOIN View Folding
• Performance tips
– ANSI OUTER view or derived table on dominant
side
– Avoid complex views (aggregate, correlated sub-
query ) with large data set
– Analyze views or derived tables with large data set
HASH JOIN for “IS NULL”
• Use HASH JOIN for “IS NULL” expression
• select count(*) from t1, t2 where a=x or (a is null
and x is null)
• Generalized expression :
• (expr1 = expr2) or ((expr1) IS NULL AND (expr2) IS
NULL)
HASH JOIN for “IS NULL”
HASH JOIN for “IS NULL”
HASH JOIN for “IS NULL”
Complex case with 3 table joins :
select count (*) from T0, T1, T2 where
(T2.C0=T0.C0 or (T2.C0 is null and T0.C0 is null)) and
(T2.C1=T0.C1 or (T2.C1 is null and T0.C1 is null)) and
(T2.C2=T1.C0 or (T2.C2 is null and T1.C0 is null)) and
(T2.C3=T1.C1 or (T2.C3 is null and T1.C1 is null)) and
(T2.C4=T1.C2 or (T2.C4 is null and T1.C2 is null))
Temp table optimization
• Large number of columns
• performance degradation due to increased memory
• The BI tools - reuse templates based on tables
• Query will use all columns at lower level, when top level
columns are less
• Large number of derived table : multiple materialization of
intermediate result sets into temporary tables
Select v1.vc1, v1.vc2, v1.vc3 from v1
Temp table for view v2
(v2c1, v2c2, v2c3)
Temp table for view v1
(vc1, vc2, vc3, vc4)
Temp table for view v3
(v3c1, v3c2, v3c3)
Temp table for view v4
(v4c1, v4c2, v4c3)
Temp table for view v5
(v5c1, v5c2, v5c3)
Temp table for view v6
(v6c1, v6c2, v6c3)
Base tables t1 , t2
(t1.c1, t2.c1, t2.c2)
Base tables t3 , t4
(t3.c1, t4.c1, t4.c2)
Base tables t5 , t6
(t5.c1, t6.c1, t6.c2)
Original column map
Select v1.vc1, v1.vc2, v1.vc3 from v1
Temp table for view v2
(v2c1, v2c2)
Temp table for view v1
(vc1, vc2, vc3)
Temp table for view v3
(v3c2)
Temp table for view v4
(v4c1)
Temp table for view v5
(v5c2)
Temp table for view v6
(v6c1)
Base tables t1 , t2
(t1.c1)
Base tables t3 , t4
( t4.c1)
Base tables t5 , t6
(t5.c1)
Optimized column map
Recursive SQL
(Hierarchical Query Rewrite )
Problem of querying hierarchical data
• Common technique of storing hierarchical data in
relational tables is self-reference
– Employee-Manager
• Employee table (key – empid)
• Every employee has a manager (indicated by mgrid)
• Manager is also an employee (with a valid empid)
– Shipment
• Inbound shipment table (key – item_id)
• Each item can belong to a package ( key –
package_id)
• Every package is itself an item (with a valid item_id)
CREATE TABLE employee (
empid INTEGER NOT NULL
PRIMARY KEY,
name VARCHAR(10),
salary DECIMAL(9, 2),
mgrid INTEGER);
CREATE TABLE employee (
empid INTEGER NOT NULL
PRIMARY KEY,
name VARCHAR(10),
salary DECIMAL(9, 2),
mgrid INTEGER);
CREATE TABLE inbound_shipment (
shipment_id VARCHAR(50),
item_id VARCHAR(20) ,
package_id VARCHAR(20),
.......
);
CREATE TABLE inbound_shipment (
shipment_id VARCHAR(50),
item_id VARCHAR(20) ,
package_id VARCHAR(20),
.......
);
SELECT level as package_level, item_id,
package_id
FROM inbound_shipment
START WITH item_id = 'pallet_BX505'
CONNECT BY PRIOR
item_id = package_id
Table with
hierarchical
data
Seed of
recursion
Condition to
recurse on
Using CONNECT BY to discover
data hierarchy
Results of CONNECT BY Query
packing_level item_id package_id
1 pallet_BX505 ship_CX2555
2 box_C1255 pallet_BX505
3 band_aid_H10 box_C1255
3 band_aid_H12 box_C1255
3 A1_pharma_F23 box_C1255
3 A1_pharma_F33 box_C1255
sqexplain• QUERY:
• SELECT level as package_level, item_id, package_id FROM inbound_shipment
START WITH item_id = 'pallet_BX505' CONNECT BY PRIOR item_id =
package_id
• Connect by Query Rewrite:
• select x0.level ,x0.item_id ,x0.package_id from
• (select x1.item_id ,x1.package_id ,x1.item_id ,1 ,1 ,0 from
"informix".inbound_shipment x1 where (x1.item_id = 'pallet_BX505' )
• union all
• select x2.item_id ,x2.package_id ,x2.item_id ,(level + 1 ) ::integer
,connect_by_isleaf ,dtab_30093_173_stkcol from
"informix".inbound_shipment x2 ,"informix".dtab_30093_173 x0 where
(dtab_30093_173_p_item_id = x2.package_id ) )
• X0
(item_id,package_id,dtab_30093_173_p_item_id,level,connect_by_isleaf,dtab
_30093_173_stkcol)
START WITH
Questions?
Discover the power of Recursive SQL and query transformation with IBM Informix 12.1
Ajaykumar Gupte IBM gupte@us.ibm.com

More Related Content

What's hot

OptView2 MUC meetup slides
OptView2 MUC meetup slidesOptView2 MUC meetup slides
OptView2 MUC meetup slidesOfek Shilon
 
Oracle AWR Data mining
Oracle AWR Data miningOracle AWR Data mining
Oracle AWR Data miningYury Velikanov
 
Turbocharge SQL Performance in PL/SQL with Bulk Processing
Turbocharge SQL Performance in PL/SQL with Bulk ProcessingTurbocharge SQL Performance in PL/SQL with Bulk Processing
Turbocharge SQL Performance in PL/SQL with Bulk ProcessingSteven Feuerstein
 
PySpark 배우기 Ch 06. ML 패키지 소개하기
PySpark 배우기 Ch 06. ML 패키지 소개하기PySpark 배우기 Ch 06. ML 패키지 소개하기
PySpark 배우기 Ch 06. ML 패키지 소개하기찬희 이
 
DB12c: All You Need to Know About the Resource Manager
DB12c: All You Need to Know About the Resource ManagerDB12c: All You Need to Know About the Resource Manager
DB12c: All You Need to Know About the Resource ManagerAndrejs Vorobjovs
 
Oracle to MySQL DatabaseLink
Oracle to MySQL DatabaseLinkOracle to MySQL DatabaseLink
Oracle to MySQL DatabaseLinkOsama Mustafa
 
Oracle Database performance tuning using oratop
Oracle Database performance tuning using oratopOracle Database performance tuning using oratop
Oracle Database performance tuning using oratopSandesh Rao
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptChien Chung Shen
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Proceduresrehaniltifat
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Developmentrehaniltifat
 
REST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseREST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseJeff Smith
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Conceptsrehaniltifat
 
Oracle 21c: New Features and Enhancements of Data Pump & TTS
Oracle 21c: New Features and Enhancements of Data Pump & TTSOracle 21c: New Features and Enhancements of Data Pump & TTS
Oracle 21c: New Features and Enhancements of Data Pump & TTSChristian Gohmann
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersCarlos Sierra
 
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...CloudxLab
 

What's hot (20)

OptView2 MUC meetup slides
OptView2 MUC meetup slidesOptView2 MUC meetup slides
OptView2 MUC meetup slides
 
Oracle AWR Data mining
Oracle AWR Data miningOracle AWR Data mining
Oracle AWR Data mining
 
Turbocharge SQL Performance in PL/SQL with Bulk Processing
Turbocharge SQL Performance in PL/SQL with Bulk ProcessingTurbocharge SQL Performance in PL/SQL with Bulk Processing
Turbocharge SQL Performance in PL/SQL with Bulk Processing
 
PySpark 배우기 Ch 06. ML 패키지 소개하기
PySpark 배우기 Ch 06. ML 패키지 소개하기PySpark 배우기 Ch 06. ML 패키지 소개하기
PySpark 배우기 Ch 06. ML 패키지 소개하기
 
DB12c: All You Need to Know About the Resource Manager
DB12c: All You Need to Know About the Resource ManagerDB12c: All You Need to Know About the Resource Manager
DB12c: All You Need to Know About the Resource Manager
 
Rac 12c optimization
Rac 12c optimizationRac 12c optimization
Rac 12c optimization
 
Oracle to MySQL DatabaseLink
Oracle to MySQL DatabaseLinkOracle to MySQL DatabaseLink
Oracle to MySQL DatabaseLink
 
Oracle Database performance tuning using oratop
Oracle Database performance tuning using oratopOracle Database performance tuning using oratop
Oracle Database performance tuning using oratop
 
Table functions - Planboard Symposium 2013
Table functions - Planboard Symposium 2013Table functions - Planboard Symposium 2013
Table functions - Planboard Symposium 2013
 
Oracle Database SQL Tuning Concept
Oracle Database SQL Tuning ConceptOracle Database SQL Tuning Concept
Oracle Database SQL Tuning Concept
 
Rapid Home Provisioning
Rapid Home ProvisioningRapid Home Provisioning
Rapid Home Provisioning
 
Using AWR for IO Subsystem Analysis
Using AWR for IO Subsystem AnalysisUsing AWR for IO Subsystem Analysis
Using AWR for IO Subsystem Analysis
 
05 Creating Stored Procedures
05 Creating Stored Procedures05 Creating Stored Procedures
05 Creating Stored Procedures
 
07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development07 Using Oracle-Supported Package in Application Development
07 Using Oracle-Supported Package in Application Development
 
REST Enabling Your Oracle Database
REST Enabling Your Oracle DatabaseREST Enabling Your Oracle Database
REST Enabling Your Oracle Database
 
MySQL Introduction
MySQL IntroductionMySQL Introduction
MySQL Introduction
 
06 Using More Package Concepts
06 Using More Package Concepts06 Using More Package Concepts
06 Using More Package Concepts
 
Oracle 21c: New Features and Enhancements of Data Pump & TTS
Oracle 21c: New Features and Enhancements of Data Pump & TTSOracle 21c: New Features and Enhancements of Data Pump & TTS
Oracle 21c: New Features and Enhancements of Data Pump & TTS
 
Understanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginnersUnderstanding SQL Trace, TKPROF and Execution Plan for beginners
Understanding SQL Trace, TKPROF and Execution Plan for beginners
 
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
Apache Spark - Dataframes & Spark SQL - Part 1 | Big Data Hadoop Spark Tutori...
 

Similar to Discover the power of Recursive SQL and query transformation with Informix database

Optimizer Enhancement in Informix
Optimizer Enhancement in InformixOptimizer Enhancement in Informix
Optimizer Enhancement in InformixBingjie Miao
 
IBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinIBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinAjay Gupte
 
Sql cheat-sheet
Sql cheat-sheetSql cheat-sheet
Sql cheat-sheetSteve Tran
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB plc
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3MariaDB plc
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101Sveta Smirnova
 
Postgres can do THAT?
Postgres can do THAT?Postgres can do THAT?
Postgres can do THAT?alexbrasetvik
 
Best SAP ABAP Training Institute with Placement in Pune | Aspire
Best SAP ABAP Training Institute with Placement in Pune | AspireBest SAP ABAP Training Institute with Placement in Pune | Aspire
Best SAP ABAP Training Institute with Placement in Pune | AspireAspire Techsoft Academy
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZENorvald Ryeng
 
ALTER TABLE Improvements in MariaDB Server
ALTER TABLE Improvements in MariaDB ServerALTER TABLE Improvements in MariaDB Server
ALTER TABLE Improvements in MariaDB ServerMariaDB plc
 
Sql Queries
Sql QueriesSql Queries
Sql Querieswebicon
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptxrediet43
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsAsuka Nakajima
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesKeshav Murthy
 
Internal tables
Internal tables Internal tables
Internal tables Jibu Jose
 
Data Warehouse and Business Intelligence - Recipe 3
Data Warehouse and Business Intelligence - Recipe 3Data Warehouse and Business Intelligence - Recipe 3
Data Warehouse and Business Intelligence - Recipe 3Massimo Cenci
 
Row Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12cRow Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12cStew Ashton
 
How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012Connor McDonald
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c eraMauro Pagano
 

Similar to Discover the power of Recursive SQL and query transformation with Informix database (20)

Optimizer Enhancement in Informix
Optimizer Enhancement in InformixOptimizer Enhancement in Informix
Optimizer Enhancement in Informix
 
IBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash JoinIBM Informix Database SQL Set operators and ANSI Hash Join
IBM Informix Database SQL Set operators and ANSI Hash Join
 
Sql cheat-sheet
Sql cheat-sheetSql cheat-sheet
Sql cheat-sheet
 
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-KompatibilitätMariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
MariaDB Server 10.3 - Temporale Daten und neues zur DB-Kompatibilität
 
What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3What's New in MariaDB Server 10.3
What's New in MariaDB Server 10.3
 
MySQL Query tuning 101
MySQL Query tuning 101MySQL Query tuning 101
MySQL Query tuning 101
 
Postgres can do THAT?
Postgres can do THAT?Postgres can do THAT?
Postgres can do THAT?
 
Aspire it sap abap training
Aspire it   sap abap trainingAspire it   sap abap training
Aspire it sap abap training
 
Best SAP ABAP Training Institute with Placement in Pune | Aspire
Best SAP ABAP Training Institute with Placement in Pune | AspireBest SAP ABAP Training Institute with Placement in Pune | Aspire
Best SAP ABAP Training Institute with Placement in Pune | Aspire
 
MySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZEMySQL 8.0 EXPLAIN ANALYZE
MySQL 8.0 EXPLAIN ANALYZE
 
ALTER TABLE Improvements in MariaDB Server
ALTER TABLE Improvements in MariaDB ServerALTER TABLE Improvements in MariaDB Server
ALTER TABLE Improvements in MariaDB Server
 
Sql Queries
Sql QueriesSql Queries
Sql Queries
 
Algorithim lec1.pptx
Algorithim lec1.pptxAlgorithim lec1.pptx
Algorithim lec1.pptx
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
IBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql FeaturesIBM Informix dynamic server 11 10 Cheetah Sql Features
IBM Informix dynamic server 11 10 Cheetah Sql Features
 
Internal tables
Internal tables Internal tables
Internal tables
 
Data Warehouse and Business Intelligence - Recipe 3
Data Warehouse and Business Intelligence - Recipe 3Data Warehouse and Business Intelligence - Recipe 3
Data Warehouse and Business Intelligence - Recipe 3
 
Row Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12cRow Pattern Matching in Oracle Database 12c
Row Pattern Matching in Oracle Database 12c
 
How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012
 
Histograms in 12c era
Histograms in 12c eraHistograms in 12c era
Histograms in 12c era
 

Recently uploaded

Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024StefanoLambiase
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmSujith Sukumaran
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaHanief Utama
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureDinusha Kumarasiri
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxnada99848
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio, Inc.
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsAhmed Mohamed
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfAlina Yurenko
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfPower Karaoke
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEEVICTOR MAESTRE RAMIREZ
 

Recently uploaded (20)

Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
Dealing with Cultural Dispersion — Stefano Lambiase — ICSE-SEIS 2024
 
Intelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalmIntelligent Home Wi-Fi Solutions | ThinkPalm
Intelligent Home Wi-Fi Solutions | ThinkPalm
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
React Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief UtamaReact Server Component in Next.js by Hanief Utama
React Server Component in Next.js by Hanief Utama
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort ServiceHot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
Hot Sexy call girls in Patel Nagar🔝 9953056974 🔝 escort Service
 
Implementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with AzureImplementing Zero Trust strategy with Azure
Implementing Zero Trust strategy with Azure
 
software engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptxsoftware engineering Chapter 5 System modeling.pptx
software engineering Chapter 5 System modeling.pptx
 
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed DataAlluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
Alluxio Monthly Webinar | Cloud-Native Model Training on Distributed Data
 
Unveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML DiagramsUnveiling Design Patterns: A Visual Guide with UML Diagrams
Unveiling Design Patterns: A Visual Guide with UML Diagrams
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdfGOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
GOING AOT WITH GRAALVM – DEVOXX GREECE.pdf
 
The Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdfThe Evolution of Karaoke From Analog to App.pdf
The Evolution of Karaoke From Analog to App.pdf
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
Cloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEECloud Data Center Network Construction - IEEE
Cloud Data Center Network Construction - IEEE
 

Discover the power of Recursive SQL and query transformation with Informix database

  • 1. Discover the power of Recursive SQL and query transformation with IBM Informix 12.1 Ajaykumar Gupte IBM
  • 2. Agenda • Overview • ANSI JOIN View Folding • ANSI to IFMX JOIN cases • OLAP & SETOPS cases • HASH JOIN for “IS NULL” Expression • TEMP Table Optimization • Recursive SQL (Hierarchical Query Rewrite )
  • 3. Overview  Query transformation (rewrite)  Simple way to execute query  Optimizer -  select better plan  More choice for table join order  Avoid internal temp table
  • 4. Views with ANSI joins • create view v1(vc1, vc2) as • select t1.a , t2.a from t1 left join t2 on t1.a = t2.a where t1.b = 5; • create view v2(vc1, vc2) as • select v1.vc1 , t3.a from (v1 left join t3 on v1.vc1 = t3.a);
  • 5. Simple case select * from v1 left join t3 on v1.vc1 = t3.a where v1.vc2 = 10 1) informix.t1: INDEX PATH Filters: informix.t1.b = 5 (1) Index Name: informix.ind1 Index Keys: a (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = 10 2) informix.t2: INDEX PATH (1) Index Name: informix.ind2 Index Keys: a (Key-Only) (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t2.a NESTED LOOP JOIN 3) informix.t3: INDEX PATH (1) Index Name: informix.ind3 Index Keys: a (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t3.a NESTED LOOP JOIN V1 → t1 left join t2
  • 6. Table map V1 T2 T3 T4T1 1 2 3 4 5 T2 T3 T4T1 T5 T6 T7 Table order before View folding Table order after View folding 1 2 3 4 5 6 7 T5 LEFT JOIN T6 LEFT JOIN T7
  • 7. Nested ANSI join views select * from v2 1) informix.t1: SEQUENTIAL SCAN Filters: informix.t1.b = 5 2) informix.t2: INDEX PATH (1) Index Name: informix.ind2 Index Keys: a (Key-Only) (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t2.a NESTED LOOP JOIN 3) informix.t3: INDEX PATH (1) Index Name: informix.ind3 Index Keys: a (Key-Only) (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t3.a NESTED LOOP JOIN V2 → V1 left join t3 V1 → t1 left join t2
  • 8. ANSI to IFMX JOIN cases  Transform ANSI OUTER JOIN --> ANSI INNER JOIN  Transform ANSI INNER JOIN -->INFORMIX INNER JOIN  Transform ANSI OUTER JOIN --> INFORMIX OUTER JOIN  Applicable to tables inside view or derived table definition
  • 9. ANSI to IFMX JOIN case select * from ((t1 left join t2 on t1.a = t2.b) left join t3 on t1.a = t3.b) Estimated Cost: 8 Estimated # of Rows Returned: 3 1) informix.t1: SEQUENTIAL SCAN 2) informix.t3: SEQUENTIAL SCAN DYNAMIC HASH JOIN Dynamic Hash Filters: informix.t1.a = informix.t3.b 3) informix.t2: SEQUENTIAL SCAN DYNAMIC HASH JOIN Dynamic Hash Filters: informix.t1.a = informix.t2.b ANSI OUTER JOIN Transformation
  • 10. Sub-query case select * from v1 where vc1 in (select t5.a from t5 where t5.b > 10) Estimated Cost: 4 Estimated # of Rows Returned: 1 1) informix.t1: INDEX PATH Filters: informix.t1.b = 5 (1) Index Name: informix.ind1 Index Keys: a (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = ANY <subquery> 2) informix.t2: INDEX PATH (1) Index Name: informix.ind2 Index Keys: a (Key-Only) (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t2.a NESTED LOOP JOIN
  • 11. OLAP case select vc1, sum(vc2) over (partition by vc2 order by vc1) from v1 Estimated Cost: 3 Estimated # of Rows Returned: 1 1) informix.t1: SEQUENTIAL SCAN Filters: informix.t1.b = 5 2) informix.t2: INDEX PATH (1) Index Name: informix.ind2 Index Keys: a (Key-Only) (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t2.a NESTED LOOP JOIN V1 → t1 left join t2 type rows_sort est_rows rows_cons time ------------------------------------------------- sort 1 0 1 00:00.00 type it_count time ---------------------------- olap 1 00:00.00
  • 12. Setops case select vc1, vc2 from v1 intersect select t3.a, t3.b from t3 1) informix.t1: SEQUENTIAL SCAN Filters: informix.t1.b = 5 2) informix.t2: INDEX PATH (1) Index Name: informix.ind2 Index Keys: a (Key-Only) (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t2.a NESTED LOOP JOIN 1) (Temp Table For Collection Subquery): SEQUENTIAL SCAN 2) informix.t3: INDEX PATH (First Row) Filters: (Temp Table For Collection Subquery).vc2 == informix.t3.b (1) Index Name: informix.ind3 Index Keys: a (Serial, fragments: ALL) Lower Index Filter: (Temp Table For Collection Subquery).vc1 == informix.t3.a NESTED LOOP JOIN (Semi Join) V1 → t1 left join t2 Temp table for view
  • 13. When Temp table is used ? • ANSI OUTER JOIN view Restrictions – Full join – Multiple views – View on subservient side – Select * from t1 left join v1 on t1.a = v1.vc1; – Union all views with OUTER JOIN – Complex cases - Sub-query inside view
  • 14. ANSI JOIN View Folding • Performance tips – ANSI OUTER view or derived table on dominant side – Avoid complex views (aggregate, correlated sub- query ) with large data set – Analyze views or derived tables with large data set
  • 15. HASH JOIN for “IS NULL” • Use HASH JOIN for “IS NULL” expression • select count(*) from t1, t2 where a=x or (a is null and x is null) • Generalized expression : • (expr1 = expr2) or ((expr1) IS NULL AND (expr2) IS NULL)
  • 16. HASH JOIN for “IS NULL”
  • 17. HASH JOIN for “IS NULL”
  • 18. HASH JOIN for “IS NULL” Complex case with 3 table joins : select count (*) from T0, T1, T2 where (T2.C0=T0.C0 or (T2.C0 is null and T0.C0 is null)) and (T2.C1=T0.C1 or (T2.C1 is null and T0.C1 is null)) and (T2.C2=T1.C0 or (T2.C2 is null and T1.C0 is null)) and (T2.C3=T1.C1 or (T2.C3 is null and T1.C1 is null)) and (T2.C4=T1.C2 or (T2.C4 is null and T1.C2 is null))
  • 19. Temp table optimization • Large number of columns • performance degradation due to increased memory • The BI tools - reuse templates based on tables • Query will use all columns at lower level, when top level columns are less • Large number of derived table : multiple materialization of intermediate result sets into temporary tables
  • 20. Select v1.vc1, v1.vc2, v1.vc3 from v1 Temp table for view v2 (v2c1, v2c2, v2c3) Temp table for view v1 (vc1, vc2, vc3, vc4) Temp table for view v3 (v3c1, v3c2, v3c3) Temp table for view v4 (v4c1, v4c2, v4c3) Temp table for view v5 (v5c1, v5c2, v5c3) Temp table for view v6 (v6c1, v6c2, v6c3) Base tables t1 , t2 (t1.c1, t2.c1, t2.c2) Base tables t3 , t4 (t3.c1, t4.c1, t4.c2) Base tables t5 , t6 (t5.c1, t6.c1, t6.c2) Original column map
  • 21. Select v1.vc1, v1.vc2, v1.vc3 from v1 Temp table for view v2 (v2c1, v2c2) Temp table for view v1 (vc1, vc2, vc3) Temp table for view v3 (v3c2) Temp table for view v4 (v4c1) Temp table for view v5 (v5c2) Temp table for view v6 (v6c1) Base tables t1 , t2 (t1.c1) Base tables t3 , t4 ( t4.c1) Base tables t5 , t6 (t5.c1) Optimized column map
  • 23. Problem of querying hierarchical data • Common technique of storing hierarchical data in relational tables is self-reference – Employee-Manager • Employee table (key – empid) • Every employee has a manager (indicated by mgrid) • Manager is also an employee (with a valid empid) – Shipment • Inbound shipment table (key – item_id) • Each item can belong to a package ( key – package_id) • Every package is itself an item (with a valid item_id) CREATE TABLE employee ( empid INTEGER NOT NULL PRIMARY KEY, name VARCHAR(10), salary DECIMAL(9, 2), mgrid INTEGER); CREATE TABLE employee ( empid INTEGER NOT NULL PRIMARY KEY, name VARCHAR(10), salary DECIMAL(9, 2), mgrid INTEGER); CREATE TABLE inbound_shipment ( shipment_id VARCHAR(50), item_id VARCHAR(20) , package_id VARCHAR(20), ....... ); CREATE TABLE inbound_shipment ( shipment_id VARCHAR(50), item_id VARCHAR(20) , package_id VARCHAR(20), ....... );
  • 24. SELECT level as package_level, item_id, package_id FROM inbound_shipment START WITH item_id = 'pallet_BX505' CONNECT BY PRIOR item_id = package_id Table with hierarchical data Seed of recursion Condition to recurse on Using CONNECT BY to discover data hierarchy
  • 25. Results of CONNECT BY Query packing_level item_id package_id 1 pallet_BX505 ship_CX2555 2 box_C1255 pallet_BX505 3 band_aid_H10 box_C1255 3 band_aid_H12 box_C1255 3 A1_pharma_F23 box_C1255 3 A1_pharma_F33 box_C1255
  • 26. sqexplain• QUERY: • SELECT level as package_level, item_id, package_id FROM inbound_shipment START WITH item_id = 'pallet_BX505' CONNECT BY PRIOR item_id = package_id • Connect by Query Rewrite: • select x0.level ,x0.item_id ,x0.package_id from • (select x1.item_id ,x1.package_id ,x1.item_id ,1 ,1 ,0 from "informix".inbound_shipment x1 where (x1.item_id = 'pallet_BX505' ) • union all • select x2.item_id ,x2.package_id ,x2.item_id ,(level + 1 ) ::integer ,connect_by_isleaf ,dtab_30093_173_stkcol from "informix".inbound_shipment x2 ,"informix".dtab_30093_173 x0 where (dtab_30093_173_p_item_id = x2.package_id ) ) • X0 (item_id,package_id,dtab_30093_173_p_item_id,level,connect_by_isleaf,dtab _30093_173_stkcol) START WITH
  • 27. Questions? Discover the power of Recursive SQL and query transformation with IBM Informix 12.1 Ajaykumar Gupte IBM gupte@us.ibm.com