optimizing recursive queries in sql

13
BY: Mark Gruszecki

Upload: xavier-nielsen

Post on 15-Mar-2016

55 views

Category:

Documents


3 download

DESCRIPTION

Optimizing recursive queries in sql. BY: Mark Gruszecki. Overview. What is a Recursive Query? Definition(s) and Algorithm(s) Optimization Techniques Practical Issues Impact of each Optimization Conclusion. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Optimizing recursive queries in  sql

BY: Mark Gruszecki

Page 2: Optimizing recursive queries in  sql

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

Page 3: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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

For example, displaying Employees in an orginizational chart.

Page 5: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

1. Early evaluation of row select conditions.

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

efficient joins.

Page 7: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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: Optimizing recursive queries in  sql

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