Database - Query processing optimization

Copyright: Reprinted indicate the original works of the address https://blog.csdn.net/zjuwxx/article/details/90374277

table of Contents

A query processing

step

Second, query optimization

2.1 Overview

2.2 relational algebra equivalent transformation rules

2.2.1 Common equivalent transformation rules

Optimization rules algebraic expression 2.2.2 Relations

2.2.3 optimization algorithm algebraic expression relationship


A query processing

step

Analysis  of query statements scanning, lexical analysis and parsing

Check the query

  • Legitimate query semantic checks that database object definitions checks statement, such as the relationship name, attribute name is valid according to the data dictionary associated mode; if it is of the view operation , it will need to view the digestion methods to view the basic operation of the operation table of conversion pairs
  • The user permissions and checking integrity constraints defined in the data dictionary of the user access
  • After examination by converting SQL query into an internal representation, that is equivalent relational algebra expression

The query optimizer  to select an efficient query processing strategy execution

  • In accordance with the level of optimization, it can be divided into: algebraic optimization, physical optimization
  • Query optimization choices based on: rule-based, cost-based and based on semantics

Query execution  based on the implementation of the policy obtained by the optimizer, the query code execution plan generated by the code generator

  • Two execution methods: top-down and bottom-up

 

 

Second, query optimization

2.1 Overview

Query optimization is the key technology of relational systems relational database management system implementation is where the advantages of relational systems, it reduces the burden on the user to select the access path

Query Optimization relations system is done by the system, not done by the user

Relational database management system calculates the cost of some models by various execution cost of the query execution strategy and select the least costly implementation of the program

Query optimization of the overall goal: finding the optimal execution plan for the query execution costs as much as possible small

 

2.2 relational algebra equivalent transformation rules

Algebraic optimization method is to optimize the relationship between the conversion rule based on Algebraic

Algebraic optimization strategy: By equivalent transformation of algebraic expressions relations to improve query efficiency

Set E, E1, E2 algebraic expression is the relationship, if E1 replaced by the same relationship, the relationship between the results obtained after the same corresponding relationship between the variable E2, called E1, E2 equivalents, referred to as E1≡E2

2.2.1 Common equivalent transformation rules

1, connected to, Cartesian product commutative

Provided E1 and E2 are relational algebra expressions, F. Is the concatenation condition, then there

  • E1  \times  E2 ≡ E2  \times  E1
  • E1  \Join  E2 ≡ E2  \Join  E1
  • E1   \Join  E2 of ≡ E2 of   \Join  E1 (\Join based on the calculation conditions F. )

2, is connected, Cartesian product of associativity

Set E1 , E2 of , E3 is the relational algebra expressions, Fl and F2 are connected to the operation conditions, then there

  • (E1  \times  E2)   \times  E3 ≡ E1   \times  (E2 × E3)
  • (E1     E2)     E3 ≡ E1   (E2   E3)\Join\Join\Join \Join 
  • (E1     E2 of)     E3 E1 ≡   (E2 of     E3) ( prior to a calculation based on the conditions F1, the latter based on the calculation conditions F2 of )\Join\Join\Join \Join\Join\Join

3, the law of projection series (power-law, etc.)

Let E be a relational algebra expression, Ai (i = 1,2, ... , n), Bj (j = 1,2, ..., m) is the attribute name , {A . 1 , A 2 , ... , A n-  } configured B { . 1 , B 2 , ... , B } subset

A table several times to make a projection, the projection conditions subject to final

4, the selected series Law (Law idempotent)

Let E be a relational algebra expression, Ai (i = 1,2, ..., n), Bj (j = 1,2, ..., m) is the attribute name, {A1, A2, ..., An} configuration {B1, B2, ..., Bm} subset

On a table to make multiple selections, the final selection is the intersection of the results of the previous conditions of the various options made

5, the projection of the selected commutativity

6, selecting the Cartesian product commutative law (distributive law)

Set F = F1∧F2∧F3, wherein E1 F1 involves only attributes, F2 E2 involves only attributes, F3 relates to both E1 and E2 attributes

If an F. I does not exist, then remove the corresponding  \sigmaF. I

7, the distributive law of natural selection connection

设F=F1∧F2,其中F1只涉及E1的属性,F2只涉及E2的属性

8、选择对并、交、差的分配律

9、投影与笛卡尔积的交换律

设E1和E2是两个关系表达式,A1,…,An是E1的属性,B1,…,Bm是E2的属性

10、投影对笛卡尔积的分配律

设A1,A2,…,An是E1的属性, B1,B2,…,Bm是E2的属性

11、投影对并、交、差的分配律

2.2.2关系代数表达式的优化规则

选择、投影运算应尽可能先做

在优化策略中这是最重要、最基本的一条。因为这样参加连接的元组就可以大大减少,从而减少下一步运算的数据量

把选择和投影运算同时进行

如有若干投影和选择运算,并且它们都对同一个关系操作,则可以在扫描此关系的同时完成所有的这些运算以避免重复扫描关系

把投影同其前或其后的双目运算结合起来,没有必要为了去掉某些字段而扫描一遍关系

每形成一个连接后的元组,就立即取出投影字段。而不是先连接形成一个临时关系,然后在再此临时关系上投影

每取出Student的一个元组,先取出投影字段,然后与SC进行连接

把某些选择同在它前面要执行的笛卡尔积结合起来成为一个连接运算,连接特别是等值连接运算要比同样关系上的笛卡尔积省很多时间

找出公共子表达式

如果这种重复出现的子表达式的结果不是很大的关系并且从外存中读入这个关系比计算该子表达式的时间少得多,则先计算一次公共子表达式并把结果写入中间文件是合算的

当查询的是视图时,定义视图的表达式就是公共子表达式的情况

2.2.3关系代数表达式的优化算法

构造查询树(语法树)

第一步:把用高级语言定义的查询转换为关系代数表达式

  • 以SELECT子句对应投影操作,以FROM字句对应笛卡尔积,以WHERE子句对应选择操作,生成原始查询树
  • SQL语句转化为原始查询树

Step two: the relational algebra expressions into a query tree

n query tree is a tree-structured representation of relational algebra expressions. In a query tree, the leaf nodes represent the relationship between the nodes represent relational algebra operations. Query tree in a bottom-up fashion performed: When the operating components within a node is available, this operation is represented by the start node are performed with the end result of this relationship in place of the node

Example:

algorithm follows these heuristic rules apply equivalent transformation formula to optimize the relationship between the expression of

  • Input: a relational query expression tree
  • Output: optimized query tree

(1) Decomposition Select

Law using the selected series, the shaped like σF1∧F2∧ ... ∧Fn (E) is converted into equation σF1 (σF2 (... (σFn (E)) ...))

(2) selection down

For each selection, use the "commutative law of series selection, selection and projection, the distributive property of a Cartesian product of choice, the distributive property of choice and choose the difference of the distributive property" as it moves to the tree the tip

(3) projected downward

For each projection, the use of "Law concatenated projection, the projection and selection of the commutative law, the projection of the Cartesian product of the distributive law, and the projection of the distributive law" as it moves to the tip of the tree

(4) selection, projection combined

Using the "law of the projection series, series commutative law of selection, selection and the projection of" selection and the projection into a single selection, a single projection, or a projection is followed by selecting three cases, so that a plurality of selection and / or projection It can be performed simultaneously or in a single scan complete

(5) by point packet (only one of each binary operator)

The packet parse tree obtained above: each binary operator (×, \Join, ∪, -) and it is a direct ancestor of all group (which is the direct ancestor (σ, π operation) when it leaves the whole generations until Yes. when unary, then they will also be included into the group. However, when a binary operator Cartesian product (×), and with its composition not later selected equijoins, can not select the same composition with the binary operator group

(6) generating program

 It is performed sequentially after their offspring a set of evaluating value generating program according to each evaluation of each group, to produce the entire expression evaluator

Example:

 

by clouds into rain yycy

Guess you like

Origin blog.csdn.net/zjuwxx/article/details/90374277