Database operation statement optimization and database operation method, device and medium
Patent Information
- Application Number
- CN202310252269.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-03-10
- Publication Date
- 2026-10-20
- Estimated Expiration
- 2043-03-10
AI Technical Summary
当SQL语句包括多个表达式时,多个表达式中出现重复的子表达式的概率较大,对重复的子表达式进行重复计算,计算效率较低,造成计算资源的浪费,影响关系数据库系统的查询性能
[0009]在本申请实施例中,基于数据库操作语句中的至少一个表达式对应的抽象语法树,从至少一个子表达式中选择公共子表达式,至少一个子表达式来源于至少一个表达式,并将至少一个表达式中的公共子表达式替换为公共子表达式的执行结果标记,以优化数据库操作语句。这样,在代码执行阶段,只需执行公共子表达式对应的子执行代码一次即可,在执行到代码中包括公共子表达式的执行结果标记时,复用执行公共子表达式对应的子执行代码所得到的执行结果,进而无需对重复的子表达式进行重复计算,提高了计算效率,节约了计算资源,提升了数据库系统的查询性能。
Smart Images

Figure CN116303574B_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the technical field of database, and particularly relates to a database operation statement optimization method and device, a database operation method, equipment and a medium. BACKGROUND
[0002] Various relational database systems support data query using SQL (Structured Query Language). Specifically, a client submits a SQL statement to a server of a relational database system, the server parses the SQL statement into an AST (abstract syntax code), and generates a logical execution plan based on the AST, executes a database operation such as data query, data update, data deletion or data insertion according to the logical execution plan, and returns a database operation result to the client.
[0003] Generally, the logical execution plan includes a plurality of logical operators, and logical operators such as a Projection operator, a filter operator and the like in the plurality of logical operators need to calculate expressions in the SQL statement. When the SQL statement includes a plurality of expressions, the probability of repeated sub-expressions appearing in the plurality of expressions is relatively high, repeated calculation of the repeated sub-expressions is relatively low in calculation efficiency, and the calculation resources are wasted, which affects the query performance of the relational database system. SUMMARY
[0004] Aspects of the present application provide a database operation statement optimization method and device, and a database operation method and medium, to optimize a database operation statement and improve the query performance of a database system.
[0005] The database operation statement optimization method provided by the present application includes: constructing an abstract syntax tree corresponding to at least one expression in a database operation statement; selecting a common sub-expression from at least one sub-expression using at least one abstract syntax tree, the at least one sub-expression being derived from the at least one expression; and replacing the common sub-expression in the at least one expression with an execution result marker of the common sub-expression, to optimize the database operation statement, the execution result marker of the common sub-expression being used to indicate that an execution result obtained by running a sub-execution code corresponding to the common sub-expression is reused in a code execution stage.
[0006] This application also provides a database operation method, comprising: generating a logical execution plan based on a first abstract syntax tree of a database operation statement, wherein at least one logical operator in the logical execution plan each includes a portion of an expression in the database operation statement; constructing a second abstract syntax tree corresponding to at least one expression for at least one logical operator; selecting a common sub-expression from at least one sub-expression using at least one second abstract syntax tree, wherein the at least one sub-expression originates from at least one expression; replacing the common sub-expression in the at least one expression with the execution result marker of the common sub-expression to optimize at least one logical operator; generating execution code based on the optimized at least one logical operator and the common sub-expression; and running the execution code to obtain the operation result of the database operation statement, wherein, during the execution of the execution code, the execution result obtained by running the sub-execution code corresponding to the common sub-expression is reused based on the execution result marker of the common sub-expression.
[0007] This application also provides an electronic device, including: a memory and a processor; the memory for storing a computer program; and the processor coupled to the memory for executing the computer program to perform the steps in the above method.
[0008] This application also provides a computer-readable storage medium storing a computer program, which, when executed by a processor, enables the processor to implement the steps in the above-described method.
[0009] In this embodiment, based on the abstract syntax tree corresponding to at least one expression in the database operation statement, a common sub-expression is selected from at least one sub-expression. The at least one sub-expression originates from at least one expression, and the common sub-expression in the at least one expression is replaced with the execution result marker of the common sub-expression to optimize the database operation statement. Thus, during the code execution phase, the sub-execution code corresponding to the common sub-expression only needs to be executed once. When the execution result marker of the common sub-expression is encountered, the execution result obtained from executing the sub-execution code corresponding to the common sub-expression is reused, thereby avoiding repeated calculations of duplicate sub-expressions, improving computational efficiency, saving computational resources, and enhancing the query performance of the database system. Attached Figure Description
[0010] The accompanying drawings, which are included to provide a further understanding of this application and form part of this application, illustrate exemplary embodiments of this application and are used to explain this application, but do not constitute an undue limitation of this application. In the drawings:
[0011] Figure 1 This is a system architecture diagram of an exemplary database system;
[0012] Figure 2 A flowchart illustrating a database operation statement optimization method provided in this application embodiment;
[0013] Figure 3 A flowchart illustrating a database operation method provided in this application embodiment;
[0014] Figure 4 A schematic diagram of a database operation statement optimization device provided in this application;
[0015] Figure 5 A schematic diagram of the structure of a database operation device provided in this application;
[0016] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0017] To make the objectives, technical solutions, and advantages of this application clearer, the technical solutions of this application will be clearly and completely described below in conjunction with specific embodiments and corresponding drawings. Obviously, the described embodiments are only a part of the embodiments of this application, and not all of them. Based on the embodiments in this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0018] In the embodiments of this application, "at least one" refers to one or more, and "more than one" refers to two or more. "And / or" describes the access relationship between associated objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, and B existing alone, where A and B can be singular or plural. In the textual description of this application, the character " / " generally indicates that the preceding and following associated objects have an "or" relationship. Furthermore, in the embodiments of this application, "first," "second," "third," etc., are only used to distinguish the content of different objects and have no other special meaning.
[0019] Typically, a logical execution plan includes multiple logical operators. Among these operators, such as Projection and Filter, logical operators need to evaluate the expressions in the SQL statement. When an SQL statement contains multiple expressions, the probability of duplicate sub-expressions is relatively high. Repeatedly evaluating these duplicate sub-expressions is inefficient, wastes computational resources, and negatively impacts the query performance of the relational database system.
[0020] Based on the above, embodiments of this application provide a database operation statement optimization method, device, and medium. In these embodiments, based on the abstract syntax tree corresponding to at least one expression in a database operation statement, a common sub-expression is selected from at least one sub-expression. The at least one sub-expression originates from at least one expression, and the common sub-expression in the at least one expression is replaced with the execution result marker of the common sub-expression to optimize the database operation statement. Thus, during code execution, the sub-execution code corresponding to the common sub-expression only needs to be executed once. When the execution result marker of the common sub-expression is encountered, the execution result obtained from executing the sub-execution code corresponding to the common sub-expression is reused, thereby eliminating the need for repeated calculations of duplicate sub-expressions, improving computational efficiency, saving computational resources, and enhancing the query performance of the database system.
[0021] The technical solutions provided by the various embodiments of this application are described in detail below with reference to the accompanying drawings.
[0022] Figure 1 This is a system architecture diagram of an exemplary database system. See also... Figure 1 The client sends various database operation statements, such as SQL statements, to the server in the database system. The management node on the server parses the database operation statements to construct an abstract syntax tree (AST). Based on the AST, the management node generates a logical execution plan, which typically includes multiple logical operators, such as projection and filter operators, which evaluate the expressions in the database operation statements. Furthermore, the management node is responsible for allocating the logical execution plan, assigning the logical operators within the plan to the compute nodes on the server. The compute nodes then execute the assigned logical operators to perform various operations on the data in the database, such as querying, modifying, or deleting data.
[0023] An expression is a combination of one or more values, operators, or functions. Taking SQL statements as an example, there are three different types of SQL expressions: Boolean expressions, numeric expressions, and date expressions. Boolean expressions retrieve data based on matching a single value; numeric expressions are used to perform mathematical operations in any query; and date expressions return the current system date and time. In practical applications, an expression may include several sub-expressions, which can be understood as expressions contained within an expression. Database operation statements have a relatively high probability of containing duplicate sub-expressions. Computational nodes repeatedly calculate these duplicate sub-expressions, resulting in low computational efficiency, wasted computing resources, and negatively impacting the query performance of the database system. Therefore, it is essential to optimize database operation statements.
[0024] In this embodiment, the client, management node, or computing node can be composed of software and / or hardware. In terms of implementation, the client, management node, or computing node can be a terminal device or a server. The terminal device or server can be hardware or software. When the terminal device is hardware, it can be, for example, a mobile phone, tablet computer, desktop computer, wearable smart device, smart home device, etc. When the terminal device is software, it can be installed on the aforementioned hardware devices. In this case, the terminal device can be, for example, multiple software modules or a single software module; this embodiment is not limited. The server can be hardware or software. When the server is hardware, it can be a single server or a distributed server cluster composed of multiple servers. When the server is software, it can be multiple software modules or a single software module; this embodiment is not limited.
[0025] Figure 2 A flowchart illustrating a database operation statement optimization method provided in this application embodiment. See also... Figure 2 The method may include the following steps:
[0026] 201. Construct an abstract syntax tree corresponding to at least one expression in a database operation statement.
[0027] 202. Using at least one abstract syntax tree, select a common subexpression from at least one subexpression, where at least one subexpression is derived from at least one expression.
[0028] 203. Replace at least one common subexpression in an expression with the execution result marker of the common subexpression to optimize database operation statements. The execution result marker of the common subexpression is used to indicate the execution result obtained by reusing the sub-execution code corresponding to the common subexpression during the code execution phase.
[0029] In this embodiment, database operation statements refer to statements that perform various operations on the database, such as querying tables, creating tables, and modifying tables, and there are no restrictions on this. Database operation statements include, but are not limited to, SQL statements.
[0030] In this embodiment, the number of expressions in the database operation statement to be optimized may be one or more. For each expression in the database operation statement, the expression is parsed to construct an abstract syntax tree for the expression.
[0031] In this embodiment, using one or more abstract syntax trees corresponding to database operation statements, for at least one expression appearing in the database operation statement, a common subexpression is found from at least one subexpression included in the at least one expression. A common subexpression can be understood as a subexpression that appears multiple times in the database operation statement. For example, if the following two expressions appear in the same SQL statement: `json_extract_0(json_parse(content), 'user')user` and `json_extract_0(json_parse(content), 'action')action`, the common subexpression in these two expressions is `json_parse(content)`.
[0032] Further, optionally, to more accurately identify common subexpressions, the levels in the abstract syntax tree can be traversed in ascending order. During the traversal, a mapping table recording at least one subexpression corresponding to each level and a reference count table recording the reference count of each subexpression are updated. After traversing the entire abstract syntax tree, the common subexpression with the most references is identified from the multiple subexpressions based on the reference count table.
[0033] For example, the leaf nodes in the abstract syntax tree have the lowest level, and the parent node's level is one level higher than the child node's level. Accordingly, one implementation of selecting a common subexpression from at least one subexpression using at least one abstract syntax tree is as follows: For any abstract syntax tree in the at least one abstract syntax tree, traverse each level in the abstract syntax tree in ascending order of level, and for the first subexpression corresponding to the node in the current traversal level, query the mapping table based on the current level and the first subexpression, where the mapping table includes at least one subexpression corresponding to each of the at least one level; based on the query results of the mapping table, update the mapping table and the reference count record table used to record the reference counts of each subexpression; after all at least one abstract syntax tree has been processed, select a common subexpression from at least one subexpression based on the reference counts of each subexpression in the reference count record table.
[0034] In practical applications, when traversing at least one abstract syntax tree, the current abstract syntax tree can be selected from at least one abstract syntax tree that has not been selected before. The levels in the current abstract syntax tree are traversed in ascending order of level. For the first subexpression corresponding to the node in the current level, the mapping table is queried based on the current level and the first subexpression. The mapping table includes at least one subexpression corresponding to each of the at least one level. Based on the query results of the mapping table, the mapping table and the reference count record table used to record the reference count of each subexpression are updated. The above steps are repeated until at least one abstract syntax tree has been selected.
[0035] For example, in order to accurately identify common subexpressions, when updating the mapping relationship table and the reference count record table for recording the reference count of each subexpression based on the query results of the mapping relationship table, if the current level includes the first subexpression in the mapping relationship table, the reference count of the first subexpression recorded in the reference count record table is incremented by 1; if the current level does not include the first subexpression in the mapping relationship table, the current level and the first subexpression are associated and stored in the mapping relationship table, and the reference count of the first subexpression is recorded as 1 in the reference count record table.
[0036] In this embodiment, after at least one abstract syntax tree has been processed, the subexpressions with a reference count greater than 1 in the reference count record table can be used as common subexpressions.
[0037] Further, optionally, in order to better optimize database operation statements, one implementation of selecting a common subexpression from at least one subexpression based on the reference count of each subexpression in the reference count record table is as follows: if the reference count of the second subexpression and the reference count of the third subexpression in the reference count record table are the same, then the third subexpression is determined to be a common subexpression, wherein the second subexpression is any one of the at least one subexpression, and the node corresponding to the third subexpression is the parent node of the node corresponding to the second subexpression.
[0038] For example, the expressions `mul(add(add(a,b),c),10)` and `mul(add(add(a,b),c),20)` have two candidate common subexpressions: `add(a,b)` and `add(add(a,b),c)`. Since the reference count of `add(a,b)` corresponding to the child node is 2, and the reference count of `add(add(a,b),c)` corresponding to the parent node is 2, this indicates that `add(a,b)` is completely nested within `add(add(a,b),c)`, and `add(add(a,b),c)` is the final common subexpression.
[0039] For example, the expressions mul(add(add(a,b),c),10), mul(add(add(a,b),c),20) and mul(add(add(a,b),d),20).
[0040] The child node's corresponding add(a, b) has a reference count of 3, and the parent node 1's corresponding (add(add(a, b), c) has a reference count of 2. This indicates that add(a, b) is not completely nested within add(add(a, b), c). At this point, it is impossible to determine whether add(a, b) or (add(add(a, b), c) belongs to the common subexpression.
[0041] The child node's add(a, b) has a reference count of 3, while the parent node's add(add(a, b), d) has a reference count of 1. This indicates that add(a, b) is not completely nested within add(add(a, b), d). At this point, it is impossible to determine whether add(a, b) or add(add(a, b), d) belongs to the common subexpression.
[0042] In practical applications, expressions may contain complex functions, which makes it difficult to accurately identify more common subexpressions. Optionally, to more accurately identify common subexpressions, before selecting common subexpressions from at least one subexpression included in at least one expression using at least one abstract syntax tree, if the fourth subexpression contains a complex function, and the fourth subexpression is any one of the at least one subexpressions included in at least one expression, then the complex function in the fourth subexpression is replaced with a composite function. A composite function can achieve the functionality of the complex function and is composed of multiple simple functions obtained by decomposing the complex function.
[0043] Specifically, complex functions often contain multiple sub-functions. A complex function can be broken down into a composite function consisting of multiple simpler functions that implement these sub-functions. Common complex functions include functions that manipulate string data types and functions that manipulate complex data types such as arrays, maps, and JSON (JavaScript Object Notation). A map is a data type with key-value pairs.
[0044] For example, network link processing functions such as `url_extract_host()`, `url_extract_path()`, and `url_extract_port()` all need to convert strings into URL (Universal Resource Locator) objects. These complex functions can be broken down into compound functions. Specifically, the `url_extract_host()` function extracts Host information from the URL. The `url_extract_path()` function extracts access path information from the URL. The `url_extract_port()` function extracts port information from the URL.
[0045] For example, the `json_extract()` function is used to extract values from a raw JSON string. This function needs to convert the string into a JSON object. Such complex functions can be broken down into compound functions. Taking the specific `json_extract(content, 'user')` function as an example, this function first converts the `content` (string) to JSON format, and then extracts `user` from the JSON data.
[0046] This complex function can be broken down into json_extract_0(json_parse(content),'user')user, where json_parse(content) means converting content (a string) into JSON format.
[0047] For example, some functions return an array, and extracting a specific index element from the array to create a new field requires constructing a unified Array object before executing such index operation functions. Such complex functions can be broken down into compound functions.
[0048] In this embodiment, after identifying a common subexpression from at least one subexpression of a database operation statement, the common subexpression in the at least one expression is replaced with the execution result marker of the common subexpression to optimize the database operation statement. The execution result marker of the common subexpression is used to indicate the execution result obtained by reusing the sub-execution code corresponding to the common subexpression during the code execution phase.
[0049] For example, consider the expressions `json_extract_0(json_parse(content),'user')user` and `json_extract_0(json_parse(content),'action')action`. Here, `json_parse(content)` is a common subexpression. Assume the common subexpression is denoted as `cse_exression`, and the result of its execution is denoted as `cse_exression_result`. Then, the expression `json_extract_0(json_parse(content),'user')user` is optimized to `json_extract_0(cse_exression_result,'user')user`, and the expression `json_extract_0(json_parse(content),'action')action` is optimized to `json_extract_0(cse_exression_result,'action')action`.
[0050] The technical solution provided in this application, based on the abstract syntax tree corresponding to at least one expression in a database operation statement, selects a common sub-expression from at least one sub-expression. The at least one sub-expression originates from at least one expression, and the common sub-expression in the at least one expression is replaced with the execution result marker of the common sub-expression, thereby optimizing the database operation statement. In this way, during the code execution phase, the sub-execution code corresponding to the common sub-expression only needs to be executed once. When the execution result marker of the common sub-expression is encountered, the execution result obtained from executing the sub-execution code corresponding to the common sub-expression is reused, thus avoiding repeated calculations of duplicate sub-expressions, improving computational efficiency, saving computational resources, and enhancing the query performance of the database system.
[0051] Figure 3 A flowchart illustrating a database operation method provided in an embodiment of this application. See also... Figure 3 The method may include the following steps:
[0052] 301. Generate a logical execution plan based on the first abstract syntax tree of the database operation statement, wherein at least one logical operator in the logical execution plan includes a portion of the expression in the database operation statement.
[0053] 302. For at least one expression corresponding to at least one logical operator, construct a second abstract syntax tree corresponding to at least one expression.
[0054] 303. Using at least one second abstract syntax tree, select a common subexpression from at least one subexpression, wherein the at least one subexpression is derived from at least one expression.
[0055] 304. Replace at least one common subexpression in an expression with the execution result marker of the common subexpression to optimize at least one logical operator.
[0056] 305. Generate executable code based on at least one optimized logical operator and a common subexpression.
[0057] 306. Run the execution code to obtain the operation results of the database operation statement. During the execution of the execution code, the execution result obtained by running the sub-execution code corresponding to the common sub-expression is reused based on the execution result of the common sub-expression.
[0058] In this embodiment, the database operation statement to be executed is parsed to obtain an abstract syntax tree of the database operation statement, which is referred to here as the first abstract syntax tree. A logical execution plan is then generated based on the first abstract syntax tree of the database operation statement. The logical execution plan includes several logical operators, such as the projection operator and the filter operator, which contain several expressions from the database operation statement.
[0059] For example, the SQL statement is select json_extract(content,'user')user,json_extract(content,'action')action from log where json_extract(content,'id')='xiaowang'.
[0060] The Projection operator includes expressions such as `json_extract(content,'user')user` and `json_extract(content,'action')action`. The Scan operator includes `from log`. The Filter operator includes expressions such as `json_extract(content,'id') = 'xiaowang'`.
[0061] Alternatively, to accurately identify common subexpressions, complex functions within the expressions included by logical operators can be decomposed. That is, for a subexpression within an expression, if the subexpression contains a complex function, the complex function in the subexpression is replaced with a composite function. The composite function can perform the functionality of the complex function and is composed of multiple simple functions obtained from the decomposition of the complex function.
[0062] For example, the complex function `json_extract()` can be broken down into `json_extract_0(json_parse(), )`. Therefore,
[0063] Replace the expression `json_extract(content,'user')user` in the projection operator with `json_extract_0(json_parse(content),'user')user`. Replace the expression `json_extract(content,'action')action` in the projection operator with `json_extract_0(json_parse(content),'action')action`. Replace the expression `json_extract(content,'id')='xiaowang'` in the filter operator with `json_extract_0(json_parse(content),'id')='xiaowang'`.
[0064] In this embodiment, for at least one expression corresponding to at least one logical operator, an abstract syntax tree is constructed for each of the at least one expression. This abstract syntax tree is referred to as the second abstract syntax tree. For details on how to select a common sub-expression from at least one sub-expression based on the abstract syntax tree corresponding to each of the at least one expression, please refer to the relevant description in the foregoing embodiments; it will not be repeated here.
[0065] In this embodiment, after identifying the common subexpression, the common subexpression in at least one expression included in the logical operator is replaced with the execution result tag of the common subexpression to optimize at least one logical operator. For example, suppose the common subexpression json_parse(content) is denoted as cse_exression, and the execution result tag of the common subexpression is denoted as cse_exression_result.
[0066] Therefore, the expression `json_extract_0(json_parse(content),'user')user` in the projection operator is replaced with `json_extract_0(cse_exression_result,'user')user`. Similarly, the expression `json_extract_0(json_parse(content),'action')action` in the projection operator is replaced with `json_extract_0(cse_exression_result,'action')action`. Finally, the expression `json_extract_0(json_parse(content),'id')='xiaowang'` in the filter operator is replaced with `json_extract_0(cse_exression_result,'id')='xiaowang'`.
[0067] In this embodiment, after optimizing at least one logical operator, executable code can be generated based on the optimized logical operator and common sub-expression. Further optionally, bytecode generation techniques such as ASM can be used to compile the substituted expression and common sub-expression in the optimized logical operator to obtain bytecode, load the bytecode into the virtual machine's memory, and convert the bytecode into executable code. ASM is a relatively complete Java bytecode manipulation and analysis framework; through the ASM framework, classes can be dynamically generated or the functionality of existing classes can be enhanced.
[0068] Specifically, the bytecode mode is used to dynamically load bytecode into the virtual machine, which effectively avoids virtual function calls, eliminates the need for overall compilation and packaging, and meets the real-time requirements of customers.
[0069] Alternatively, bytecode can be cached periodically to avoid repeated compilation and loading of expressions while ensuring memory utilization, thus guaranteeing the real-time performance of the database system.
[0070] Alternatively, during bytecode generation, the functions and variables corresponding to the common subexpression can be generated separately, and these functions and variables can be dynamically generated when actually used using lazy loading. This more effectively ensures that the sub-executable code corresponding to the common subexpression is executed only once. In this way, if an execution result corresponding to the common subexpression already exists, the existing execution result can be reused.
[0071] Optionally, to fully utilize the database system's hardware resources, according to the data source sharding rules, and based on the data sources corresponding to the input data required by the common sub-expression and the data sources corresponding to the input data required by each sub-expression in the optimized logical operator, the common sub-expression and each sub-expression in the optimized logical operator are scheduled to be executed on computing nodes close to their respective data sources. Among all computing nodes, some computing nodes use data provided by the data source as input data; these computing nodes can be called computing nodes in the local computing stage. Other computing nodes use the output results of other computing nodes as input data; these computing nodes can be called computing nodes in the global computing stage. This avoids processing all data on the same computing node, maximizing parallelism while ensuring correctness.
[0072] In this embodiment, after obtaining the execution code corresponding to the database operation statement, the execution code is run to obtain the operation result of the database operation statement. During the execution of the execution code, the execution result obtained by running the sub-execution code corresponding to the common sub-expression is reused based on the execution result marker of the common sub-expression. For example, the sub-execution code corresponding to the common sub-expression `json_parse(content)` only needs to be executed once, whereas it originally needed to be executed three times. During the code execution phase, the execution result obtained by running the sub-execution code corresponding to the common sub-expression `json_parse(content)` once is reused based on the execution result marker `cse_exression_result`.
[0073] The technical solution provided in this application, based on the abstract syntax tree corresponding to at least one expression in a database operation statement, selects a common sub-expression from at least one sub-expression. The at least one sub-expression originates from at least one expression, and the common sub-expression in the at least one expression is replaced with the execution result marker of the common sub-expression, thereby optimizing the database operation statement. In this way, during the code execution phase, the sub-execution code corresponding to the common sub-expression only needs to be executed once. When the execution result marker of the common sub-expression is encountered, the execution result obtained from executing the sub-execution code corresponding to the common sub-expression is reused, thus avoiding repeated calculations of duplicate sub-expressions, improving computational efficiency, saving computational resources, and enhancing the query performance of the database system.
[0074] Figure 4 A schematic diagram of a database operation statement optimization device provided in this application. See also... Figure 4 The device may include:
[0075] Module 41 is used to construct an abstract syntax tree corresponding to at least one expression in a database operation statement;
[0076] Select module 42 is used to select a common subexpression from at least one subexpression using at least one abstract syntax tree, wherein the at least one subexpression is derived from at least one expression;
[0077] Replacement module 43 is used to replace the common subexpression in at least one expression with the execution result marker of the common subexpression in order to optimize the database operation statement. The execution result marker of the common subexpression is used to indicate the execution result obtained by reusing the sub-execution code corresponding to the common subexpression during the code execution phase.
[0078] Further optionally, the leaf nodes in the abstract syntax tree have the lowest level, and the parent node's level is one level higher than the child node's level. Correspondingly, when the selection module 42 selects a common subexpression from at least one subexpression using at least one abstract syntax tree, it specifically performs the following: for any abstract syntax tree in the at least one abstract syntax tree, it traverses each level of the abstract syntax tree in ascending order of level, and for the first subexpression corresponding to the node in the current traversed level, it queries a mapping table based on the current level and the first subexpression, wherein the mapping table includes at least one subexpression corresponding to each of the at least one level; based on the query result of the mapping table, it updates the mapping table and a reference count record table used to record the reference counts of each subexpression; after all at least one abstract syntax tree has been processed, it selects a common subexpression from at least one subexpression based on the reference counts of each subexpression in the reference count record table.
[0079] Further optionally, when selecting a common subexpression from at least one subexpression based on the reference count of each subexpression in the reference count record table, the selection module 42 is specifically used to: if the reference count of the second subexpression and the reference count of the third subexpression in the reference count record table are the same, then the third subexpression is determined to be a common subexpression, wherein the second subexpression is any one of the at least one subexpression, and the node corresponding to the third subexpression is the parent node of the node corresponding to the second subexpression.
[0080] Optionally, when the selection module 42 updates the mapping relationship table and the reference count record table for recording the reference count of each sub-expression based on the query results of the mapping relationship table, it specifically performs the following: if the current level includes the first sub-expression in the mapping relationship table, the reference count of the first sub-expression recorded in the reference count record table is incremented by 1; if the current level does not include the first sub-expression in the mapping relationship table, the current level and the first sub-expression are associated and stored in the mapping relationship table, and the reference count of the first sub-expression is recorded as 1 in the reference count record table.
[0081] Further optionally, the above apparatus also includes a complex function decomposition module, used to: if the fourth sub-expression includes a complex function, and the fourth sub-expression is any one of at least one sub-expression included in at least one expression, then replace the complex function in the fourth sub-expression with a composite function, the composite function being composed of multiple simple functions obtained by decomposing the complex function.
[0082] Figure 4 The device shown can perform Figure 2 The implementation principle and technical effects of the method shown in the embodiments will not be elaborated further. Regarding the above embodiments... Figure 4 The specific ways in which each module and unit of the device performs operations have been described in detail in the embodiments of the method, and will not be elaborated here.
[0083] Figure 5 A schematic diagram of a database operation device provided in this application. See also... Figure 5 The device may include:
[0084] The first generation module 51 is used to generate a logical execution plan based on the first abstract syntax tree of the database operation statement. At least one logical operator in the logical execution plan includes a part of the expression in the database operation statement.
[0085] Module 52 is used to construct a second abstract syntax tree corresponding to at least one expression for at least one logical operator;
[0086] Select module 53 is used to select a common subexpression from at least one subexpression using at least one second abstract syntax tree, wherein the at least one subexpression is derived from at least one expression;
[0087] Replacement module 54 is used to replace a common subexpression in at least one expression with an execution result marker of the common subexpression in order to optimize at least one logical operator;
[0088] The second generation module 55 is used to generate execution code based on at least one optimized logical operator and a common subexpression;
[0089] The execution module 55 is used to run the execution code to obtain the operation results of the database operation statement. During the execution of the execution code, the execution result obtained by running the sub-execution code corresponding to the common sub-expression is reused based on the execution result of the common sub-expression.
[0090] Optionally, when the second generation module 55 generates executable code based on the optimized at least one logical operator and the common sub-expression, it is specifically used to: compile the replaced expression and the common sub-expression in the optimized at least one logical operator to obtain bytecode; load the bytecode into the memory of the virtual machine, and convert the bytecode into executable code.
[0091] Further optionally, the above apparatus also includes a complex function decomposition module, used to: for any subexpression in at least one expression, if the subexpression includes a complex function, replace the complex function in the subexpression with a composite function, wherein the composite function is composed of multiple simple functions obtained by decomposing the complex function.
[0092] Figure 5 The device shown can perform Figure 3 The implementation principle and technical effects of the method shown in the embodiments will not be elaborated further. Regarding the above embodiments... Figure 3 The specific ways in which each module and unit of the device performs operations have been described in detail in the embodiments of the method, and will not be elaborated here.
[0093] It should be noted that the execution subject of each step of the method provided in the above embodiments can be the same device, or the method can be executed by different devices. For example, the execution subject of steps 201 to 203 can be device A; or the execution subject of steps 201 and 202 can be device A, and the execution subject of step 203 can be device B; and so on.
[0094] Furthermore, in some of the processes described in the above embodiments and accompanying drawings, multiple operations appear in a specific order. However, it should be clearly understood that these operations may not be executed in the order they appear herein, or they may be executed in parallel. The operation numbers, such as 201, 202, etc., are merely used to distinguish different operations and do not represent any execution order. Additionally, these processes may include more or fewer operations, and these operations may be executed sequentially or in parallel. It should be noted that the descriptions such as "first" and "second" in this document are used to distinguish different messages, devices, modules, etc., and do not represent a sequential order, nor do they limit "first" and "second" to different types.
[0095] It should be noted that the user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, data stored, data displayed, etc.) involved in this application are all information and data authorized by the user or fully authorized by all parties. Furthermore, the collection, use and processing of the relevant data must comply with the relevant laws, regulations and standards of the relevant countries and regions, and corresponding operation portals are provided for users to choose to authorize or refuse.
[0096] Figure 6 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Figure 6 As shown, the electronic device includes: a memory 61 and a processor 62;
[0097] Memory 61 is used to store computer programs and can be configured to store various other data to support operation on the computing platform. Examples of this data include instructions for any application or method operating on the computing platform, contact data, phone book data, messages, pictures, videos, etc.
[0098] The memory 61 can be implemented by any type of volatile or non-volatile storage device or a combination thereof, such as static random-access memory (SRAM), electrically erasable programmable read-only memory (EEPROM), erasable programmable read-only memory (EPROM), programmable read-only memory (PROM), read-only memory (ROM), magnetic storage, flash memory, magnetic disk, or optical disk.
[0099] Processor 62, coupled to memory 61, is used to execute computer programs in memory 61 for: performing steps in a database operation statement optimization method or a database operation method.
[0100] Furthermore, such as Figure 6 As shown, the electronic device also includes other components such as a communication component 63, a display 64, a power supply component 65, and an audio component 66. Figure 6 The diagram only shows some components and does not mean that the electronic device includes only these components. Figure 6 The components shown. Additionally... Figure 6 The components within the dashed box are optional, not mandatory, and their specific requirements depend on the product form of the electronic device. The electronic device in this embodiment can be a desktop computer, laptop computer, smartphone, or IoT (Internet of Things) device, or a server-side device such as a conventional server, cloud server, or server array. If the electronic device in this embodiment is a desktop computer, laptop computer, or smartphone, it may include... Figure 6The components within the dashed box; if the electronic device in this embodiment is implemented as a conventional server, cloud server, or server array, etc., it may be omitted. Figure 6 The component within the dashed box.
[0101] For a detailed description of the implementation process of each action by the processor, please refer to the relevant descriptions in the foregoing method embodiments or device embodiments, which will not be repeated here.
[0102] Accordingly, embodiments of this application also provide a computer-readable storage medium storing a computer program, which, when executed, can perform the steps that can be executed by an electronic device in the above method embodiments.
[0103] Accordingly, this application also provides a computer program product, including a computer program / instructions, which, when executed by a processor, enables the processor to perform the steps that can be executed by an electronic device in the above method embodiments.
[0104] The aforementioned communication components are configured to facilitate wired or wireless communication between the device containing the communication components and other devices. The device containing the communication components can access wireless networks based on communication standards, such as WiFi, 2G, 3G, 4G / LTE, 5G, or combinations thereof. In one exemplary embodiment, the communication components receive broadcast signals or broadcast-related information from an external broadcast management system via a broadcast channel. In one exemplary embodiment, the communication components also include a Near Field Communication (NFC) module to facilitate short-range communication. For example, the NFC module may be implemented based on Radio Frequency Identification (RFID), Infrared Data Association (IrDA), Ultra Wide Band (UWB), Bluetooth (BT), and other technologies.
[0105] The aforementioned display includes a screen, which may include a Liquid Crystal Display (LCD) and a touch panel (TP). If the screen includes a touch panel, the screen can be implemented as a touchscreen to receive input signals from the user. The touch panel includes one or more touch sensors to sense touches, swipes, and gestures on the touch panel. The touch sensors can sense not only the boundaries of touch or swipe actions but also the duration and pressure associated with the touch or swipe operation.
[0106] The aforementioned power supply components provide power to various components within the device in which they reside. These power supply components may include a power management system, one or more power sources, and other components associated with generating, managing, and distributing power to the device in which they reside.
[0107] The aforementioned audio component can be configured to output and / or input audio signals. For example, the audio component includes a microphone (MIC) configured to receive external audio signals when the device containing the audio component is in an operating mode, such as call mode, recording mode, or voice recognition mode. The received audio signals can be further stored in memory or transmitted via a communication component. In some embodiments, the audio component also includes a speaker for outputting audio signals.
[0108] Those skilled in the art will understand that embodiments of this application can be provided as methods, systems, or computer program products. Therefore, this application can take the form of a completely hardware embodiment, a completely software embodiment, or an embodiment combining software and hardware aspects. Furthermore, this application can take the form of a computer program product embodied on one or more computer-readable storage media (including but not limited to disk storage, CD-ROM, optical storage, etc.) containing computer-usable program code.
[0109] This application is described with reference to flowchart illustrations and / or block diagrams of methods, apparatus (systems), and computer program products according to embodiments of this application. It will be understood that each block of the flowchart illustrations and / or block diagrams, and combinations of blocks in the flowchart illustrations and / or block diagrams, can be implemented by computer program instructions. These computer program instructions can be provided to a processor of a general-purpose computer, special-purpose computer, embedded processor, or other programmable data processing apparatus to produce a machine, such that the instructions, which execute via the processor of the computer or other programmable data processing apparatus, generate instructions for implementing the flowchart... Figure 1 One or more processes and / or boxes Figure 1 A device that provides the functions specified in one or more boxes.
[0110] These computer program instructions may also be stored in a computer-readable storage medium that can direct a computer or other programmable data processing device to function in a particular manner, such that the instructions stored in the computer-readable storage medium produce an article of manufacture including instruction means, which are implemented in a process Figure 1 One or more processes and / or boxes Figure 1 The function specified in one or more boxes.
[0111] These computer program instructions may also be loaded onto a computer or other programmable data processing equipment to cause a series of operational steps to be performed on the computer or other programmable equipment to produce a computer-implemented process, thereby providing instructions that execute on the computer or other programmable equipment for implementing the process. Figure 1 One or more processes and / or boxes Figure 1 The steps of the function specified in one or more boxes.
[0112] In a typical configuration, a computing device includes one or more processors (central processing unit, CPU), input / output interfaces, network interfaces, and memory.
[0113] Memory may include non-persistent storage in computer-readable media, such as random access memory (RAM) and / or non-volatile memory, such as read-only memory (ROM) or flash RAM. Memory is an example of computer-readable media.
[0114] Computer-readable media, including both permanent and non-permanent, removable and non-removable media, can store information using any method or technology. Information can be computer-readable instructions, data structures, program modules, or other data. Examples of computer storage media include, but are not limited to, phase-change RAM (PRAM), static random-access memory (SRAM), dynamic random-access memory (DRAM), other types of random access memory (RAM), read-only memory (ROM), electrically erasable programmable read-only memory (EEPROM), flash memory or other memory technologies, CD-ROM, digital versatile disc (DVD) or other optical storage, magnetic tape, magnetic disk storage or other magnetic storage devices, or any other non-transfer medium that can be used to store information accessible by a computing device. As defined in this article, computer-readable media do not include transient media, such as modulated data signals and carrier waves.
[0115] It should also be noted that the terms "comprising," "including," or any other variations thereof are intended to cover non-exclusive inclusion, such that a process, method, article, or apparatus that comprises a list of elements includes not only those elements but also other elements not expressly listed, or elements inherent to such a process, method, article, or apparatus. Without further limitation, an element defined by the phrase "comprising one..." does not exclude the presence of other identical elements in the process, method, article, or apparatus that includes said element.
[0116] The above description is merely an embodiment of this application and is not intended to limit the scope of this application. Various modifications and variations can be made to this application by those skilled in the art. Any modifications, equivalent substitutions, improvements, etc., made within the spirit and principles of this application should be included within the scope of the claims of this application.
Claims
1. A method for optimizing database operation statements, characterized in that, include: Construct an abstract syntax tree corresponding to at least one expression in a database operation statement; The leaf nodes in the abstract syntax tree have the lowest level, and the parent node has a level higher than the child node. For any abstract syntax tree in at least one abstract syntax tree, traverse each level of the abstract syntax tree in ascending order of level. For the first sub-expression corresponding to the node in the current level, query the mapping table according to the current level and the first sub-expression. The mapping table includes at least one sub-expression corresponding to each of the at least one level. If the mapping table shows that the current level includes the first sub-expression, increment the reference count of the first sub-expression recorded in the reference count record table by 1. If the mapping table does not show that the current level includes the first sub-expression, store the current level and the first sub-expression together in the mapping table, and record the reference count of the first sub-expression as 1 in the reference count record table. After all at least one abstract syntax tree has been processed, select a common sub-expression from the at least one sub-expression according to the reference count of each sub-expression in the reference count record table. The at least one sub-expression originates from the at least one expression, and the sub-expression includes functions. The database operation statement is optimized by replacing the common subexpression in the at least one expression with the execution result marker of the common subexpression. The execution result marker of the common subexpression is used to indicate the execution result obtained by reusing the sub-execution code corresponding to the common subexpression during the code execution phase.
2. The method according to claim 1, characterized in that, Based on the reference count of each subexpression in the reference count record table, a common subexpression is selected from the at least one subexpression, including: If the reference count of the second subexpression and the reference count of the third subexpression are the same in the reference count record table, then the third subexpression is determined to be a common subexpression, wherein the second subexpression is any one of the at least one subexpression, and the node corresponding to the third subexpression is the parent node of the node corresponding to the second subexpression.
3. The method according to claim 1, characterized in that, Before selecting a common subexpression from at least one subexpression using at least one abstract syntax tree, the process also includes: If the fourth subexpression includes a complex function, and the fourth subexpression is any one of the at least one subexpressions included in the at least one expression, then the complex function in the fourth subexpression is replaced with a composite function, which is composed of multiple simple functions obtained by decomposing the complex function.
4. A database operation method, characterized in that, include: A logical execution plan is generated based on the first abstract syntax tree of the database operation statement, wherein at least one logical operator in the logical execution plan includes a portion of the expression in the database operation statement. For at least one expression corresponding to at least one logical operator, construct a second abstract syntax tree corresponding to at least one expression; In the second abstract syntax tree, the leaf nodes have the lowest level, and the parent node has a level higher than the child node. For any second abstract syntax tree in at least one second abstract syntax tree, traverse each level of the second abstract syntax tree in ascending order of level. For the first sub-expression corresponding to the node in the current level, query the mapping table according to the current level and the first sub-expression. The mapping table includes at least one sub-expression corresponding to each level. If the current level is found to include the first sub-expression in the mapping table, increment the reference count of the first sub-expression recorded in the reference count record table by 1. If the current level is not found to include the first sub-expression in the mapping table, store the current level and the first sub-expression together in the mapping table, and record the reference count of the first sub-expression as 1 in the reference count record table. After all at least one second abstract syntax tree has been processed, select a common sub-expression from the at least one sub-expression according to the reference count of each sub-expression in the reference count record table. The at least one sub-expression originates from the at least one expression, and the sub-expression is a function. Replace the common subexpression in the at least one expression with the execution result marker of the common subexpression to optimize at least one logical operator; Execution code is generated based on at least one optimized logical operator and the common subexpression; The execution code is run to obtain the operation result of the database operation statement. During the execution of the execution code, the execution result obtained by running the sub-execution code corresponding to the common sub-expression is reused according to the execution result of the common sub-expression.
5. The method according to claim 4, characterized in that, Generating executable code based on at least one optimized logical operator and the common subexpression includes: Compile the replaced expression and the common subexpression in at least one optimized logical operator to obtain bytecode; The bytecode is loaded into the virtual machine's memory and then converted into the executable code.
6. The method according to claim 4, characterized in that, Before constructing the second abstract syntax tree corresponding to at least one expression for at least one logical operator, the process also includes: For any subexpression in at least one expression, if the subexpression includes a complex function, the complex function in the subexpression is replaced with a composite function, wherein the composite function is composed of multiple simple functions obtained by decomposing the complex function.
7. An electronic device, characterized in that, include: Memory and processor; The memory is used to store computer programs; The processor is coupled to the memory for executing the computer program to perform the steps of the method according to any one of claims 1-6.
8. A computer-readable storage medium storing a computer program, characterized in that, When the computer program is executed by a processor, it causes the processor to perform the steps of the method according to any one of claims 1-6.
Citation Information
Patent Citations
Running methods and devices and compiling method and device of stored procedure and database system
CN107798025A