Database connection cardinality estimation method and system based on connection diagram

By using Join Sketch in the relational database to divide elements into hot elements and cold elements and store them separately, the problem of slow connection cardinality estimation and low accuracy in the existing technology is solved, and more efficient connection cardinality estimation is achieved, improving Improve database query performance.

CN114625760BActive Publication Date: 2024-09-03PEKING UNIV +1
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202210137615.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2022-02-15
Publication Date
2024-09-03
Estimated Expiration
2042-02-15

AI Technical Summary

Technical Problem

The existing technology has slow speed and low accuracy in estimating connection cardinality in relational databases. It cannot effectively solve this problem especially when faced with skewed data distribution, causing the database to be unable to find the optimal connection sequence and dragging down database performance.

Method used

Using a method based on Join Sketch, the elements in the database table are divided into hot elements and cold elements, which are stored using hash tables and Sketch data structures respectively, and separated through element filters to calculate the connection between the hot element table and the cold element Sketch cardinality to improve estimation accuracy.

Benefits of technology

It improves the accuracy and efficiency of connection cardinality estimation, reduces time and space overhead, helps the database management system select the best connection sequence, and improves database query performance.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114625760B
    Figure CN114625760B
Patent Text Reader

Abstract

The present invention relates to a database connection cardinality estimation method and system based on a connection diagram. The steps of the method include: using an element filter to divide the elements in a database table into hot elements and cold elements; storing the hot elements in a hot element table, and storing the cold elements in a cold element Sketch; respectively calculating the connection cardinality of the hot element table of two database tables, the connection cardinality of the cold element Sketch, and the connection cardinality of the hot element table and the cold element Sketch, and adding them to obtain an estimation result of the connection cardinality of the two database tables. The present invention can improve the accuracy of database connection cardinality estimation by separating hot elements from cold elements, and the time and space overhead of the algorithm are reduced; accurate connection cardinality estimation is conducive to the database management system to give the best connection sequence, thereby improving the performance of complex database queries.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of database connection cardinality query, and in particular to a method and system for rapidly estimating the cardinality of connection results of two tables in a relational database by using a Join Sketch (connection sketch). Background Art

[0002] A relational database is a database that uses a relational model to organize data. It is currently the most widely used type of database. Common database systems, including PostgreSQL, MySQL, SQL Server, and Oracle, are typical relational databases. In a relational database, table join is an important operation that connects two or more data tables to form a new data table as output.

[0003] Database query optimization is an important issue in relational databases and has a significant impact on the overall performance of the database. When a user gives a query, the database management system needs to select the solution with the lowest query overhead from multiple equivalent query solutions. In order to find the best query solution, the database system needs to quickly and accurately estimate the overhead generated by different query solutions. Among the many influencing factors, the size of the connection cardinality has the greatest impact on query overhead. The connection cardinality refers to the number of records contained in the new data table generated after the connection operation of two data tables.

[0004] At present, the mainstream connection cardinality estimation schemes include establishing data histograms, sampling, machine learning models, and Sketch technology. Among these four schemes, data histograms require more storage to obtain better accuracy, and sampling schemes have poor performance when facing skewed data distribution and sparse data. Traditional Sketch-based connection cardinality estimation algorithms include AGMS Sketch and Fast AGMS Sketch. AGMS Sketch can estimate the database connection cardinality more accurately, but the algorithm has high time and space complexity; Fast AGMS Sketch is an improvement on AGMS Sketch, which significantly improves the speed, but loses some accuracy. The current algorithms cannot solve the task of connection cardinality estimation well, which may cause the database to be unable to find the optimal connection order, thereby dragging down the performance of the database system. Summary of the invention

[0005] In order to overcome the problems of slow speed, low accuracy and inability to cope with skewed data distribution in existing connection cardinality estimation algorithms, the present invention provides a Sketch-based connection cardinality estimation method, which is based on the idea of ​​separating cold elements from hot elements and can effectively improve the accuracy of connection cardinality estimation under skewed data distribution.

[0006] The purpose of the present invention is achieved through the following technical solutions:

[0007] A method for estimating database connection cardinality based on a connection diagram comprises the following steps:

[0008] Use element filters to classify elements in the database table into hot elements and cold elements;

[0009] Store hot elements in the hot element table and store cold elements in the cold element Sketch;

[0010] The connection cardinality of the hot element table of the two database tables, the connection cardinality of the cold element Sketch, and the connection cardinality of the hot element table and the cold element Sketch are calculated respectively, and the cardinality is added to obtain an estimated result of the connection cardinality of the two database tables.

[0011] Furthermore, the hot element table is used to store hot elements, and each entry of the hot element table is composed of two parts: an element fingerprint and a counter. The element fingerprint is a hash value of the element, and the counter records the frequency of occurrence of the hot element.

[0012] Furthermore, the element filter includes several buckets, each bucket includes several records, each record consists of two parts: an element fingerprint and a counter. The element fingerprint is a hash value of the element, and the counter records the frequency of occurrence of the element.

[0013] Furthermore, the cold element Sketch is a CM Sketch, which is composed of a number of counters and has d mutually independent hash functions, and the function of the hash function is to randomly map an element to a counter of the CM Sketch.

[0014] Furthermore, when an element of the database table is inserted into the hot element table and the cold element Sketch, the element is first inserted into the element filter. The element filter calculates the hash value of the element and then maps it to a bucket. There are four different processing situations depending on whether the bucket contains the element:

[0015] If the bucket contains this element, then increase the counter of the element by 1;

[0016] If the bucket does not contain this element and there is an empty record, insert the element into the position of the empty record;

[0017] If the bucket does not contain this element and the bucket is full, then determine whether the frequency of the element with the largest frequency in the bucket exceeds the threshold T. If it exceeds, then the largest element is considered to be a hot element, and the hot element is inserted into the hot element table, and then the element to be inserted is inserted into the original position of the hot element;

[0018] If the bucket does not contain this element and the bucket is full, and the maximum number of elements in the bucket is not greater than T, then select an element in the bucket through some selection method, treat the element as a cold element, insert it into the cold element Sketch, and then insert the element to be inserted into the original position of the cold element.

[0019] Furthermore, the selection method of selecting an element in the bucket is one of the following: random selection, probability selection, and selection of the element with the smallest frequency.

[0020] Furthermore, the calculation of the connection cardinality of the hot element tables and the cold element Sketches of different database tables includes:

[0021] For the hot element table, compare the hot element tables of database table 1 and database table 2. If the fingerprints are the same, they are considered to be the same element, and the corresponding counters are multiplied and all the products are summed;

[0022] For the cold element Sketch, multiply and sum the values ​​of the counters at the corresponding positions of the cold element Sketch in database table 1 and database table 2;

[0023] For each element in the hot element table of database table 1, the estimated value of the frequency is queried in the cold element Sketch in database table 2; for each element in the hot element table of database table 2, the estimated value of the frequency is queried in the cold element Sketch in database table 1.

[0024] A database connection cardinality estimation system based on a connection diagram using the above method comprises:

[0025] The element filtering module is used to classify the elements in the database table into hot elements and cold elements by using element filters;

[0026] A thermal element storage module, used for storing thermal elements in a thermal element table;

[0027] The cold element storage module is used to store the cold elements in the cold element Sketch;

[0028] The connection cardinality estimation module is used to calculate the connection cardinality of the hot element table of the two database tables, the connection cardinality of the cold element Sketch, and the connection cardinality of the hot element table and the cold element Sketch, and add them up to obtain the estimated result of the connection cardinality of the two database tables.

[0029] The beneficial effects of the present invention are as follows: by separating hot elements from cold elements, the accuracy of estimating the connection cardinality of the database can be improved, and the time and space overhead of the algorithm are reduced. Accurate connection cardinality estimation is conducive to the database management system to give the best connection sequence, thereby improving the performance of complex database queries. BRIEF DESCRIPTION OF THE DRAWINGS

[0030] Figure 1 It is a flowchart for inserting elements into Join Sketch.

[0031] Figure 2 This is an example of inserting an element into a Join Sketch. DETAILED DESCRIPTION

[0032] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the examples in the accompanying drawings. It should be understood that the specific examples described herein are only used to explain the present invention and are not used to limit the present invention.

[0033] The key idea of ​​the present invention is to separate cold elements from hot elements, and use different data structures to store cold elements and hot elements to improve the accuracy of element frequency estimation, thereby improving the accuracy of table connection cardinality estimation. In a real database system, the data distribution of different values ​​of a domain is not even, and the skewness is often very high. Those elements with higher frequencies are called "hot elements" and those with lower frequencies are called "cold elements". The traditional AGMS and Fast AGMS algorithms treat all elements in the same way, that is, insert all elements directly into Sketch, which has problems. Although the above two algorithms can provide unbiased estimates of connection cardinality, the variance of the estimation results is large when the data distribution is highly skewed. For Fast AGMS, the main source of error is the hash collision between cold elements and hot elements, that is, all counters mapped to cold elements are modified by hot elements, resulting in a greatly overestimated frequency of cold elements, which ultimately has a great impact on the accuracy of connection cardinality estimation. In order to solve the above problems, the present invention proposes an algorithm called Join Sketch, which uses a hash table to accurately store hot elements, uses Sketch to store cold elements, and designs a filter that is responsible for separating cold elements from hot elements.

[0034] Join Sketch can separate cold elements from hot elements through one traversal. By storing cold elements and hot elements separately, hash collision is avoided, so that the algorithm estimates both cold elements and hot elements relatively accurately, ensuring the accuracy of the connection cardinality estimation. Specifically, Join Sketch consists of three parts: hot element table, element filter and cold element Sketch. The hot element table is used to store hot elements. Each entry in the table consists of two parts: element fingerprint and counter. The element fingerprint is a hash value of the element, and the counter records the frequency of occurrence of the hot element. The element filter includes several buckets, each of which includes several records. Each record consists of two parts: element fingerprint and counter. Similarly, the element fingerprint is a hash value of the element, and the counter records the frequency of occurrence of the element. Cold element Sketch is a traditional CM Sketch. CMSketch consists of several counters and d independent hash functions. The hash function is to randomly map an element to a counter in CM Sketch.

[0035] Join Sketch supports two main operations: construction and query of connection cardinality. The construction operation requires traversing the corresponding domain of the database table and inserting each element of the database table into the Join Sketch. The specific operation of insertion is introduced below. When inserting an element, the element is first inserted into the element filter. The element filter calculates the hash value of the element and then maps it to one of the buckets. There are four different processing situations depending on whether the bucket contains the element:

[0036] 1) If the bucket contains this element, then increase the counter of the element by 1;

[0037] 2) If the bucket does not contain this element and there is an empty record, insert the element into the position of the empty record;

[0038] 3) If the bucket does not contain this element and the bucket is full, determine whether the frequency of the element with the largest frequency in the bucket exceeds the threshold T. If it exceeds, the largest element is considered to be a hot element, and the hot element is inserted into the hot element table, and then the element to be inserted is inserted into the original position of the hot element;

[0039] 4) If the bucket does not contain the element and the bucket is full, and the maximum number of elements in the bucket is not greater than T, select an element in the bucket through a certain mechanism, treat the element as a cold element, insert it into the cold element Sketch, and then insert the element to be inserted into the original position of the cold element. The selection of an element in the bucket through a certain mechanism can be random selection, probability selection, or selection of the element with the smallest frequency.

[0040] When all elements are inserted, the remaining elements in the element filter are inserted into the hot element table or the cold element Sketch. For each element, if its counter is greater than the threshold T, it is inserted into the hot element table, otherwise it is inserted into the cold element Sketch. Figure 1 It is a flowchart for inserting elements into Join Sketch.

[0041] Given two database tables, Table 1 and Table 2, construct Join Sketches for the corresponding fields in the two database tables. When querying the join cardinality, estimate the join cardinality for different parts of the Join Sketch and add the estimated join cardinality. Specifically, for the hot element table, compare the hot element table in Table 1 with the hot element table in Table 2. If the fingerprints are the same, they are considered to be the same element. The corresponding counters are multiplied and all the products are summed (as the first part result). For the cold element Sketch, the values ​​of the counters at the corresponding positions of the CM Sketches in Table 1 and Table 2 are multiplied and summed (as the second part result). In addition, the situation of hot elements and cold elements being connected should also be considered (as the third part result), that is, for each element in the hot element table in Table 1, query the estimated value of the frequency in the CM Sketch in Table 2; the same is true for the hot element table in Table 2, that is, for each element in the hot element table in Table 2, query the estimated value of the frequency in the CM Sketch in Table 1. Adding the above three parts of the results, you can get the estimate of the join cardinality of Table 1 and Table 2.

[0042] Figure 1 This is the flow chart of inserting elements in Join Sketch. When inserting element K, the algorithm first maps element K to a bucket in the element filter, and then determines whether element K is already stored in the bucket. If element K is already stored in the bucket, the counter of element K is increased by 1, and the algorithm ends. If there is no element K in this bucket, the algorithm determines whether there is an empty position in this bucket. If there is an empty position, element K is inserted into this empty position, and the algorithm ends. If there is no K in the bucket and no empty position, the algorithm will find the element with the largest frequency in this bucket, and then determine whether the frequency of this element is greater than the threshold T. If the frequency of the largest element is greater than the threshold T, the element with the largest frequency is inserted into the hot element table, and the position is set to empty; if the frequency of the largest element is not greater than the threshold T, an element is selected by some mechanism and inserted into the cold element Sketch. Different selection mechanisms can be used in different scenarios, such as random selection, probability selection, or selection of the element with the smallest frequency. Then, element K is inserted into the empty position in the bucket, and the algorithm ends.

[0043] Figure 2This is an example of inserting an element into a Join Sketch. As you can see, the Join Sketch consists of three parts: the hot element table, the element filter, and the cold element Sketch. The hot element table is a simple linear table, and each entry in the table consists of two parts: an element fingerprint and a counter, i.e., f in the figure. i and n i . The element filter includes several buckets, each bucket includes several records, and each record is also composed of two parts: element fingerprint and counter, that is, the (f, c) tuple in the figure. The element filter is associated with a hash function g. The function of this hash function is to map the element to a certain bucket. The cold element Sketch is a traditional CM Sketch. The CM Sketch consists of several counters and has d independent hash functions. The function of the hash function is to randomly map an element to a counter in the CM Sketch. The CM Sketch in the figure has 3 hash functions, namely h1, h2, and h3. To insert an element e, the algorithm will first calculate the value of g(e) and map the element e to a bucket of the element filter. Figure 2 In the example, e is mapped to the second bucket. Then, the fingerprint of e is calculated to determine whether e is in the bucket. Assume that the fingerprint of e is f 21 , that is, if there is element e in the bucket, execute c 21 ←c 21 +1, this is not shown in the figure. If e is not in the bucket and there is no empty position in the bucket, the algorithm will kick an element out of the element filter. Assume f 22 is the element with the largest frequency, and its frequency is greater than the threshold T, that is, c 22 >T, then insert the element into the hot element table, as shown in the figure, and then insert e into f 22 If c 22 ≤T, then select an element through the selection mechanism and insert it into the cold element Sketch. Assume that element f 24 is the selected cold element, then the algorithm will insert the element into the cold element Sketch, the insertion process is shown in the figure, and then insert e into f 24 Location.

[0044] In the process of database query optimization, since the connection order has a great impact on the performance of database query, the method of the present invention can be used to estimate the connection cardinality of two tables, and select the query solution with the smallest connection cardinality of the intermediate result as the final query solution to improve the performance of the database system when facing complex queries. Specifically, a query involving N database tables has C N Different query schemes (C NThe overhead of different query schemes varies greatly. By using this patent to estimate the connection cardinality and then selecting the query scheme with the smallest connection cardinality, the operating efficiency of the database system can be improved.

[0045] The core idea of ​​the present invention is to use a method of separating hot and cold elements to avoid estimation errors caused by collisions between hot and cold elements. The cold element Sketch uses Count Sketch, which can also be replaced by other types of Sketch, such as Count-MinSketch, CU Sketch, A Sketch, etc. When the element length is small, the element itself can also be saved directly without calculating the fingerprint (i.e., the element itself is used as the fingerprint).

[0046] Based on the same inventive concept, another embodiment of the present invention provides a database connection cardinality estimation system based on a connection diagram using the above method, which includes:

[0047] The element filtering module is used to classify the elements in the database table into hot elements and cold elements by using element filters;

[0048] A thermal element storage module, used for storing thermal elements in a thermal element table;

[0049] The cold element storage module is used to store the cold elements in the cold element Sketch;

[0050] The connection cardinality estimation module is used to calculate the connection cardinality of the hot element table of the two database tables, the connection cardinality of the cold element Sketch, and the connection cardinality of the hot element table and the cold element Sketch, and add them up to obtain the estimated result of the connection cardinality of the two database tables.

[0051] Based on the same inventive concept, another embodiment of the present invention provides an electronic device (computer, server, smart phone, etc.), which includes a memory and a processor, the memory stores a computer program, the computer program is configured to be executed by the processor, and the computer program includes instructions for executing each step in the method of the present invention.

[0052] Based on the same inventive concept, another embodiment of the present invention provides a computer-readable storage medium (such as ROM / RAM, disk, CD), which stores a computer program. When the computer program is executed by a computer, it implements the various steps of the method of the present invention.

[0053] The specific embodiments of the present invention disclosed above are intended to help understand the content of the present invention and implement it accordingly. It can be understood by those skilled in the art that various replacements, changes and modifications are possible without departing from the spirit and scope of the present invention. The present invention should not be limited to the contents disclosed in the embodiments of this specification, and the scope of protection of the present invention shall be subject to the scope defined in the claims.

Claims

1. A method for estimating database connection cardinality based on a connection diagram, characterized in that: The following steps are involved: Use element filters to classify elements in the database table into hot elements and cold elements; Store hot elements in the hot element table and store cold elements in the cold element Sketch; Calculate the connection cardinality of the hot element table of the two database tables, the connection cardinality of the cold element Sketch, and the connection cardinality of the hot element table and the cold element Sketch respectively, and add them up to obtain an estimated result of the connection cardinality of the two database tables; The calculation of the connection cardinality of the hot element tables and the cold element Sketches of different database tables includes: For the hot element table, compare the hot element tables of database table 1 and database table 2. If the fingerprints are the same, they are considered to be the same element, and the corresponding counters are multiplied and all the products are summed; For the cold element Sketch, multiply and sum the values ​​of the counters at the corresponding positions of the cold element Sketch in database table 1 and database table 2; For each element in the hot element table of database table 1, the estimated value of the frequency is queried in the cold element Sketch in database table 2; for each element in the hot element table of database table 2, the estimated value of the frequency is queried in the cold element Sketch in database table 1.

2. The method according to claim 1, characterized in that The hot element table is used to store hot elements, and each entry of the hot element table consists of an element fingerprint and a counter. The element fingerprint is a hash value of the element, and the counter records the frequency of occurrence of the hot element.

3. The method according to claim 1, characterized in that The element filter includes several buckets, each bucket includes several records, each record consists of two parts: an element fingerprint and a counter. The element fingerprint is a hash value of the element, and the counter records the frequency of occurrence of the element.

4. The method according to claim 1, characterized in that The cold element Sketch is a CM Sketch, which is composed of several counters and has d mutually independent hash functions. The function of the hash function is to randomly map an element to a counter of the CMSketch.

5. The method according to claim 1, characterized in that When an element of the database table is inserted into the hot element table and the cold element Sketch, the element is first inserted into the element filter, and the element filter calculates the hash value of the element. It is then mapped into a bucket, and there are four different processing situations depending on whether the bucket contains the element: If the bucket contains this element, then increase the counter of the element by 1; If the bucket does not contain this element and there is an empty record, insert the element into the position of the empty record; If the bucket does not contain this element and the bucket is full, then determine whether the frequency of the element with the largest frequency in the bucket exceeds the threshold T. If it exceeds, then the largest element is considered to be a hot element, and the hot element is inserted into the hot element table, and then the element to be inserted is inserted into the original position of the hot element; If the bucket does not contain this element and the bucket is full, and the maximum number of elements in the bucket is not greater than T, then select an element in the bucket through some selection method, treat the element as a cold element, insert it into the cold element Sketch, and then insert the element to be inserted into the original position of the cold element.

6. The method according to claim 5, characterized in that The selection method of selecting an element in the bucket is one of the following: random selection, probability selection, and selection of the element with the minimum frequency.

7. A database connection cardinality estimation system based on a connection diagram using the method described in any one of claims 1 to 6, characterized in that: include: The element filtering module is used to classify the elements in the database table into hot elements and cold elements by using element filters; A thermal element storage module, used for storing thermal elements in a thermal element table; The cold element storage module is used to store the cold elements in the cold element Sketch; The connection cardinality estimation module is used to calculate the connection cardinality of the hot element table of the two database tables, the connection cardinality of the cold element Sketch, and the connection cardinality of the hot element table and the cold element Sketch, and add them up to obtain the estimated result of the connection cardinality of the two database tables.

8. An electronic device, characterized in that: The invention comprises a memory and a processor, wherein the memory stores a computer program, the computer program is configured to be executed by the processor, and the computer program comprises instructions for executing the method according to any one of claims 1 to 6.

9. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores a computer program, and when the computer program is executed by a computer, the method according to any one of claims 1 to 6 is implemented.