This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
How to evaluate the cost and size of other RAoperations.operations.How pipelining can be used to improve efficiencyof queriesof queries.Difference between materialization and
i li ipipelining.Advantages of left-deep trees.Approaches to finding optimal executionstrategy.gyHow Oracle handles QO.
In network and hierarchical DBMSs, low-levelprocedural query language is generally embeddedprocedural query language is generally embeddedin high-level programming language.Programmer’s responsibility to select mostProgrammer s responsibility to select mostappropriate execution strategy.With d l ti l h SQLWith declarative languages such as SQL, userspecifies what data is required rather than how iti t b t i dis to be retrieved.Relieves user of knowing what constitutes goodexecution strategy.
Also gives DBMS more control over systemperformance.performance.
Two main techniques for query optimization:q q y p– heuristic rules that order operations in a query;
comparing different strategies based on relative– comparing different strategies based on relative costs, and selecting one that minimizes resource usageusage.
Disk access tends to be dominant cost in queryDisk access tends to be dominant cost in query processing for centralized DBMS.
Activities involved in retrieving data from thedatabasedatabase.
Aims of QP:Aims of QP:
– transform query written in high-level language(e.g. SQL), into correct and efficient executionstrategy expressed in low-level languagegy p g g(implementing RA);
execute strategy to retrieve required data– execute strategy to retrieve required data.
Activity of choosing an efficient executionstrategy for processing query.strategy for processing query.
As there are many equivalent transformations ofhi h l l i f QO i hsame high-level query, aim of QO is to choose one
that minimizes resource usage.Generally, reduce total execution time of query.May also reduce response time of query.May also reduce response time of query.Problem computationally intractable with largenumber of relations so strategy adopted isnumber of relations, so strategy adopted isreduced to finding near optimum solution.
Cartesian product and join operations muchmore expensive than selection, and third optionp , psignificantly reduces size of relations being joinedtogether.g
Two times when first three phases of QP can be carried out:carried out:– dynamically every time query is run;
t ti ll h i fi t b itt d– statically when query is first submitted. Advantages of dynamic QO arise from fact thatinformation is up to date.Disadvantages are that performance of query isg p q yaffected, time may limit finding optimumstrategy.gy
Aims are to transform high-level query into RAquery and check that query is syntactically andquery and check that query is syntactically andsemantically correct.Typical stages are:Typical stages are:– analysis,– normalization,– semantic analysis,y ,– simplification,
Finally, query transformed into some internalrepresentation more suitable for processing.representation more suitable for processing.Some kind of query tree is typically chosen,constructed as follows:constructed as follows:– Leaf node created for each base relation.– Non-leaf node created for each intermediate
relation produced by RA operation.– Root of tree represents query result.– Sequence is directed from leaves to rootSequence is directed from leaves to root.
Rejects normalized queries that are incorrectlyformulated or contradictory.formulated or contradictory.Query is incorrectly formulated if componentsdo not contribute to generation of resultdo not contribute to generation of result.Query is contradictory if its predicate cannot be
ti fi d b t lsatisfied by any tuple.Algorithms to determine correctness exist onlyfor queries that do not contain disjunction andnegation.
Create node for each relation and node forresult. Create edges between two nodes thatresult. Create edges between two nodes thatrepresent a join, and edges between nodes thatrepresent projectionrepresent projection.
If not connected, query is incorrectly formulated.24
Create node for each reference to an attribute, orconstant 0constant 0.
Create directed edge between nodes that representj i d di t d d b t tt ib t da join, and directed edge between attribute node
and 0 node that represents selection.
Weight edges a → b with value c, if it representsinequality condition (a ≤ b + c); weight edges 0 → ainequality condition (a ≤ b c); weight edges 0 → awith -c, if it represents inequality condition (a ≥ c).
If graph has cycle for which valuation sum isIf graph has cycle for which valuation sum isnegative, query is contradictory.
SELECT p.propertyNo, p.streetFROM Client c, Viewing v, PropertyForRent p, g , p y pWHERE c.clientNo = v.clientNo AND
c.maxRent >= 500 ANDc.maxRent > 500 AND
c.prefType = ‘Flat’ AND p.ownerNo = ‘CO93’;
Relation connection graph not fully connected, sog p y ,query is not correctly formulated.Have omitted the join condition (v propertyNo =Have omitted the join condition (v.propertyNo =p.propertyNo) .
SELECT p.propertyNo, p.streetFROM Client c, Viewing v, PropertyForRent p, g , p y pWHERE c.maxRent > 500 AND
c.clientNo = v.clientNo ANDc.clientNo v.clientNo ANDv.propertyNo = p.propertyNo ANDc prefType = ‘Flat’ AND c maxRent < 200;c.prefType = Flat AND c.maxRent < 200;
Normalized attribute connection graph has cycleNormalized attribute connection graph has cyclebetween nodes c.maxRent and 0 with negativevaluation sum so query is contradictoryvaluation sum, so query is contradictory.
– Detects redundant qualifications,– eliminates common sub-expressions– eliminates common sub-expressions,– transforms query to semantically equivalent
b t il d ffi i tl t d fbut more easily and efficiently computed form.Typically, access restrictions, view definitions,and integrity constraints are considered.Assuming user has appropriate access privileges,g pp p p g ,first apply well-known idempotency rules ofboolean algebra.g
Commutativity of Selection and Theta join (orCartesian product).Cartesian product).
If l ti di t i l l tt ib t fIf selection predicate involves only attributes ofone of join relations, Selection and Join (orC t i d t) ti tCartesian product) operations commute:
If selection predicate is conjunctive predicatehaving form (p ∧ q) where p only involveshaving form (p ∧ q), where p only involvesattributes of R, and q only attributes of S,S l ti d Th t j i ti tSelection and Theta join operations commute as:
σ (R S) = (σ (R)) (σ (S))σp ∧ q(R r S) = (σp(R)) r (σq(S))
Commutativity of Projection and Theta join (orCartesian product).p )
If projection list is of form L = L ∪ L where LIf projection list is of form L = L1 ∪ L2, where L1only has attributes of R, and L2 only hasattributes of S provided join condition onlyattributes of S, provided join condition onlycontains attributes of L, Projection and Thetajoin commute:join commute:
Π (R S) = (Π (R)) (Π (S))ΠL1∪L2(R r S) (ΠL1(R)) r (ΠL2(S))
If join condition contains additional attributesnot in L (M = M ∪ M where M only hasnot in L (M = M1 ∪ M2 where M1 only hasattributes of R, and M2 only has attributes of S),
fi l j i i i i da final projection operation is required:
Perform Selection operations as early as possible.
K di t l ti t th– Keep predicates on same relation together.
Combine Cartesian product with subsequentCombine Cartesian product with subsequentSelection whose predicate represents joincondition into a Join operationcondition into a Join operation.
Use associativity of binary operations torearrange leaf nodes so leaf nodes with mostrestrictive Selection operations executed first.restrictive Selection operations executed first.
K j ti tt ib t l ti t th– Keep projection attributes on same relation together.
Compute common expressions once.Compute common expressions once.
– If common expression appears more than once, andresult not too large store result and reuse it whenresult not too large, store result and reuse it whenrequired.
– Useful when querying views, as same expression is usedto construct view each time.
Many different ways of implementing RAoperations.operations.Aim of QO is to choose most efficient one.U f l th t ti t t f b fUse formulae that estimate costs for a number ofoptions, and select one with lowest cost.Consider only cost of disk access, which is usuallydominant cost in QP.Many estimates are based on cardinality of therelation, so need to be able to estimate this.,
Predicate may be simple or composite.Number of different implementations dependingNumber of different implementations, dependingon file structure, and whether attribute(s)involved are indexed/hashedinvolved are indexed/hashed.Main strategies are:– Linear Search (Unordered file, no index).– Binary Search (Ordered file, no index).y ( , )– Equality on hash key.
Equality condition on primary key– Equality condition on primary key.
May need to scan each tuple in each block tocheck whether it satisfies predicate.check whether it satisfies predicate.For equality condition on key attribute, costestimate is:estimate is:
[nBlocks(R)/2][ ( ) ]
For any other condition, entire file may need to besearched so more general cost estimate is:searched, so more general cost estimate is:
Can first use index to locate record satisfyingpredicate (A = x)predicate (A = x).
Provided index is sorted, records can be found byaccessing all records before/after this one.
Assuming uniform distribution, would expectAssuming uniform distribution, would expecthalf the records to satisfy inequality, so estimatedcost is:cost is:
Second term is estimate of number of blocks thatSecond term is estimate of number of blocks thatwill be required to store number of tuples thatsatisf eq alit condition represented as SC (R)satisfy equality condition, represented as SCA(R).
Inequality Condition on a Secondary B+-q y yTree Index
From leaf nodes of tree, can scan keys fromsmallest value up to x (< or <= ) or from x up tosmallest value up to x (< or <= ) or from x up tomaximum value (> or >=).
Assuming uniform distribution, would expecthalf the leaf node blocks to be accessed and, viaindex, half the file records to be accessed.
Composite Predicates - Conjunction p jwithout Disjunction
May consider following approaches:If one attribute has index or is ordered can use one of- If one attribute has index or is ordered, can use one of
above selection strategies. Can then check each retrievedrecordrecord.
- For equality on two or more attributes, with compositeindex (or hash key) on combined attributes can searchindex (or hash key) on combined attributes, can searchindex directly.
With d i d tt ib t- With secondary indexes on one or more attributes(involved only in equality conditions in predicate), could
se record pointers if e istuse record pointers if exist.
Composite Predicates - Selections with pDisjunction
If one term contains an ∨ (OR), and term requireslinear search entire selection requires linearlinear search, entire selection requires linearsearch.
Only if index or sort order exists on every termcan selection be optimized by retrieving recordsp y gthat satisfy each condition and applying unionoperator.operator.
Simplest join algorithm is nested loop that joinstwo relations together a tuple at a time.two relations together a tuple at a time.Outer loop iterates over each tuple in R, andinner loop iterates over each tuple in Sinner loop iterates over each tuple in S.As basic unit of reading/writing is a disk block,b tt t h t t l th tbetter to have two extra loops that processblocks.Estimated cost of this approach is:
Could read as many blocks as possible of smallerrelation, R say, into database buffer, saving onerelation, R say, into database buffer, saving oneblock for inner relation and one for result.New cost estimate becomes:New cost estimate becomes:
If have index (or hash function) on joinattributes of inner relation, can use indexattributes of inner relation, can use indexlookup.For each tuple in R use index to retrieveFor each tuple in R, use index to retrievematching tuples of S.C t f i R i Bl k (R) b fCost of scanning R is nBlocks(R), as before.Cost of retrieving matching tuples in S dependson type of index and number of matching tuples.If join attribute A in S is PK, cost estimate is:j ,
For Equijoins, most efficient join is when bothrelations are sorted on join attributesrelations are sorted on join attributes.
Can look for qualifying tuples merging relations.
May need to sort relations first.
Now tuples with same join value are in orderNow tuples with same join value are in order.
If assume join is *:* and each set of tuples withsame join value can be held in database buffer atsame time, then each block of each relation need,only be read once.
For Natural or Equijoin, hash join may be used.Idea is to partition relations according to someIdea is to partition relations according to somehash function that provides uniformity andrandomnessrandomness.Each equivalent partition should hold same
l f j i tt ib t lth h it h ldvalue for join attributes, although it may holdmore than one value.Cost estimate of hash join as:
To implement projection need to:– remove attributes that are not required;– remove attributes that are not required;– eliminate any duplicate tuples produced from
i t O l i d if j tiprevious step. Only required if projectionattributes do not include a key.
Two phases: partitioning and duplicateeliminationelimination.
In partitioning phase, for each tuple in R,remove unwanted attributes and apply hashfunction to combination of remaining attributes,and write reduced tuple to hashed value.
Two tuples that belong to different partitions areTwo tuples that belong to different partitions areguaranteed not to be duplicates.
Can be implemented by sorting both relations onsame attributes and scanning through each ofsame attributes, and scanning through each ofsorted relations once to obtain desired result.
Could use sort-merge join as basis.
Estimated cost in all cases is:Estimated cost in all cases is:
As duplicates are eliminated when performingUnion difficult to estimate cardinality but canUnion, difficult to estimate cardinality, but cangive an upper and lower bound as:
Fundamental to efficiency of QO is the searchspace of possible execution strategies and thespace of possible execution strategies and theenumeration algorithm used to search this space.
Query with 2 joins gives 12 join orderings:
R (S T) R (T S) (S T) R (T S) RR (S T) R (T S) (S T) R (T S) R
S (R T) S (T R) (R T) S (T R) S
T (R S) T (S R) (R S) T (S R) TT (R S) T (S R) (R S) T (S R) T
With n relations, (2(n – 1))!/(n – 1)! orderings., ( ( )) ( ) g
If n = 4 this is 120; if n = 10 this is > 176 billion.
Materialization - output of one operation isstored in temporary relation for processing bystored in temporary relation for processing bynext.Could also pipeline results of one operation toCould also pipeline results of one operation toanother without creating temporary relation.K i li i th fl iKnown as pipelining or on-the-fly processing.Pipelining can save on cost of creatingtemporary relations and reading results back inagain.Generally, pipeline is implemented as separateprocess or thread.
With linear trees, relation on one side of eachoperator is always a base relation.operator is always a base relation.However, as need to examine entire inner relationfor each tuple of outer relation inner relationsfor each tuple of outer relation, inner relationsmust always be materialized.Thi k l ft d t li iThis makes left-deep trees appealing as innerrelations are always base relations.Reduces search space for optimum strategy, andallows QO to use dynamic processing.Not all execution strategies are considered.
Term physical operator refers to specificalgorithm that implements a logical operationalgorithm that implements a logical operation,such as selection or join.
For example, can use sort-merge join toimplement the join operation.
Replacing logical operations in a R.A.T. withphysical operators produces an execution strategyphysical operators produces an execution strategy(or query evaluation plan or access plan).
Restriction 1: Unary operations processed on-the-fly: selections processed as relations are
d f fi i j i daccessed for first time; projections processed asresults of other operations are generated.R i i 2 C i dRestriction 2: Cartesian products are neverformed unless query itself specifies one.R i i 3 I d f h j i iRestriction 3: Inner operand of each join is abase relation, never an intermediate result. Thisuses fact that with left deep trees inner operand isuses fact that with left-deep trees inner operand isa base relation and so already materialized.
R i i 3 l d l i iRestriction 3 excludes many alternative strategiesbut significantly reduces number to be considered.
Enumeration of left-deep trees using dynamicprogramming first proposed for System R QOprogramming first proposed for System R QO.
Algorithm based on assumption that the costmodel satisfies principle of optimality.
Thus, to obtain optimal strategy for query with nus, to obta opt a st ategy o que y w t njoins, only need to consider optimal strategies forsubexpressions with (n – 1) joins and extend thosesubexpressions with (n – 1) joins and extend thosestrategies with an additional join. Remaining
b ti l t t i b di d dsuboptimal strategies can be discarded.
To ensure some potentially useful strategies arenot discarded algorithm retains strategies withnot discarded algorithm retains strategies withinteresting orders: an intermediate result has ani t ti d if it i t d b fi l ORDERinteresting order if it is sorted by a final ORDERBY attribute, GROUP BY attribute, or anyattributes that participate in subsequent joins.
SELECT p.propertyNo, p.streetFROM Client c Viewing v PropertyForRent pFROM Client c, Viewing v, PropertyForRent pWHERE c.maxRent < 500 AND
c.clientNo = v.clientNo AND v.propertyNo = p.propertyNo;p p y p p p y ;
Attributes c.clientNo, v.clientNo, v.propertyNo,and p propertyNo are interestingand p.propertyNo are interesting.If any intermediate result is sorted on any of theseattributes then corresponding partial strategyattributes, then corresponding partial strategymust be included in search.
Algorithm proceeds from the bottom up and constructs all alternative join trees that satisfy theconstructs all alternative join trees that satisfy the restrictions above, as follows:Pass 1: Enumerate the strategies for each basePass 1: Enumerate the strategies for each base relation using a linear search and all available indexes on the relation These partial strategiesindexes on the relation. These partial strategies are partitioned into equivalence classes based on any interesting orders An additional equivalenceany interesting orders. An additional equivalence class is created for the partial strategies with no interesting orderinteresting order.
For each equivalence class, strategy with lowest cost is retained for consideration in next pass.cost is retained for consideration in next pass. Do not retain equivalence class with no interesting order if its lowest cost strategy is not lower thanorder if its lowest cost strategy is not lower than all other strategies. F i l ti R l ti i l iFor a given relation R, any selections involving only attributes of R are processed on-the-fly. Si il l tt ib t f R th t t t fSimilarly, any attributes of R that are not part of the SELECT clause and do not contribute to any
b t j i b j t d t t thi tsubsequent join can be projected out at this stage (restriction 1 above).
Pass 2: Generate all 2-relation strategies byconsidering each strategy retained after Pass 1 asconsidering each strategy retained after Pass 1 asouter relation, discarding any Cartesian productsgenerated (restriction 2 above). Again, any on-the-generated (restriction 2 above). Again, any on thefly processing is performed and lowest coststrategy in each equivalence class is retained.strategy in each equivalence class is retained.Pass n: Generate all n-relation strategies byconsidering each strategy retained after Pass (nconsidering each strategy retained after Pass (n –1) as outer relation, discarding any Cartesianproducts generated After pruning now haveproducts generated. After pruning, now havelowest overall strategy for processing the query.
Although algorithm is still exponential, there arequery forms for which it only generates O(n3)query forms for which it only generates O(n )strategies, so for n = 10 the number is 1,000, whichis significantly better than the 176 billion differentis significantly better than the 176 billion differentjoin orders noted earlier.
Based on constraints specified on the databaseschema to reduce the search space.schema to reduce the search space.For example, a constraint states that staff cannotsupervise more than 100 properties so any querysupervise more than 100 properties, so any querysearching for staff who supervise more than 100properties will produce zero rows Now consider:properties will produce zero rows. Now consider:CREATE ASSERTION ManagerSalary
CHECK ( l > 20000 AND iti ‘M ’)CHECK (salary > 20000 AND position = ‘Manager’)SELECT s.staffNo, fName, lName, propertyNoFROM S ff P F RFROM Staff s, PropertyForRent pWHERE s.staffNo = p.staffNo AND
Oracle supports two approaches to queryoptimization: rule-based and cost-based.optimization: rule based and cost based.
R le basedRule-based15 rules, ranked in order of efficiency. Particularaccess path for a table only chosen if statementcontains a predicate or other construct thatmakes that access path available.Score assigned to each execution strategy usingg gy gthese rankings and strategy with best (lowest)score selected.
When 2 strategies have same score, tie-breakresolved by making decision based on order inresolved by making decision based on order inwhich tables occur in the SQL statement.
SELECT propertyNoFROM PropertyForRentp yWHERE rooms > 7 AND city = ‘London’
Single-column access path using index on city fromSingle column access path using index on city fromWHERE condition (city = ‘London’). Rank 9.Unbounded range scan using index on rooms fromg gWHERE condition (rooms > 7). Rank 11.Full table scan - rank 15.Although there is index on propertyNo, column does notappear in WHERE clause and so is not considered bypp yoptimizer.Based on these paths, rule-based optimizer will choose to
To improve QO, Oracle introduced cost-basedoptimizer in Oracle 7, which selects strategy thatp , gyrequires minimal resource use necessary toprocess all rows accessed by query (avoidingp ocess ows ccessed by que y ( vo d gabove tie-break anomaly).User can select whether minimal resource usageUser can select whether minimal resource usageis based on throughput or based on response time,by setting the OPTIMIZER MODE initializationby setting the OPTIMIZER_MODE initializationparameter.C t b d ti i l t k i tCost-based optimizer also takes intoconsideration hints that the user may provide.
Cost-based optimizer depends on statistics for alltables, clusters, and indexes accessed by query.tables, clusters, and indexes accessed by query.Users’ responsibility to generate these statisticsand keep them currentand keep them current.Package DBMS_STATS can be used to generate
d t ti tiand manage statistics.Whenever possible, Oracle uses a parallel methodto gather statistics, although index statistics arecollected serially.EXECUTE
Previously made assumption that data valueswithin columns of a table are uniformlywithin columns of a table are uniformlydistributed.
Histogram of values and their relativefrequencies gives optimizer improved selectivityq g p p yestimates in presence of non-uniformdistributiondistribution.
Histogram is data structure that can improveestimates of number of tuples in result.estimates of number of tuples in result.Two types of histogram:
width balanced histogram which divides data into a– width-balanced histogram, which divides data into afixed number of equal-width ranges (called buckets)each containing count of number of values fallingeach containing count of number of values fallingwithin that bucket;
– height-balanced histogram, which placesg g , papproximately same number of values in each bucketso that end points of each bucket are determined byhow many values are in that bucket.