当前位置:网站首页>Royal treasure: an analysis of SQL algebra optimization
Royal treasure: an analysis of SQL algebra optimization
2022-06-24 06:06:00 【Write about Nancheng】
1. Algebraic optimization
Algebraic optimization is the equivalent exchange of queries , To reduce execution overhead . The so-called equivalence means that the result of the transformed relational algebra expression is the same as that of the transformed relational algebra expression .
(1) The equivalent change rule
Transform a relational algebraic expression into another equivalent expression that can be executed more efficiently .
Try to select and project first , Then do the connection operation .
When the connection is , First, make connections between small relationships , And then make the connection of big relationship .
1) Multiple choice (σ)
2) choice (σ) Commutative law of
3) Multiple projection (∏)
4) choice (σ) And projection (∏) In exchange for
5) Connection and Cartesian product (x) Commutative law of
6) and (∪) Make friends with others (∩) Commutative law of operations
R ∪ S = S ∪ R
R ∩ S = S ∩ R
7) choice (σ) And connected commutative law
8) Projection (∏) And the distributive law of connection
9) Select merge with set 、 hand over 、 Distributive law of difference operation
10) Projection (∏) Distributive law of union operation
11) Connection and Cartesian product
12) and (∪) Make friends with others (∩) The law of association of
(2) Heuristic rules
1) The selection operation should be done first as far as possible ;
2) Preprocess the relationship appropriately before executing the connection ;
3) Projection operation and selection operation are performed simultaneously ;
4) The binocular operation before or after a projection ( and 、 hand over 、 Bad ) Combine ;
5) Transform some selection operations and Cartesian products performed before them into join operations ;
6) Do the projection operation in advance ( But keep the properties used for the connection );
7) Find the common subexpression .
give an example : Check the elective course 2 The name of the student in course No
1 2 select Sname from Student join SC on Student.Sno=SC.Sno 3 where SC.Cno='2'; |
|---|
The process of optimization :
(1) Convert to an initial relational algebraic expression ( Not optimized ):
(2) Optimize with transformation rules
① Use rules 1 Decompose the connection operation part of the selection operation into each selection operation , Make the selection operation as early as possible
② Replace Cartesian product operation with equivalent connection operation
③ Use rules 4 And rules 6 Yes Student Do the projection operation
2. Physical optimization
(1) Select the optimization of the operation
1) For small relationships , There is no need to consider other access paths , Direct sequential scanning ;
2) If no access paths such as indexes or hashes are available , Or it is estimated that the number of tuples selected accounts for a large proportion in the relationship ( For example, greater than 15%), And there is no clustered index for related attributes , Then reference sequence scanning ;
3) For non primary key equivalent condition query , To estimate the proportion of the number of tuples selected in the relationship . If the proportion is small ( For example, the proportion is less than 15%), Available nonclustered indexes , Otherwise, only clustered indexes or sequential scans can be used ;
4) For range condition queries , Generally, the boundary of the range is found through the index , Then, the ordered set of the index is collected along the corresponding direction ;
5) For using and Join selection condition of connection , If there is a corresponding multi-attribute index , Multi attribute index should be adopted first ;
6) For using or Disjunctive selection condition of connection , There is no good optimization method , Only one tuple set can be selected according to each condition , Then we calculate the union of these tuples ;
7) Some selection operations can get results just by accessing the index .
(2) Optimization of connection operation
1) If both relationships are sorted by connection properties , Then the sorting and merging method is preferred ;
2) If one of the two relationships has an index in the connection attribute ( Especially clustered indexes ) Or hash , Another relationship can be treated as an external relationship , Sequential scanning , And use the index or hash on the inner relation to find the matching tuple , Instead of polytropic scanning ;
3) If the conditions for applying the above two rules are not met , And both relationships are relatively small , Then the nested loop method can be applied ;
4) If the rules 1、2、3 None of them apply , You can use hash join method .
(3) Optimization of projection operation
Projection operation is generally related to selection 、 Connection and other operations are carried out at the same time , No attachment required I/O expenses . If the projected attribute set does not contain a primary key , Then duplicate tuples may appear in the projection result . Eliminating duplicate tuples is a time-consuming operation , Generally, you need to sort the projection results by all their attributes , Keep repeating tuples in continuous storage , To find duplicate tuples . Hashing is also a feasible way to eliminate duplicate tuples .
边栏推荐
- Install and use juicefs storage on Tencent cloud
- Build ZABBIX on Tencent ECS
- System of test development - create test virtual machine on demand
- Tencent (host security) was listed in the market guide for cloud workload protection platform released by Gartner
- How do I view the IP address of a domain name? What is the relationship between domain name and IP?
- At the beginning of the school season, use this template to improve the management level
- Flutter - date of birth calculation age tool class
- Experience sharing on unified management and construction of virtual machine
- Smart Logistics: with the advent of aiot era, how to get through the "last mile" of logistics?
- Enterprise management background user manual
猜你喜欢
随机推荐
Enterprise management background user manual
What if the domain name is blocked? What can I do to quickly unseal?
Tencent Youtu presents a number of AI technologies at the 2021 global digital economy conference
Coding and codesign: make design and development easier
Tencent security monthly report - Tencent security has been selected into several authoritative research reports, a data security special committee has been established, and zero trust specifications
Idea2020 latest activation tutorial, continuously updated
Summary of basic notes of C language (I)
Brief introduction to the working principle of high frequency signal generator
Fixed assets management software enables enterprises to realize intelligent management of fixed assets
How to recover data by splicing database fragments
How about the online domain name? Is it easy to use from the current market
Tencent cloud harbor private warehouse deployment practice
Net domain name? Net domain name?
My two-year persistence is worth it!
Detailed explanation of IPv6 theory and concept
How to do reverse domain name resolution? What does reverse domain name resolution mean?
Kubernetes Chapter 1: Foundation
What does it mean that the domain name is being walled? How to solve the problem of domain name by wall?
New tea: reshuffle, transformation, merger and acquisition
Script updates CLB type ingress Certificate in tke cluster



