Field note

ch16

Query Optimization

Outline:

  • Introduction
  • Transformation of Relational Expressions
  • Statistical Information for Cost Estimation
  • Cost-based Optimization
  • Dynamic Programming for Choosing Evaluation Plans
  • Nested Subqueries
  • Materialized Views
  • Advanced Topics in Query Optimization

Introduction

  • Alternative ways of evaluating a given query

    • Equivalent expressions
      • 逻辑优化, transformation based optimization
    • Different algorithms for each operation
      • 物理优化, cost based optimization
  • Estimation of plan cost based on:

    • Statistical information about relations. Examples:
      • number of tuples, number of distinct values for an attribute
    • Statistics estimation for intermediate results(Cardinality Estimation)
      • to compute cost of complex expressions
    • Cost formulae for algorithms, computed using statistics

Viewing Query Evaluation Plans

如果想要查看数据库系统选择的查询计划,可以使用 EXPLAIN 语句。这个语句会显示出数据库系统在执行查询时所选择的查询计划,包括每个操作的成本估计和执行顺序。

Transformation of Relational Expressions

  • Two relational algebra expressions are said to be equivalent if the two expressions generate the same set of tuples on every legal database instance.
    • 如果两个关系代数表达式在每个合法的数据库实例上生成相同的元组集合,则这两个表达式是等价的。
  • An equivalence says that expressions of two forms are equivalent.
    • Can replace expression of first form by second, or vice versa

Equivalence Rules

Selection Operations

  • Conjunctive selection operations can be deconstructed into a sequence of individual selections.选择操作可以分解为一系列单独的选择操作。
σθ1θ2(E)=σθ1(σθ2(E))\sigma_{\theta_1 \land \theta_2}(E) = \sigma_{\theta_1}(\sigma_{\theta_2}(E))
  • Selection operations are commutative.选择操作是可交换的。
σθ1(σθ2(E))=σθ2(σθ1(E))\sigma_{\theta_1}(\sigma_{\theta_2}(E)) = \sigma_{\theta_2}(\sigma_{\theta_1}(E))
  • Only the last in a sequence of projection operations is needed, the others can be omitted.投影操作中只有最后一个是必要的,其他的可以省略。
πL1(πL2((πLn(E))))=πL1(E)\pi_{L_1}(\pi_{L_2}(\cdots (\pi_{L_n}(E)) \cdots)) = \pi_{L_1}(E)
  • Selections can be combined with Cartesian products and theta joins.选择操作可以与笛卡尔积和 θ\theta 连接结合使用。

    a. σθ(E1×E2)=E1θE2\sigma_{\theta}(E_1 \times E_2) = E_1 \bowtie_{\theta} E_2

    b. σθ1(E1θ2E2)=E1θ1θ2E2\sigma_{\theta_1}(E_1 \bowtie_{\theta_2} E_2) = E_1 \bowtie_{\theta_1 \land \theta_2} E_2

  • Theta-join operations (and natural joins) are commutative. θ\theta 连接操作(和自然连接)是可交换的。

E1θE2=E2θE1E_1 \bowtie_{\theta} E_2 = E_2 \bowtie_{\theta} E_1
  • (a). Natural join operations are associative.自然连接操作是结合的。
(E1E2)E3=E1(E2E3)(E_1 \bowtie E_2) \bowtie E_3 = E_1 \bowtie (E_2 \bowtie E_3)
  • (b). Theta joins are associative in the following manner:
(E1θ1E2)θ2θ3E3=E1θ1θ3(E2θ2E3)(E_1 \bowtie_{\theta_1} E_2) \bowtie_{\theta_2 \land \theta_3} E_3 = E_1 \bowtie_{\theta_1 \land \theta_3} (E_2 \bowtie_{\theta_2} E_3)

: where θ2\theta_2 involves attributes from only E2E_2 and E3E_3

  • The selection operation distributes over the theta join operation under the following two conditions

    a. When all the attributes in θ0\theta_0 involve only the attribute of one of the expressions(E1E_1) begin joined.

    σθ0(E1θE2)=σθ0(E1)θE2\sigma_{\theta_0}(E_1 \bowtie_{\theta} E_2) = \sigma_{\theta_0}(E_1) \bowtie_{\theta} E_2

    b. When θ1\theta_1 involves only the attributes of E1E_1 and θ2\theta_2 involves only the attributes of E2E_2.

    σθ1θ2(E1θE2)=σθ1(E1)θσθ2(E2)\sigma_{\theta_1 \land \theta_2}(E_1 \bowtie_{\theta} E_2) = \sigma_{\theta_1}(E_1) \bowtie_{\theta} \sigma_{\theta_2}(E_2)

Projection Operations

The projection operation distributes over the theta join operation as follows:

a. if θ\theta involves only attributes from L1L2L_1 \cup L_2:

πL1L2(E1θE2)=(πL1(E1)θπL2(E2))\pi_{L_1 \cup L_2}(E_1 \bowtie_{\theta} E_2) = (\pi_{L_1}(E_1) \bowtie_{\theta} \pi_{L_2}(E_2))

b. Consider a join E1θE2E_1 \bowtie_{\theta} E_2.

  • Let L1L_1 and L2L_2 be sets of attributes from E1E_1 and E2E_2, respectively.
  • Let L3L_3 be attributes of E1E_1 that are involved in join condition θ\theta, but are not in L1L2L_1 \cup L_2 and
  • Let L4L_4 be attributes of E2E_2 that are involved in join condition θ\theta, but are not in L1L2L_1 \cup L_2.
πL1L2(E1θE2)=πL1L2(E1)θπL2L4(E2)\pi_{L_1 \cup L_2}(E_1 \bowtie_{\theta} E_2) = \pi_{L_1 \cup L_2}(E_1) \bowtie_{\theta} \pi_{L_2 \cup L_4}(E_2)

Set Operations

  • The set operations union and intersection are commutative
E1E2=E2E1E_1 \cup E_2 = E_2 \cup E_1 E1E2=E2E1E_1 \cap E_2 = E_2 \cap E_1
  • Set union and intersection are associative
(E1E2)E3=E1(E2E3)(E_1 \cup E_2) \cup E_3 = E_1 \cup (E_2 \cup E_3) (E1E2)E3=E1(E2E3)(E_1 \cap E_2) \cap E_3 = E_1 \cap (E_2 \cap E_3)
  • The selection operation distributes over \cup and \cap operations as follows:
σθ(E1E2)=σθ(E1)σθ(E2)\sigma_{\theta}(E_1 - E_2) = \sigma_{\theta}(E_1) - \sigma_{\theta}(E_2)

also: σθ(E1E2)=σθ(E1)E2\sigma_{\theta}(E_1 - E_2) = \sigma_{\theta}(E_1) - E_2 and similarly for \cap in place of -, but not for \cup.

  • The projection operation distributes over union
πLE1E2=πLE1πLE2\pi_{L}{E_1 \cup E_2} = \pi_{L}{E_1} \cup \pi_{L}{E_2}

Other Equivalence Rules

Enumeration of Equivalent Expressions

Statistics for Cost Estimation

估计代价需要以下的信息:

  • nrn_r: number of tuples in a relation rr.
  • brb_r: number of blocks containing tuples of rr.
  • lrl_r: size of a tuple of rr.
  • frf_r: blocking factor of rr - i.e., the number of tuples of rr that fit into ont block.每个块能放几个元组
  • V(A,r)V(A, r): number of distinct values that appear in rr for attribute AA; same as the size of πA(r)\pi_A(r).
  • If tuples of rr are stored together physically in a file, then:
br = nrfrb_r \ = \ \frac{n_r}{f_r}

Selection Size Estimation

  • σA=v(r)\sigma_{A = v}(r)
    • nr/V(A,r)n_r / V(A, r): number of records that will satisfy the selection
    • Equality condition on a key attribute: size estimate = 1
  • σAv(r)\sigma_{A \leq v}(r)
    • Let c denote the estimated number of tuples satisfying the condition.
    • If min(A, r) and max(A, r) are available in catalog
      • c = 0 if v<min(A,r)v < min(A, r)
      • c = nrvmin(A,r)max(A,r)min(A,r)n_r \frac{v - min(A, r)}{max(A, r) - min(A, r)}
    • If histograms available, can refine above estimate
    • In absence of statistical information c is assumed to be nr/2n_r / 2.

Join Size Estimation

The Cartesian product r×sr \times s contains nr,nsn_r, n_s tuples; each tuple occupies sr+sss_r + s_s bytes.

  • If RS=R \cap S = \emptyset, then rsr \bowtie s is the same as r×sr \times s
  • If RSR \cap S is a key for RR, then a tuple of s will join at most one tuple from rr.
    • Therefore, (the number of tuples in rsr \bowtie s) ns\leq n_s

  • If RSR \cap S is a foreign key in SS referencing RR, then the number of tuples in rs=thenumberoftuplesinsr \bowtie s = \mathrm{the number of tuples in} s
    • The case for RSR \cup S being a foreign key referencing SS is symmetric.

  • If RS=AR \cap S = {A} is not a key for RR or SS
  • If we assume that every tuple tt in RR produces tuples in RSR \bowtie S, the number of tuples in RSR \bowtie S is estimated to be:
nrnsV(A,s)=nrnsV(A,s)\frac{n_r * n_s}{V(A, s)} = n_r * \frac{n_s}{V(A, s)}

可以理解为:每一个 rr 都可以和 ss 中的每一个元组进行连接.

: If the reverse is true, the estimate obtained will be:

nrnsV(A,r)=nsnrV(A,r)\frac{n_r * n_s}{V(A, r)} = n_s * \frac{n_r}{V(A, r)}

Size Estimation for Other Operations

  • Projection: estimated size of πA(r)=V(A,r)\pi_A(r) = V(A, r)
  • Aggregation: estimated size of $$_A\mathcal{G}(r) = V(A, r)$
  • Set operations:
    • For unions/intersections of selections on the same relation: rewrite and use size estimate for selections
      • E.g., σθ1(r)σθ2(r)\sigma_{\theta_1}(r) \cup \sigma_{\theta_2}(r) can be rewritten as σθ1θ2(r)\sigma_{\theta_1 \land \theta_2}(r)
    • For operations on different realtions:
      • Estimated size of rsr \cup s = size of rr + size of ss
      • Estimated size of rsr \cap s = min(size of rr, size of ss)
      • Estimated size of rsr - s = size of rr
      • All the three estimated may be quite inaccurate, but provide upper bounds on the sizes.
  • Outer join:

Estimation of Number of Distinct Values

  • Selections: σθ(r)\sigma_{\theta}(r), estimate V(A,σθ(r))V(A, \sigma_{\theta}(r))
    • If θ\theta forces A to take a specified value: V(A,σθ(r))=1V(A, \sigma_{\theta}(r)) = 1
      • e.g., A = 3
    • If θ\theta forces A to take on one of a specified set of values: V(A,σθ(r))=numberofspecifiedvaluesV(A, \sigma_{\theta}(r)) = number of specified values
      • e.g., A in {1, 2, 3}
    • If the selection condition θ\theta is of the form AopvA op v, estimated V(A,σθ(r))=V(A,r)sV(A, \sigma_{\theta}(r)) = V(A, r) * s
      • Where ss is the selectivity of the selection
    • In all the other cases, use approximate estimate: V(A,σθ(r))=min(V(A,r),nσθ(r))V(A, \sigma_{\theta}(r)) = min(V(A, r), n_{\sigma \theta}(r))
  • Joins: rsr \bowtie s, estimate V(A,rs)V(A, r \bowtie s)
    • If all attributes in A are from rr, the estimated
V(A,rs)=min(V(A,r),nrs)V(A, r \bowtie s) = min(V(A, r), n_{r \bowtie s})

: - If A contains attributes A1 from rr and A2 from ss, then estimated

V(A,rs)=min(V(A1,r)V(A1A2,r)V(A2,s),nrs)V(A, r \bowtie s) = min(V(A1, r) * V(A1 - A2, r) * V(A2, s), n_{r \bowtie s})

Choice of Evaluation Plans

Cost-based Join-Order Selection

Consider finding the best join-order for r1r2...rnr_1 \bowtie r_2 ... r_n

There are (2(n1))!(n1)!\frac{(2(n-1))!}{(n-1)!} different join orders for above expression.

!!! EXAMPLE

![](./figure/ch16/join_order1.png)

Join Order Optimization Algorithm

  1. 对于一个集合 SS, 考虑其所有形式为 S1(SS1)S_1 \bowtie (S - S_1) 的连接方案,其中 S1S_1SS 的一个子集。
  2. 对于 SS 的每一个子集,递归计算其连接方案的代价。
  3. 当递归到单个关系时,考虑对其采取所有的连接方案,从而找出最优的连接方案。
  4. 实际上是一个动态规划的思想。

Left Deep Join Trees

In left-deep join trees, the right-hand-side input for each join is a relation, not the result of an intermediate join.

在 left-deep-join trees 中,每个连接的右侧输入都是一个关系,而不是中间连接的结果。

左边可以是中间结果。

Cost of Optimization
  • With dynamic programming
    • Time complexity of optimization with bushy tree is O(3n)O(3^n)
    • Space complexity is O(2n)O(2^n)
  • If only left-deep trees are considered
    • Time complexity of finding best join order is O(n2n)O(n*2^n)
    • Space complexity is O(2n)O(2^n)

Heuristic Optimization(启发式优化)

  • Systems may use Heuristics to reduce the number of choices that must be made in a cost-based fashion.
  • Heuristic optimization transforms the query-tree by using a set of rules that typically (but not in all cases) improve execution preformance:
    • Perform selection early(reduces the number of tuples)
    • Perform projection early(reduces the number of attributes)
    • Perform most restrictive selection and join operations(i.e. with smallest result size) before other similar operations.
    • Perform left-deep join order

Additional Optimization Techniques

Nested Subqueries

  • Nested query example:
select name
from instructor
where exists (select *
            from teaches
            where instructor.ID = teaches.ID and teaches.year = 2022)
  • SQL conceptually treats nested subqueries in the where clause as functions that take parameters and return a single value or set of values.
    • Parameters are variables from outer level query that are used in the nested subquery; such variables are called correlated variables(相关变量).

在上边的例子中,对于每一层外部循环,我们都要遍历内层的循环。但是如果来自外循环的变量没有相关变量的话,那么我们可以先执行内层的循环,然后将结果传递给外层的循环。

  • The semijoin(半连接) operator ⋉ is defined as follows:
    • If a tuple rir_i appears nn times in rr, it appears nn times in the result of rθsr ⋉_{\theta} s, if there is at least one tuple sjs_j in ss matching with rir_i.

  • The process of replacing a nested query by a query with a join/semijoin (possibly with a temporary relation) is called decorrelation(去除相关).

Decorrelation

  • Decorrelation of scalar aggregate subqueries can be done using groupby/aggregation in some cases.

Materialized Views

  • A materialized view is a view whose contents are computed and stored.
create view department_total_salary(dept_name, total_salary) as
select dept_name, sum(salary)
from instructor
group by dept_name

Materialized View Maintenance

Use incremental maintenance(增量视图维护).

Changes to database relations are used to compute changes to the materialized view, which is then updated.

对于数据库的关系的变化,我们可以计算出物化视图的变化,然后更新物化视图。

The changes (inserts and deletes) to a relation or expressions are referred to as its differential(差分).

Join Operation

for inserts:vnew=vold(irs)\mathrm{for\ inserts:} v^{new} = v^{old} \cup (i_r \bowtie s) for deletes:vnew=vold(drs)\mathrm{for\ deletes:} v^{new} = v^{old} - (d_r \bowtie s)
Selection and Projection Operations

Seelction: Consider a view V=σθ(r)V = \sigma_{\theta}(r)

: - vnew=voldσθ(ir)v^{new} = v^{old} \cup \sigma_{\theta}(i_r) : - vnew=voldσθ(dr)v^{new} = v^{old} - \sigma_{\theta}(d_r)

Aggregation Operations

Other Operations