BY: Mark Gruszecki. What is a Recursive Query? Definition(s) and Algorithm(s) Optimization…

13
BY: Mark Gruszecki

description

CREATE RECURSIVE VIEW R(d, i, j, v) AS ( SELECT 1, i, j, v FROM T UNION ALL SELECT d+1, R.i, T.j, R.v + T.v FROM R JOIN T ON R.j = T.i WHERE d < 8);

Transcript of BY: Mark Gruszecki. What is a Recursive Query? Definition(s) and Algorithm(s) Optimization…

Page 1: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

BY: Mark Gruszecki

Page 2: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

What is a Recursive Query? Definition(s) and Algorithm(s) Optimization Techniques Practical Issues Impact of each Optimization Conclusion

Page 3: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

CREATE RECURSIVE VIEW R(d, i, j, v) AS (SELECT 1, i, j, v FROM TUNION ALLSELECT d+1, R.i, T.j, R.v + T.vFROM R JOIN T ON R.j = T.iWHERE d < 8);

Page 4: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Any kind of Hierarchical Data is more easily accessed using a Recursive Query.

For example, displaying Employees in an orginizational chart.

Page 5: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

A base table T is defined as T(i, j, v) with primary key (i, j) and v representing a numerical value.

Define the result table from a recursive query by R(d, i, j, v) with primary key (d, i, j).

The queries of interest are of the form R[k] = T T T … T where is the recursive query.

Let G = (V, E) be a directed graph with n vertices and m edges.

The algorithm to evaluate optimizations is: R = k

kR ][

Page 6: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

1. Early evaluation of row select conditions.

2. Deleting Duplicate Rows.3. Indexing base and result tables for

efficient joins.

Page 7: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Used on Queries of the form:SELECT i , j, v FROM RWHERE <condition>

Two Cases: If the “WHERE” condition doesn’t participate

in the JOIN condition of the recursion. The “WHERE” condition is part of the JOIN in

the recursion.

Page 8: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Suppose we want to know which vertices of a graph are connected to a specific node (e.g. all dependants of a particular person).

What if G is complete, dense, sparse, or even a tree?

Queries are optimized by deleting duplicate rows at each step.

Page 9: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Two Choices:

Index based on i and j. Gives rise to an optimal Hash Join Cannot uniquely identify rows

Index based on primary key of T and R Can uniquely identify rows

Page 10: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

The possible size of a result table of a recursive query is |T| x |T| x … |T|.

In general, if there are n recursive steps and T has m entries, then there are m^n.

Page 11: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Early Evaluation of Row Selection Conditions Good in all cases

Deleting Duplicate Rows Good in certain cases, but not necessary in all

Indexing Base and Result Tables for Efficient Joins If G is highly connected, slight loss of

performance (Hash Collisions). Otherwise, this is the best optimization.

Page 12: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Recursive Queries are an important tool to be exposed to when dealing with databases.

There are three types of optimizations of these queries discussed: Early evaluation of row selections Deleting duplicate rows Indexing for more efficient joins

Every optimization is good, but some are more applicable in certain circumstances than others.

Page 13: BY: Mark Gruszecki.  What is a Recursive Query?  Definition(s) and Algorithm(s)  Optimization…

Carlos Ordonez (2005) Optimizing Recursive Queries in SQL. Retrieved February 10, 2009 from IEEE Computer Science Digital Library

http://publib.boulder.ibm.com/infocenter/iseries/v5r4/index.jsp?topic=/rzajq/rzajqrcteexample.htm