SlideShare a Scribd company logo
Useful Business Analytics SQL
operators and more
Ajaykumar Gupte
IBM
1
4/9/15 2
AGENDA
ïŹ
Set Operators
ïŹ
Functionality, Basic Rules
ïŹ
Null Friendly Intersect and Minus
ïŹ
Usage
ïŹ
Execution Plans
ïŹ
Scenarios
ïŹ
ANSI JOIN Query improvements
4/9/15 3
SET Operators
ïŹ
UNION Operator - Combines the rows from two or
more result sets into a single result set.
ïŹ
INTERSECT Operator - Computes a result set that
contains the common rows from two result sets.
ïŹ
MINUS/EXCEPT Operator - Evaluates two result sets and
returns all rows from the first set that are not also
contained in the second set.
Set Operations Result Sets
‱ MINUS and EXCEPT are synonyms.
4/9/15 5
Functionality Of Intersect and Minus
ïŹ
Extension to the existing UNION/UNION ALL
SET operation
ïŹ
Results are always distinct or unique rows
(eliminate duplicate rows)
ïŹ
Same rules of UNION also applies e.g
- Both query blocks should have exact same number of
columns
- Projection clause should have comparable data types
- Projection clause can not have BYTE or TEXT
4/9/15 6
Functionality Of Intersect and Minus
- Order by should be at the end
- Precedence will be from left to right, unless
they are grouped using parentheses
- Existing restrictions for UNION, applies to
these operators too.
4/9/15 7
NULL Friendly SET Operators
ïŹ
Both Intersect and Minus are NULL friendly,
means when comparing NULL to NULL they are
considered equal
4/9/15 8
Examples
create table t1 (col1 int); create table t2 (col1 int);
insert into t1 values (1); insert into t2 values (1);
insert into t1 values (2); insert into t2 values (3);
insert into t1 values (2); insert into t2 values (4);
insert into t1 values (2); insert into t2 values (4);
insert into t1 values (3); insert into t2 values (NULL);
insert into t1 values (4);
insert into t1 values (4);
insert into t1 values (NULL);
insert into t1 values (NULL);
insert into t1 values (NULL);
4/9/15 9
Examples
select col1 from t1 intersect select col1 from t2;
col1
1
3
4
4 row(s) retrieved.
select col1 from t1 minus select col1 from t2;
col1
2
1 row(s) retrieved.
NULL
4/9/15 10
Usage
Inside VIEW definitions
ïŹ
create view v1(c1,c2) as
select * from tabp intersect select * from tabr;
ïŹ
create view v55(c1,c2) as
select * from tabp minus
(select * from tabr minus select * from v1)
union (select * from tabp minus select * from
tabr);
4/9/15 11
Usage
Inside the Derived Table
select * from
(select tab1.* from tab1 LEFT OUTER JOIN tab2
ON tab1.intcol = tab2.intcol2
intersect
select tab2.* from tab3 FULL OUTER JOIN tab2
ON tab2.charcol2 = tab3.charcol3);
4/9/15 12
Usage
Inside the Subquery
select c1,c2,c3,c4,c5 from mtab1 where
exists (select c1,c2,c3,c4,c5 from stab1
group by c2,c3,c4,c5,c1
intersect
select c1,c2,c3,c4,c5 from stab2
group by c2,c3,c4,c5,c1
having count(*) < 3)
and c1 = 1;
4/9/15 13
Usage
Inside the Procedure
create procedure p1_1()
returning int;
define ret_val int;
define row_val int;
let ret_val = 0;
foreach select intcol into row_val from tab1
intersect
select intcol2 from tab2
let ret_val = ret_val + 1;
end foreach
return ret_val;
end procedure;
4/9/15 14
Usage
Cross database and Cross server
select intcol2, charcol2 from tab2
minus
(select intcol3, charcol3 from db2:tab3
intersect
select intcol, charcol from db3@serv3:tab1);
Set Operators Optimization
‱INTERSECT – rows common to both arms
– internally transformed into EXISTS subquery with special
NULL handling
‱MINUS or EXCEPT – rows in first arm that’s not
in second arm
– internally transformed into NOT EXISTS subquery with
special NULL handling
Nested Loop – Semi Join
‱Execute subquery as a variation of nested-loop join
‱Semi Join- read inner table only until server finds a
match
– for each row in the outer table, the inner table
contributes at most one row
‱Anti Semi Join – return all non-matching rows from
inner table
Set Operations in explain
QUERY:
------
select intcol from tab1
intersect
select intcol2 from tab2
Estimated Cost: 4
Estimated # of Rows Returned: 1
1) informix.tab1: SEQUENTIAL SCAN
2) informix.tab2: SEQUENTIAL SCAN (First Row)
Filters: informix.tab1.intcol ==
informix.tab2.intcol2
NESTED LOOP JOIN (Semi Join)
Set Operations in explain
QUERY:
------
select intcol, charcol from tab1
intersect
select intcol2, charcol2 from tab2
minus
select intcol3, charcol3 from tab3
Estimated Cost: 6
Estimated # of Rows Returned: 1
1) informix.tab1: SEQUENTIAL SCAN
2) informix.tab2: SEQUENTIAL SCAN (First Row)
Filters: (informix.tab1.intcol == informix.tab2.intcol2
AND informix.tab1.charcol == informix.tab2.charcol2 )
NESTED LOOP JOIN (Semi Join)
3) informix.tab3: SEQUENTIAL SCAN (First Row)
Filters: (informix.tab1.charcol == informix.tab3.charcol3
AND informix.tab1.intcol == informix.tab3.intcol3 )
NESTED LOOP JOIN (Anti Semi Join)
Scenarios
This INTERSECT query example finds suppliers who have
placed an order.
select supplier_id from suppliers
INTERSECT
select supplier_id from orders;
This MINUS query example finds suppliers who have not
placed any order.
select supplier_id from suppliers
MINUS
select supplier_id from orders;
ANSI Join improvements
‱ Join Directives supported in ANSI queries
– ORDERED directive not allowed.
‱ HASH Join Support
– Support for Bushy tree and Right deep tree execution.
‱ Optimizer changes to allow comparison
between Nested Loop and Hash Joins.
Hash Join Support in ANSI JOIN
‱ Without Hash join support, only way to
execute joins on large tables without index is
to create DYNAMIC index followed by Nested
Loop join.
‱ Hash join can be faster for large joins
‱ Optimizer costing is adjusted for situation
where build/probe sides for hash join can be
composite
Hash Join for ANSI JOIN in sqexplain
QUERY:
------
select * from (t1 left join t2 on t1.a = t2.a )
left join (t3 inner join t4 on t3.a = t4.a) on t4.a = t1.a
1) informix.t1: SEQUENTIAL SCAN
2) informix.t2: INDEX PATH
(1) Index Name: informix.ind2
Index Keys: a (Serial, fragments: ALL)
Lower Index Filter: informix.t1.a = informix.t2.a
ON-Filters:informix.t1.a = informix.t2.a
NESTED LOOP JOIN(LEFT OUTER JOIN)
3) informix.t3: SEQUENTIAL SCAN
4) informix.t4: INDEX PATH
(1) Index Name: informix.ind4
Index Keys: a (Serial, fragments: ALL)
Lower Index Filter: informix.t3.a = informix.t4.a
ON-Filters:informix.t3.a = informix.t4.a
NESTED LOOP JOIN
ON-Filters:informix.t4.a = informix.t1.a
DYNAMIC HASH JOIN (LEFT OUTER JOIN)
Dynamic Hash Filters: informix.t4.a = informix.t1.a
Questions?
23

More Related Content

PDF
Discover the power of Recursive SQL and query transformation with Informix da...
DOCX
Not in vs not exists
PPT
Single row functions
DOCX
The internals
DOCX
10053 - null is not nothing
PPT
Circular linked list
PPTX
Functions
PPTX
Introduction to oracle optimizer
Discover the power of Recursive SQL and query transformation with Informix da...
Not in vs not exists
Single row functions
The internals
10053 - null is not nothing
Circular linked list
Functions
Introduction to oracle optimizer

What's hot (20)

PPTX
Linked list
PPTX
How mysql choose the execution plan
PPTX
Stacks and Queue - Data Structures
PPT
linked list
PPTX
PLSQL Practices
PPTX
MySQL index optimization techniques
PPTX
QUEUE || FUNCTION WRITING BASED ON QUEUE || LINKED LIST || DATA STRUCTURE || ...
PPSX
Data Structure (Double Linked List)
PPTX
MYSQL single rowfunc-multirowfunc-groupby-having
PPT
Unit ii(dsc++)
PPT
Array Presentation (EngineerBaBu.com)
PPTX
MYSQL using set operators
PPTX
MYSql manage db
PPT
header, circular and two way linked lists
PDF
Sql cheat-sheet
PPTX
A few things about the Oracle optimizer - 2013
PPTX
Linear data structure concepts
PPTX
Sql modifying data - MYSQL part I
DOCX
Basic commands in C++
PPSX
Data Structure (Circular Linked List)
Linked list
How mysql choose the execution plan
Stacks and Queue - Data Structures
linked list
PLSQL Practices
MySQL index optimization techniques
QUEUE || FUNCTION WRITING BASED ON QUEUE || LINKED LIST || DATA STRUCTURE || ...
Data Structure (Double Linked List)
MYSQL single rowfunc-multirowfunc-groupby-having
Unit ii(dsc++)
Array Presentation (EngineerBaBu.com)
MYSQL using set operators
MYSql manage db
header, circular and two way linked lists
Sql cheat-sheet
A few things about the Oracle optimizer - 2013
Linear data structure concepts
Sql modifying data - MYSQL part I
Basic commands in C++
Data Structure (Circular Linked List)
Ad

Viewers also liked (20)

PDF
13119 60c0af96eefbe938c72eb484bc5ac596
PPTX
Step By Step How To Install Oracle XE
PDF
Tutorial Instalisasi Oracle 10g dan Setting User
PPT
Intro oracle10gexpress
PPT
Intro to Application Express
PDF
Oracle intro to designer abridged
PPTX
Sql server ___________ (advance sql)
PPT
Time-Based Blind SQL Injection using Heavy Queries
PPTX
Oracle database introduction
PPT
Managing Oracle Streams Using Enterprise Manager Grid Control
PPTX
T sqlèȘžæł•äč‹ cte 20140214
PPT
Transaction
PDF
Oracle dba trainining in hyderabad
PPTX
Oracle: Joins
PDF
Oracle 10g Installation
PPTX
PPT
Advanced sql
PPT
Advanced Sql Training
PPTX
SQL Data Manipulation
PDF
Oracle Essentials Oracle Database 11g
13119 60c0af96eefbe938c72eb484bc5ac596
Step By Step How To Install Oracle XE
Tutorial Instalisasi Oracle 10g dan Setting User
Intro oracle10gexpress
Intro to Application Express
Oracle intro to designer abridged
Sql server ___________ (advance sql)
Time-Based Blind SQL Injection using Heavy Queries
Oracle database introduction
Managing Oracle Streams Using Enterprise Manager Grid Control
T sqlèȘžæł•äč‹ cte 20140214
Transaction
Oracle dba trainining in hyderabad
Oracle: Joins
Oracle 10g Installation
Advanced sql
Advanced Sql Training
SQL Data Manipulation
Oracle Essentials Oracle Database 11g
Ad

Similar to IBM Informix Database SQL Set operators and ANSI Hash Join (20)

PPT
Ch7
PDF
DBMS Nested & Sub Queries Set operations
PPTX
Subqueriesandjoins unit6
PPTX
1. dml select statement reterive data
PDF
Tech Jam 01 - Database Querying
PPTX
Relational Algebra in DBMS power ppoint pesenetation
PPTX
Oracle: Joins
PPT
Mssql
PPTX
Database Management System Review
PPT
Les08 set operators by Szabist for the MS and MPM
PPT
Les07
PPTX
Practice on Practical SQL
PPTX
Sql server
PPT
Les07 (using the set operators)
PDF
Troubleshooting MySQL Performance add-ons
PDF
Lesson 6 - Relational Algebra.pdf
DOCX
CIS 515 Discussion post responses.Respondto the colleagu.docx
PPT
Sql server introduction to sql server
PPTX
OracleSQLraining.pptx
PPT
lecture2.ppt
 
Ch7
DBMS Nested & Sub Queries Set operations
Subqueriesandjoins unit6
1. dml select statement reterive data
Tech Jam 01 - Database Querying
Relational Algebra in DBMS power ppoint pesenetation
Oracle: Joins
Mssql
Database Management System Review
Les08 set operators by Szabist for the MS and MPM
Les07
Practice on Practical SQL
Sql server
Les07 (using the set operators)
Troubleshooting MySQL Performance add-ons
Lesson 6 - Relational Algebra.pdf
CIS 515 Discussion post responses.Respondto the colleagu.docx
Sql server introduction to sql server
OracleSQLraining.pptx
lecture2.ppt
 

More from Ajay Gupte (6)

ODP
Using Lateral derived table in Informix database
ODP
Building a Hierarchical Data Model Using the Latest IBM Informix Features
PPT
Enabling Applications with Informix' new OLAP functionality
PPT
Using JSON/BSON types in your hybrid application environment
PPT
How IBM API Management use Informix and NoSQL
PPT
NoSQL Analytics: JSON Data Analysis and Acceleration in MongoDB World
Using Lateral derived table in Informix database
Building a Hierarchical Data Model Using the Latest IBM Informix Features
Enabling Applications with Informix' new OLAP functionality
Using JSON/BSON types in your hybrid application environment
How IBM API Management use Informix and NoSQL
NoSQL Analytics: JSON Data Analysis and Acceleration in MongoDB World

Recently uploaded (20)

PDF
top salesforce developer skills in 2025.pdf
PDF
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PDF
Upgrade and Innovation Strategies for SAP ERP Customers
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Digital Strategies for Manufacturing Companies
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PPTX
ai tools demonstartion for schools and inter college
PPTX
Introduction to Artificial Intelligence
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
AI in Product Development-omnex systems
PPTX
history of c programming in notes for students .pptx
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PPTX
Essential Infomation Tech presentation.pptx
PDF
medical staffing services at VALiNTRY
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
System and Network Administraation Chapter 3
top salesforce developer skills in 2025.pdf
EN-Survey-Report-SAP-LeanIX-EA-Insights-2025.pdf
2025 Textile ERP Trends: SAP, Odoo & Oracle
Understanding Forklifts - TECH EHS Solution
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
Upgrade and Innovation Strategies for SAP ERP Customers
PTS Company Brochure 2025 (1).pdf.......
Digital Strategies for Manufacturing Companies
Navsoft: AI-Powered Business Solutions & Custom Software Development
ai tools demonstartion for schools and inter college
Introduction to Artificial Intelligence
Adobe Illustrator 28.6 Crack My Vision of Vector Design
AI in Product Development-omnex systems
history of c programming in notes for students .pptx
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
Essential Infomation Tech presentation.pptx
medical staffing services at VALiNTRY
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
 
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
System and Network Administraation Chapter 3

IBM Informix Database SQL Set operators and ANSI Hash Join

  • 1. Useful Business Analytics SQL operators and more Ajaykumar Gupte IBM 1
  • 2. 4/9/15 2 AGENDA ïŹ Set Operators ïŹ Functionality, Basic Rules ïŹ Null Friendly Intersect and Minus ïŹ Usage ïŹ Execution Plans ïŹ Scenarios ïŹ ANSI JOIN Query improvements
  • 3. 4/9/15 3 SET Operators ïŹ UNION Operator - Combines the rows from two or more result sets into a single result set. ïŹ INTERSECT Operator - Computes a result set that contains the common rows from two result sets. ïŹ MINUS/EXCEPT Operator - Evaluates two result sets and returns all rows from the first set that are not also contained in the second set.
  • 4. Set Operations Result Sets ‱ MINUS and EXCEPT are synonyms.
  • 5. 4/9/15 5 Functionality Of Intersect and Minus ïŹ Extension to the existing UNION/UNION ALL SET operation ïŹ Results are always distinct or unique rows (eliminate duplicate rows) ïŹ Same rules of UNION also applies e.g - Both query blocks should have exact same number of columns - Projection clause should have comparable data types - Projection clause can not have BYTE or TEXT
  • 6. 4/9/15 6 Functionality Of Intersect and Minus - Order by should be at the end - Precedence will be from left to right, unless they are grouped using parentheses - Existing restrictions for UNION, applies to these operators too.
  • 7. 4/9/15 7 NULL Friendly SET Operators ïŹ Both Intersect and Minus are NULL friendly, means when comparing NULL to NULL they are considered equal
  • 8. 4/9/15 8 Examples create table t1 (col1 int); create table t2 (col1 int); insert into t1 values (1); insert into t2 values (1); insert into t1 values (2); insert into t2 values (3); insert into t1 values (2); insert into t2 values (4); insert into t1 values (2); insert into t2 values (4); insert into t1 values (3); insert into t2 values (NULL); insert into t1 values (4); insert into t1 values (4); insert into t1 values (NULL); insert into t1 values (NULL); insert into t1 values (NULL);
  • 9. 4/9/15 9 Examples select col1 from t1 intersect select col1 from t2; col1 1 3 4 4 row(s) retrieved. select col1 from t1 minus select col1 from t2; col1 2 1 row(s) retrieved. NULL
  • 10. 4/9/15 10 Usage Inside VIEW definitions ïŹ create view v1(c1,c2) as select * from tabp intersect select * from tabr; ïŹ create view v55(c1,c2) as select * from tabp minus (select * from tabr minus select * from v1) union (select * from tabp minus select * from tabr);
  • 11. 4/9/15 11 Usage Inside the Derived Table select * from (select tab1.* from tab1 LEFT OUTER JOIN tab2 ON tab1.intcol = tab2.intcol2 intersect select tab2.* from tab3 FULL OUTER JOIN tab2 ON tab2.charcol2 = tab3.charcol3);
  • 12. 4/9/15 12 Usage Inside the Subquery select c1,c2,c3,c4,c5 from mtab1 where exists (select c1,c2,c3,c4,c5 from stab1 group by c2,c3,c4,c5,c1 intersect select c1,c2,c3,c4,c5 from stab2 group by c2,c3,c4,c5,c1 having count(*) < 3) and c1 = 1;
  • 13. 4/9/15 13 Usage Inside the Procedure create procedure p1_1() returning int; define ret_val int; define row_val int; let ret_val = 0; foreach select intcol into row_val from tab1 intersect select intcol2 from tab2 let ret_val = ret_val + 1; end foreach return ret_val; end procedure;
  • 14. 4/9/15 14 Usage Cross database and Cross server select intcol2, charcol2 from tab2 minus (select intcol3, charcol3 from db2:tab3 intersect select intcol, charcol from db3@serv3:tab1);
  • 15. Set Operators Optimization ‱INTERSECT – rows common to both arms – internally transformed into EXISTS subquery with special NULL handling ‱MINUS or EXCEPT – rows in first arm that’s not in second arm – internally transformed into NOT EXISTS subquery with special NULL handling
  • 16. Nested Loop – Semi Join ‱Execute subquery as a variation of nested-loop join ‱Semi Join- read inner table only until server finds a match – for each row in the outer table, the inner table contributes at most one row ‱Anti Semi Join – return all non-matching rows from inner table
  • 17. Set Operations in explain QUERY: ------ select intcol from tab1 intersect select intcol2 from tab2 Estimated Cost: 4 Estimated # of Rows Returned: 1 1) informix.tab1: SEQUENTIAL SCAN 2) informix.tab2: SEQUENTIAL SCAN (First Row) Filters: informix.tab1.intcol == informix.tab2.intcol2 NESTED LOOP JOIN (Semi Join)
  • 18. Set Operations in explain QUERY: ------ select intcol, charcol from tab1 intersect select intcol2, charcol2 from tab2 minus select intcol3, charcol3 from tab3 Estimated Cost: 6 Estimated # of Rows Returned: 1 1) informix.tab1: SEQUENTIAL SCAN 2) informix.tab2: SEQUENTIAL SCAN (First Row) Filters: (informix.tab1.intcol == informix.tab2.intcol2 AND informix.tab1.charcol == informix.tab2.charcol2 ) NESTED LOOP JOIN (Semi Join) 3) informix.tab3: SEQUENTIAL SCAN (First Row) Filters: (informix.tab1.charcol == informix.tab3.charcol3 AND informix.tab1.intcol == informix.tab3.intcol3 ) NESTED LOOP JOIN (Anti Semi Join)
  • 19. Scenarios This INTERSECT query example finds suppliers who have placed an order. select supplier_id from suppliers INTERSECT select supplier_id from orders; This MINUS query example finds suppliers who have not placed any order. select supplier_id from suppliers MINUS select supplier_id from orders;
  • 20. ANSI Join improvements ‱ Join Directives supported in ANSI queries – ORDERED directive not allowed. ‱ HASH Join Support – Support for Bushy tree and Right deep tree execution. ‱ Optimizer changes to allow comparison between Nested Loop and Hash Joins.
  • 21. Hash Join Support in ANSI JOIN ‱ Without Hash join support, only way to execute joins on large tables without index is to create DYNAMIC index followed by Nested Loop join. ‱ Hash join can be faster for large joins ‱ Optimizer costing is adjusted for situation where build/probe sides for hash join can be composite
  • 22. Hash Join for ANSI JOIN in sqexplain QUERY: ------ select * from (t1 left join t2 on t1.a = t2.a ) left join (t3 inner join t4 on t3.a = t4.a) on t4.a = t1.a 1) informix.t1: SEQUENTIAL SCAN 2) informix.t2: INDEX PATH (1) Index Name: informix.ind2 Index Keys: a (Serial, fragments: ALL) Lower Index Filter: informix.t1.a = informix.t2.a ON-Filters:informix.t1.a = informix.t2.a NESTED LOOP JOIN(LEFT OUTER JOIN) 3) informix.t3: SEQUENTIAL SCAN 4) informix.t4: INDEX PATH (1) Index Name: informix.ind4 Index Keys: a (Serial, fragments: ALL) Lower Index Filter: informix.t3.a = informix.t4.a ON-Filters:informix.t3.a = informix.t4.a NESTED LOOP JOIN ON-Filters:informix.t4.a = informix.t1.a DYNAMIC HASH JOIN (LEFT OUTER JOIN) Dynamic Hash Filters: informix.t4.a = informix.t1.a