



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
A magic sets optimization technique that combines the benefits of both top-down and bottom-up processing of logic, without the disadvantages of either. It provides examples of nonrecursive and recursive use of this technique in datalog and sql. The document also explains the advantages of top-down and bottom-up processing and the use of rule/goal graphs to assure unique binding patterns for idb predicates.
Typology: Slides
1 / 6
This page cannot be seen from the preview
Don't miss anything!




Magic Sets
Optimization technique for recursive Datalog.
Also a win on some nonrecursive SQL (Mumick, Finkelstein, Pirahesh, and Ramakrishnan, 1990 SIGMOD, pp. 247{258).
Combines b ene ts of b oth top-down (backward chaining, recursive tree search) and b ottom-up (forward chaining, naive, seminaive) pro cessing of logic, without disadvantages of either.
Example of Nonrecursive Use
Find the programmers who are making less than the average salary for their department.
SELECT e1.name FROM Emps e WHERE e1.job = 'programmer' AND e1.sal < ( SELECT AVG(e2.sal) FROM Emps e WHERE e2.dept = e1.dept );
Naive implementation computes the average salary for all departments.
\Magic-sets" implementation rst determines the departments that have programmers (p erhaps very few). It can then use an index on Emps.dept to avoid accessing the entire Emps relation.
Recursive Example
anc(X,Y) :- par(X,Y) anc(X,Y) :- par(X,Z) & anc(Z,Y)
Query: anc(0; W ).
Top-down search (e.g., Prolog) would:
Advantage of Top-Down
We never even ask ab out individuals that are not in the ancestry of individual 0.
Advantage of Bottom-Up
(i.e., naive, seminaive)
We don't go into in nite recursive lo ops.
Example
Both of the following Datalog programs lo op if evaluated top-down:
anc(X,Y) :- par(X,Y) anc(X,Y) :- anc(X,Z) & par(Z,Y)
anc(X,Y) :- par(X,Y) anc(X,Y) :- anc(X,Z) & anc(Z,Y)
Key Magic-Sets Ideas
a) Predicate splitting : an IDB predicate must b e \called" (in top-down search) with only one binding pattern.
b) Subgoal recti cation : avoid IDB subgoals with rep eated variables.
Rule/Goal Graphs
Needed to assure unique binding patterns for IDB predicates.
Comp osed of rule and goal nodes, as follows.
Goal No des
Predicate + \adornment."
Adornment = list of b's and f 's, indicating which arguments are b ound, which are free.
Example: pbf^ b^. First and third arguments of p are b ound.
2
Query form pbf^ , e.g., p(0; W )?
pbf
r [ 1 X: 0 jY^ ;Z^ ]
q bf^ r [ 1 X;Z: 1 jY^ ]
r bf
r [ 2 A: 0 j B^ ] r [ 3 A: 0 jB^ ]
sbf^ tbf
Recursive Example
r 1 : anc(X,Y) :- par(X,Y) r 2 : anc(X,Y) :- anc(X,Z) & anc(Z,Y)
Query; ancbb^ , e.g., anc(j oe; sue)?
ancbb
r [ 1 X: 0 Y j] r [ 2 X;Y: 0 jZ^ ]
par bb^ ancbf^ r [ 2 X;Y: 1 ;Z^ j]
r X 1 :^0 jY^ ] r [ 2 X: 0 jY^ ;Z^ ]
par bf^ r [ 2 X;Z: 1 jY^ ]
Splitting Predicates
For magic-sets to work, there must b e a unique binding pattern asso ciated with each IDB predicate.
No constraint on EDB predicates.
Key idea: For each adornment such that p app ears in the RGG, make a new predicate
4
p. Rules for p are the same as for p, but predicates of IDB subgoals are the version with the correct binding pattern.
RGG helps us gure out the needed binding patterns.
Example
For RGG ab ove:
anc bb(X,Y) :- par(X,Y) anc bb(X,Y) :- anc bf(X,Z) & anc bb(Z,Y) anc bf(X,Y) :- par(X,Y) anc bf(X,Y) :- anc bf(X,Z) & anc bf(Z,Y)
Rectifying Subgoals
All IDB subgoals must have arguments that are distinct variables.
Feasible for datalog (no function symb ols).
Fixes some problems where RGG knows ab out fewer b ound arguments than the top-down expansion do es. F See p. 801 of PDKS-I I.
Trick: replace an IDB subgoal G with variables app earing in more than one argument and/or constant arguments by a new predicate whose arguments are single copies of the variables app earing in G.
Create rules for the new predicate by unifying G with heads of rules for G's predicate.
Rep etition may b e needed b ecause the resulting rules may have unrecti ed subgoals.
Example
r 1 : p(X,Y) :- a(X,Y) r 2 : p(X,Y) :- b(X,Z) & p(Z,Z) & b(Z,Y)
p(Z ; Z ) is unrecti ed. Create q (Z ) = p(Z ; Z ).
Unify heads of rules with p(Z ; Z ). Careful! Z in b o dy of r 2 must b e renamed.
r 1 b ecomes p(Z,Z) :- a(Z,Z) or
q(Z) :- a(Z,Z)