Docsity

Query optimization in distributed database management system, Summaries of Distributed Database Management Systems

Partial preview of the text

Download Query optimization in distributed database management system and more Summaries Distributed Database Management Systems in PDF only on Docsity!

Introduction:

SQL one of the success factors of the RDBMS:

As we know, there are three basic components of the data model and one of them is manipulation language and SQL has been accepted as the standard language for RDBMS. As SQL has support of some mathematical languages and it is quite simple and useful language and kind of popular language used to interact with RDBMS. That is why it is considered as the success factor of the RDBMS. Query processor transforms complex queries into concise and simple ones. Query processing is critical performance issue we will realize why it is critical performance once we solved the examples and how the required resources are affected as we change the adjustment of different queries. The QP is an important factor in the success of any application or DBMS. QP is a complex process in centralized but more complex problem especially in DDBS environment as more factors are involved.

Main function of QP is to transform an SQL query into equivalent relational algebra one (low

level language) and Transformation must achieve correctness and efficiency. It means QP will adjust the SQL commands to make them more concise and efficient and then it will transform them into low-level language i.e. Relational Algebra Expression because relation algebra language is closer to the actual operations that are performed in the database.

Correctness is straightforward since rules exist:

As we know the transformation must have two factors i.e. ● Correctness ● Efficiency Correctness c an be achieved automatically as for transformation we use certain mathematical properties. As we use these properties the transformation will be correct automatically. As for Efficiency, an SQL query can have many equivalents in R (Relational) Algebra. As we get multiple relational algebra expression against a single SQL query, the level of each R algebra expression is different. So, in QP we will select that relational algebra expression which is more efficient than others to be executed actually. By level means the cost to execute the query. QP will execute the R algebra expression which is least costly. However, all the R algebra expressions are correct as these transformations are governed by the mathematic rules. So, in all these correct expressions we will choose the one which use less resources and will give you instant response.

Example No.1:

Considering the tables

ΠeName (EMP ⋈ENO (σresp = ‘Manager’)

(ASG)

We assume that relations EMP and ASG are horizontally fragmented as follows:

EMP1 = σENO ≤ “E3” (EMP)

EMP2 = σENO >“E3” (EMP)

ASG1 = σENO ≤ “E3” (ASG)

ASG2 = σENO >“E3” (ASG)

Fragments ASG1, ASG2, EMP1, and EMP2 are stored at sites 1, 2, 3, and 4, respectively, and the result is expected at site 5.

Strategy A:

Strategy A exploits the fact that relations EMP and ASG are fragmented the same way in order to perform the select and join operator in parallel.

Strategy B:

Strategy B is a default strategy (always works) that simply centralizes all the operand data at the result site before processing the query.

Cost of both Strategies:

To evaluate the resource consumption of these two strategies, we use a very simple cost model. We assume that a tuple access, denoted by Tupac, is 1 unit (which we leave unspecified) and a tuple transfer, denoted turbans, is 10 units. We assume that relations EMP and ASG have 400 and 1000 tuples, respectively, and that there are 20 managers in relation ASG. We also assume that data is uniformly distributed among sites. Finally, we assume that relations ASG and EMP are locally clustered on attributes RESP and ENO, respectively. Therefore, there is direct access to tuples of ASG (respectively, EMP) based on the value of attribute RESP (respectively, ENO). Strategy A Cost: produce ASG': 201 20 transfer ASG' to the sites of E: 20 * 10 200 produce EMP': (10+10) 12 40 transfer EMP' to result site: 2010 200 Total 440 Strategy B Cost: Transfer EMP to site 5: 400 * 10 4000 Transfer ASG to the site 5: 1000 * 10 10000 Produce ASG’ by selecting ASG 1000 Join EMP and ASG’ 8000 Total 23000 Conclusion: Just by selecting different strategies for the executing the query see how the cost is affected. In Strategy A we got cost of 460 units and with B we got 23000 units. From this we can see how the QP decision effects the efficiency.

that the cost occurring because of the Exhaustive search is justifiable. For example, if we have to use the query repeatedly then if we do Exhaustive search than it is good as by repeated execution of that query will provide efficiency. So, in this case it is justifiable. But in case of the Complex Queries which are not used one or two times and if we do Exhaustive search then it is not justifiable.

  • However, this is one possibility if we do Exhaustive search or not. May be very costly in case of multiple options and more fragments.
  • And in some cases we use Heuristics (we look into the previous records and decides which query cost should be calculated and we don’t use Exhaustive search)

What is the timing for the Optimization?

Optimization Timing

  • Static: during compilation (optimize the query when you are compiling the query). The advantage of the static optimization is that the optimization cost occurs one time and once you have find the optimized solution you can use it repeatedly but the problem is Size of intermediate tables not known always and that is why in some cases it is not that much accurate. If you are doing Exhaustive search and you are doing optimization at compile time, then the Cost justified with repeated execution of this query.
  • Dynamic: during execution (optimize during the execution of the query). Advantage is that the Intermediate tables’ size known. Re-optimization may be required for more efficient strategy because table size changes dynamically which effects the size of intermediate tables requiring re-optimization of same query with different strategies.

Statistics:

It is also used in QO. Some of the information is related to Tables/Relation/Fragment and some is about the attributes.

- Relation/Fragment: Cardinality, size of a tuple, fraction of tuples participating in a join with another relation - Attribute: Cardinality of domain/type, actual number of distinct/unique values. All these statistics are used in QO. Decision Sites (the pattern for deciding and defining the strategy of QP or QO)

  • Centralized site: the central site which we make all the decisions related to optimization. simple, need knowledge about the entire distributed database
  • Distributed: cooperation among sites to determine the schedule, need only local information
  • Hybrid: one site determines the global schedule, the sub queries are transferred on other sites and each site itself optimizes the local sub queries

Other factors like:

  • Network topology
  • Replicated fragments
  • Use of semi joins

Layers of Query Processing:

From here we will highlight or represent the four successive phases of query decomposition : normalization , semantic analysis , simplification , and restructuring of the query. Then, we describe data localization, with emphasis on reduction and simplification technique with the help of Horizontal fragmentation. ● Query Decomposition: Query decomposition is the first phase of query processing that transforms a relational calculus query into a relational algebra query. Both input and output

replicated join and select predicates, as shown in the following example. The reason is that predicates are very often linked with the other predicates by AND. The use of rule 5 mentioned above, with p1 as a join or select predicate, would result in replicating p1. The conjunctive normal form is more practical since query qualifications typically include more AND than OR predicates. However, it leads to predicate replication for queries involving many disjunctions and few conjunctions, a rare case.

2. Analysis: Query analysis enables rejection of normalized queries for which further processing is either impossible or unnecessary. The main reasons for rejection are that the query is type incorrect or semantically incorrect. When one of these cases is detected, the query is simply returned to the user with an explanation. Otherwise, query processing is continued. Below we present techniques to detect these incorrect queries. A query is type incorrect if any of its attribute or relation names are not defined in the global schema, or if operations are being applied to attributes of the wrong type. The technique used to detect type incorrect queries is similar to type checking for programming languages. However, the type declarations are part of the global schema rather than of the query, since a relational query does not produce new types. A query is semantically incorrect if its components do not contribute in any way to the generation of the result. In the context of relational calculus, it is not possible to determine the semantic correctness of general queries. However, it is possible to do so for a large class of relational queries, those which do not contain disjunction and negation this is based on the representation of the query as a graph, called a query graph or connection graph. We define this graph for the most useful kinds of queries involving select, project, and join operators. In a query graph, one node indicates the result relation, and any other node indicates an operand relation. An edge between two nodes one of which does not correspond to the result represents a join, whereas an edge whose destination node is the result represents a project. Furthermore, a non-result node may be labeled by a select or a self-join (join of the relation with itself) predicate. An important sub graph of the query graph is the join graph, in which only the joins are considered. The join graph is particularly useful in the query optimization phase. The query graph is useful to determine the semantic correctness of a conjunctive multivariable query without negation. Such a query is semantically incorrect if its query graph is not connected. In this case one or more sub graphs (corresponding to sub queries) are disconnected from the graph that contains the result relation. The query could be considered correct (which some systems do) by considering the missing connection as a Cartesian product. But, in general, the problem is that join predicates are missing and the query should be rejected. 3) Elimination of Redundancy: One way to simplify a query is to eliminate redundant predicates. Note that redundant queries are likely to arise when a query is the result of system transformations applied to the user query. In particular, a user query typically

expressed on a view may be enriched with several predicates to achieve view-relation correspondence and ensure semantic integrity and security. The enriched query qualification may then contain redundant predicates. A naive evaluation of a qualification with redundancy can well lead to duplicated work. ● Such redundancy and thus redundant work may be eliminated by simplifying the qualification with the following well-known impotency rules:

  1. P∧ p ⇔ p
  2. P∨ p ⇔ p
  3. P∧ true ⇔ p
    1. P∨ false ⇔ p
  4. P∧ false ⇔ false
    1. P∨ true ⇔ true
    2. P∧ ¬p ⇔ false
  5. P∨ ¬p ⇔ true
  6. P1 ∧ (p1 ∨ p2) ⇔ p
  7. P1 ∨ (p1 ∧ p2) ⇔ p 4) Rewriting: The last step of query decomposition rewrites the query in relational algebra. For the sake of clarity it is customary to represent the relational algebra query graphically by an operator tree. An operator tree is a tree in which a leaf node is a relation stored in the database, and a non-leaf node is an intermediate relation produced by a relational algebra operator. The sequence of operations is directed from the leaves to the root, which represents the answer to the query. The transformation of a tuple relational calculus query into an operator tree can easily be achieved as follows. First, a different leaf is created for each different tuple variable (corresponding to a relation). In SQL, the leaves are immediately available in the FROM clause. Second, the root node is created as a project operation involving the result attributes. These are found in the SELECT clause in SQL. Third, the qualification (SQL WHERE clause) is translated into the appropriate sequence of relational operations (select, join, union, etc.) going from the leaves to the root. The sequence can be given directly by the order of appearance of the predicates and operators.

With this transformation, unions can be moved up in the operator tree so that all possible joins of fragments are exhibited. Useless joins of fragments can be determined when the qualifications of the joined fragments are contradicting, thus yielding an empty result. Assuming that fragments RI and Rj are defined, respectively, according to predicates pi and pj on the same attribute, the simplification rule can be stated as follows: Rule 2: Ri ✶ Rj = φ if ∀ x in Ri , ∀ y in Rj : ¬(pi(x) ∧ pj(y)) The determination of useless joins and their elimination using rule 2 can thus be performed by looking only at the fragment predicates. The application of this rule permits the join of two relations to be implemented as parallel partial joins of fragments. It is not always the case that the reduced query is better (i.e., simpler) than the localized query. The localized query is better when there are a large number of partial joins in the reduced query. This case arises when there are few contradicting fragmentation predicates. The worst case occurs when each fragment of one relation must be joined with each fragment of the other relation. This is tantamount to the Cartesian product of the two sets of fragments, with each set corresponding to one relation. The reduced query is better when the number of partial joins is small. For example, if both relations are fragmented using the same predicates, the number of partial joins is equal to the number of fragments of each relation. One advantage of the reduced query is that the partial joins can be done in parallel, and thus increase response time. Search Space: The search space is obtained by applying transformation rules, such as those for relational algebra. Query execution plans are typically abstracted by means of operator trees which define the order in which the operations are executed. They are enriched with additional information, such as the best algorithm chosen for each operation. For a given query, the search space can thus be defined as the set of equivalent operator trees that can be produced using transformation rules. To characterize query optimizers, it is useful to concentrate on join trees, which are operator trees whose operators are join or Cartesian product. This is because permutations of the join order have the most important effect on performance of relational queries. Therefore, query optimizers typically restrict the size of the search space they consider. The first restriction is to use heuristics. The most common heuristic is to perform selection and projection when accessing base relations. Another common heuristic is to avoid Cartesian products that are not required by the query Another important restriction is with respect to the shape of the join tree. Two kinds of join trees are usually distinguished: linear versus bushy trees. A linear tree is a tree such that at least one operand of each operator node is a base relation. A bushy tree is more general and may have operators with no base relations as operands (i.e., both operands are intermediate relations). By

considering only linear trees, the size of the search space is reduced to O (2 N). However, in a distributed environment, bushy trees are useful in exhibiting parallelism.

Search Strategy:

The most popular search strategy used by query optimizers is dynamic programming, which is deterministic. Deterministic strategies proceed by building plans, starting from base relations, joining one more relation at each step until complete plans are obtained Dynamic programming builds all possible plans, breadth first, before it chooses the “best” plan. To reduce the optimization cost, partial plans that are not likely to lead to the optimal plan are pruned (i.e., discarded) as soon as possible. By contrast, another deterministic strategy, the greedy algorithm, builds only one plan, depth-first. Dynamic programming is almost exhaustive and assures that the “best” of all plans is found. It incurs an acceptable optimization cost when the number of relations in the query is small. However, this approach becomes too expensive when the number of relations is greater than 5 or

  1. For more complex queries, randomized strategies have been proposed, which reduce the optimization complexity but do not guarantee the best of all plans. Unlike deterministic strategies, randomized strategies allow the optimizer to trade optimization time for execution time.

Distributed Cost Model:

An optimizer’s cost model includes cost functions to predict the cost of operators, statistics and base data, and formulas to evaluate the sizes of intermediate results. The cost is in terms of execution time, so a cost function represents the execution time of a query. Cost Functions: The cost of a distributed execution strategy can be expressed with respect to either the total time or the response time. The total time is the sum of all time (also referred to as cost) components, while the response time is the elapsed time from the initiation to the completion of the query: Total time = TCPU ∗ #insts+TI/O ∗ #I/Os+TMSG ∗ #msgs+TT R ∗ #bytes The two first components measure the local processing time, where TCPU is the time of a CPU instruction and TI/O is the time of a disk I/O. The communication time is depicted by the two last components. TMSG is the fixed time of initiating and receiving a message, while TT R is the time it takes to transmit a data unit from one site to another. The data unit is given here in terms of bytes (#bytes is the sum of the sizes of all messages), but could be in different units (e.g., packets). A typical assumption is that TT R is constant. This might not be true for wide area

alternative trees is reduced using dynamic programming. The set of alternative strategies is constructed dynamically so that, when two joins are equivalent by commutativity, only the cheapest one is kept. Furthermore, the strategies that include Cartesian products are eliminated whenever possible. To avoid the high cost of exhaustive search, randomized strategies, such as iterative improvement and simulated annealing.