Presentation is loading. Please wait.

Presentation is loading. Please wait.

Query Folding Xiaolei Qian Presented by Ram Kumar Vangala.

Similar presentations


Presentation on theme: "Query Folding Xiaolei Qian Presented by Ram Kumar Vangala."— Presentation transcript:

1 Query Folding Xiaolei Qian Presented by Ram Kumar Vangala

2 Query Folding Query Folding refers to the activity of determining if and how a query can be answered using a given set of resources. Query Folding refers to the activity of determining if and how a query can be answered using a given set of resources. Resources can be views or cached results of previous queries. Resources can be views or cached results of previous queries.

3 Why Query Folding The base relation referred to in a query might be stored remotely and accessing it might be expensive The base relation referred to in a query might be stored remotely and accessing it might be expensive Accessing the database might not be possible because of network problem( disconnected). Accessing the database might not be possible because of network problem( disconnected). Database might be conceptual but not physically available. Database might be conceptual but not physically available.

4 Query folding Used for Query optimization in centralized database Query optimization in centralized database Query processing in distributed database Query processing in distributed database Query answering in federated database. Query answering in federated database.

5 Example Patients (patient_id, clinic,dob,insurance) Patients (patient_id, clinic,dob,insurance) Physician (physician_id,clinic,pager_no) Physician (physician_id,clinic,pager_no) Drugs (drug_name,generic) Drugs (drug_name,generic) Notes (note_id,patient_id,physican_id,note_text) Notes (note_id,patient_id,physican_id,note_text) Allergy (note_id,drug_name,allergy_text) Allergy (note_id,drug_name,allergy_text) Prescription (note_id,drug_name,prescription_text) Prescription (note_id,drug_name,prescription_text)

6 Suppose that the database maintains materialized views defined as Suppose that the database maintains materialized views defined as CREATE VIEW Drug_Allergy (patient_id,drug_name,text) SELECT patient_id, drug_name, allergy_text FROM Notes, Allergy WHERE Notes.note_id=Allergy.note_id CREATE VIEW Drug_Allergy (patient_id,drug_name,text) SELECT patient_id, drug_name, allergy_text FROM Notes, Allergy WHERE Notes.note_id=Allergy.note_id

7 General query A user might use the following query to get the patient ids who are allergic to drug xd_2001. A user might use the following query to get the patient ids who are allergic to drug xd_2001. SELECT patient_id,allergy_text FROM Patients,Notes, Allergy WHERE Patients.patients_id=Notes.patient_id AND Notes.note_id=Allergy.note_id AND clinic=palo_alto AND drug_name=xd_2001 SELECT patient_id,allergy_text FROM Patients,Notes, Allergy WHERE Patients.patients_id=Notes.patient_id AND Notes.note_id=Allergy.note_id AND clinic=palo_alto AND drug_name=xd_2001

8 Folded Query Using View SELECT patient_id,text FROM Patients, Drug_Allergy WHERE Patients.patient_id=Drug_Allergy.pat ient_id AND clinic=palo_alto AND drug_name= xd_2001 SELECT patient_id,text FROM Patients, Drug_Allergy WHERE Patients.patient_id=Drug_Allergy.pat ient_id AND clinic=palo_alto AND drug_name= xd_2001 This query is more efficient than the original query This query is more efficient than the original query

9 Query containment is special case of Query folding Query containment is special case of Query folding The problem of containment for conjunctive queries is known as NP- complete. The problem of containment for conjunctive queries is known as NP- complete. NP-Complete: Toughest problems which do not have perfect solution NP-Complete: Toughest problems which do not have perfect solution

10 Conjunctive Queries Queries which are result of project- select-join where the selection condition are restricted to equality. Queries which are result of project- select-join where the selection condition are restricted to equality. Conjunctive Query form: Conjunctive Query form: h:- p 1,…….,p n Where h,p1,..,pn are atomic formulas whose arguments are variables or constants, h is the head, and p 1,…,p n is the body.

11 Variables in the head are distinguished and also appear in the body. Variables in the head are distinguished and also appear in the body. X, Y  distinguished variables X, Y  distinguished variables W, U  other variables W, U  other variables A, B  constants A, B  constants Example of conjunctive query Example of conjunctive query q(X,Y) :- patients(X,palo_alto,W 1,W 2 ), notes(W 3,X,W 4,W 5 ), allergy(W 3,xd_2001,Y) q(X,Y) :- patients(X,palo_alto,W 1,W 2 ), notes(W 3,X,W 4,W 5 ), allergy(W 3,xd_2001,Y)

12 Hypergraph Representation A hypergraph is a set of nodes A hypergraph is a set of nodes A hypergraph is a graph where edges can connect any number of vertices A hypergraph is a graph where edges can connect any number of vertices Conjunctive query can be represented by a hypergraph. Conjunctive query can be represented by a hypergraph. A conjunctive query is said to be acyclic if its hypergraph is acyclic. A conjunctive query is said to be acyclic if its hypergraph is acyclic. Example: Example: q(X,Y):- notes(W 1,X,W 2,W 3 ), allergy(W 1,Y,W 4 ), notes(W 5,X,W 6,W 7 ), prescription(W 5,Y,W 8 ) q(X,Y):- notes(W 1,X,W 2,W 3 ), allergy(W 1,Y,W 4 ), notes(W 5,X,W 6,W 7 ), prescription(W 5,Y,W 8 )

13 The example computes patients X and drugs Y such that X is prescribed to Y and is treated with allergy to Y. The example computes patients X and drugs Y such that X is prescribed to Y and is treated with allergy to Y.

14 Query-Folding Problem Folding Rules Folding Rules Let Q be a query, and R={R 1,…,R n } be a set of resources. Let Q be a query, and R={R 1,…,R n } be a set of resources. We assume that no two resources have the same resource predicate, and there are no variables in common between Q and R i or between R i and R j for 1≤i, j≤n We assume that no two resources have the same resource predicate, and there are no variables in common between Q and R i or between R i and R j for 1≤i, j≤n

15 Folding types Partial folding Partial folding Strong folding Strong folding Partial Folding: Partial Folding: A partial folding of Q using R is a conjunctive query Q’ such that Q’ Q and the body of Q’ contains one or more resource predicate defined in R. A partial folding of Q using R is a conjunctive query Q’ such that Q’ Q and the body of Q’ contains one or more resource predicate defined in R.

16 Strong Folding Strong Folding A strong folding of Q using R is a partial folding Q’ of Q using R such that Q Q’ A strong folding of a query is a partial folding that contains the original query.

17 Example: r1(X 1,X 2,X 3 ):- notes(U 1,X 1,U 2,U 3 ), allergy(U 1,X 2,X 3 ) r2(Y 1,Y 2,Y 3,Y 4 ):-notes(V 1,Y 1,Y 2,V 2 ), prescription(V 1,Y 3,V 3 ), drugs(Y 3,Y 4 ). drugs(Y 3,Y 4 ). Where X,Y  distinguished variable U,V  other variables U,V  other variables A complete folding of the above example will be as follows: q(X,Y) :-r 1 (X,Y,W),r 2 (X,W 1,Y,W 2 ).

18 Query Folding Algorithm Let Q be a query, G Q be the hypergraph representing Q, and F be a set of folding rules. Then the query folding algorithm computes complete or partial folding of Q using F. Let Q be a query, G Q be the hypergraph representing Q, and F be a set of folding rules. Then the query folding algorithm computes complete or partial folding of Q using F. Two steps: Two steps: InitializationInitialization Folding GenerationFolding Generation

19 Initialization: Initialization: Compute labels for every hyperedge in G QCompute labels for every hyperedge in G Q Given hyperedge e G Q and conjunct p assosiated with e, its label Le is a relation with attributes var(p). For every F f such that p unifies with head(F). with most general unifier, there is a tuple in Le consisting of two parts: tuple var(p) and expression body (F),where second part is used to store folding of p.Given hyperedge e G Q and conjunct p assosiated with e, its label Le is a relation with attributes var(p). For every F f such that p unifies with head(F). with most general unifier, there is a tuple in Le consisting of two parts: tuple var(p) and expression body (F),where second part is used to store folding of p.

20 Folding Generation Folding Generation Construct set of folding by u-joining the labels of all the hyperedges in an arbitrary order.Construct set of folding by u-joining the labels of all the hyperedges in an arbitrary order.

21 Query Folding for Acyclic Queries Existence of Folding Existence of Folding Pairwise consistency is necessary but not sufficient for the existence of foldings of cyclic queries.Pairwise consistency is necessary but not sufficient for the existence of foldings of cyclic queries.Example: q(X,Y):-patients(W 1,W 2,W 3,W 4 ), notes(X,W 1,W 5,Y), physician(W 5,W 2,W 6 ) with resources r 1 (X 1,X 2 ) :-patients(B 1,A 1,U 1,U 2 ), notes(X 1,B 1,C 1,X 2 ),physician(C 1,A 2,U 3 )

22 r 2 (Y 1,Y 2 ):-patients(B 2,A 2,V 1,V 2 ), notes(Y 1,B 2,C 2,Y 2 ), physician(C 2,A 1,V 3 ) Example:

23 Label for hyperedges

24 Theorem: There exists a complete folding of acyclic query Q using folding rules F iff no hyperedges in reduction(G Q ) have empty labels.

25 Example : consider an acyclic query which computes notes from clinics with allergic reactions. Example : consider an acyclic query which computes notes from clinics with allergic reactions. q(X,Y):- allergy(X,W 1,W 2 ), drug(W 1,W 3 ), notes(X,W 4,W 5,W 6 ), patients(W 4,Y,W 7,W 8 ) q(X,Y):- allergy(X,W 1,W 2 ), drug(W 1,W 3 ), notes(X,W 4,W 5,W 6 ), patients(W 4,Y,W 7,W 8 ) Resources: Resources: r1(X 1,X 2 ):- allergy(X 1,U 1,U 2 ),drugs(U 1,X 2 ),notes(X 1,U 3,U 4,U 5 ) r1(X 1,X 2 ):- allergy(X 1,U 1,U 2 ),drugs(U 1,X 2 ),notes(X 1,U 3,U 4,U 5 ) r2(Y 1,Y 2 ):- notes(Y 1,V 1,V 2,V 3 ),patients(V 1,Y 2,V 4,V 5 ),dr ugs(V 6,V 7 ) r2(Y 1,Y 2 ):- notes(Y 1,V 1,V 2,V 3 ),patients(V 1,Y 2,V 4,V 5 ),dr ugs(V 6,V 7 )

26 Folding rules Folding rules

27 Labels for hypergraph

28 Theorem: Theorem: There does not exist a partial folding of acyclic query Q using folding rules F iff every hyperedge in reduction (G Q ) has a singleton label.

29 Resources: Resources: Folding Rules: Folding Rules:

30 Labels to Hypergraph

31 Conclusion Query folding can be used in centralized databases Query folding can be used in centralized databases Queries can be answered using views instead of base relations. Queries can be answered using views instead of base relations. In multiple queries, the result of a query can be used to partially answer another query. In multiple queries, the result of a query can be used to partially answer another query. In client server application, views can be cached. In client server application, views can be cached.

32 Questions?

33 Thank you


Download ppt "Query Folding Xiaolei Qian Presented by Ram Kumar Vangala."

Similar presentations


Ads by Google