Database query statement processing method, device, medium and product

By extracting and setting the truncation value expression as plan-level metadata in the database query statement, and selecting the TOP-N sorting algorithm, the optimization deficiencies of truncation-type queries in the existing technology are solved, and query efficiency is improved.

CN123048595APending Publication Date: 2026-11-06CETC JINCANG (BEIJING) TECH CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202611264042.5
Authority / Receiving Office
CN · China
Patent Type
Applications(China)
Current Assignee / Owner
Filing Date
2026-08-19
Publication Date
2026-11-06

AI Technical Summary

Technical Problem

In existing database systems, the optimization phase of truncated queries fails to fully utilize truncated information, resulting in the use of a full sorting algorithm for sorting operations, causing unnecessary overhead.

Method used

Extract the truncation value expression from the query statement and set it as plan-level metadata. Select the sorting algorithm based on the truncation value expression, and use the TOP-N sorting algorithm to avoid full sorting.

Benefits of technology

By sensing truncation information during the optimization phase and selecting an appropriate sorting algorithm, unnecessary computational overhead is reduced and query efficiency is improved.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN123048595A_ABST
    Figure CN123048595A_ABST
Patent Text Reader

Abstract

This invention provides a method, device, medium, and product for processing database query statements. The method for processing database query statements includes: receiving a query statement; extracting the truncation value expression corresponding to the truncation operation if the query statement contains sorting and truncation operations; setting the truncation value expression as plan-level metadata; and selecting the sorting algorithm to be used for the sorting operation based on the truncation value expression. When the optimizer generates a physical execution plan containing sorting operators, the sorting operator node can access the truncation value expression stored in the plan-level metadata during the initialization phase. This allows the sorting operator node to evaluate the applicability of the TOP-N sorting algorithm based on the truncation value expression during the cost estimation and algorithm selection phases, thereby avoiding the unnecessary overhead caused by the optimizer's inability to perceive truncation semantics and thus having to use a full sorting algorithm for the sorting operator in the prior art.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] This invention relates to the field of database technology, and in particular to a method, device, medium, and product for processing database query statements. Background Technology

[0002] During database usage, clients can use query statements to retrieve data from the database. Among these, truncated queries, implemented using clauses such as LIMIT, are a special type of query. Truncated queries are typically used in conjunction with the ORDER BY clause to retrieve the first N results sorted according to a specified rule.

[0003] In existing database systems, there is a problem with insufficient propagation of truncation information during the optimization phase of the query optimizer and execution engine's processing of the aforementioned truncation-type queries. Specifically, the truncation value corresponding to the truncation clause is only perceived during the execution phase. However, during the optimization phase or operator selection phase, the ORDER BY operator often still evaluates according to the cost model of "full sorting," failing to fully utilize the semantic information of "only needing the first N results." Summary of the Invention

[0004] One object of the present invention is to provide a method, apparatus, medium and product for processing database query statements that can solve any of the above problems.

[0005] Specifically, the present invention provides a method for processing database query statements, including: Received query statement; If the query statement contains sorting and truncation operations, extract the truncation value expression corresponding to the truncation operation; Set the truncation value expression as a plan-level metadata; The sorting algorithm to be used in the sorting operation is selected based on the truncation value expression.

[0006] Optionally, the step of selecting the sorting algorithm to be used in the sorting operation based on the truncation value expression includes: Check if the truncation value expression is a constant value. If it is, estimate the cost of the TOP-N sorting algorithm based on the constant value. If not, estimate the cost of the TOP-N sorting algorithm based on the estimated value of the truncation value expression.

[0007] Optionally, the step of estimating the cost of the TOP-N sorting algorithm based on the truncation value expression includes: If, based on cost, the TOP-N sorting algorithm is initially chosen as the sorting algorithm for the sorting operation, Set the truncation value expression to be evaluated first during the statement execution phase; Check if the truncation value expression can be computed to a constant value. If so, perform the step of estimating the cost of the TOP-N sorting algorithm based on the constant value. If not, select a sorting algorithm other than the TOP-N sorting algorithm.

[0008] Optionally, the step of estimating the cost of the TOP-N sorting algorithm based on constant values ​​is followed by: Determine whether the cost and available memory of the TOP-N sorting algorithm meet the preset conditions. If yes, select the TOP-N sorting algorithm as the sorting algorithm to be used for the sorting operation. If not, select a sorting algorithm other than the TOP-N sorting algorithm.

[0009] Optionally, the step of selecting the TOP-N sorting algorithm as the sorting algorithm to be used in the sorting operation includes: Configure a bounded data structure with a constant capacity in memory for sorting operations; Perform sorting operations using bounded data structures.

[0010] Optionally, the steps for extracting the truncation value expression corresponding to the truncation operation include: Get the original expression for the truncation operation; The original expression is simplified to obtain the truncated value expression.

[0011] Optionally, if the truncation expression can be used to calculate a constant value, the correspondence between the truncation expression and the constant value obtained in the stage is stored in a preset buffer. The steps to check if a truncation value expression is a constant include: Check if the truncated value expression itself is a constant value. If it is, determine that the truncated value expression is a constant value. If not, check if there is a constant value corresponding to the truncated value expression in the preset buffer. If it is, determine that the truncated value expression is a constant value. If not, determine that the truncated value expression is not a constant value.

[0012] According to another aspect of the present invention, a computer device is also provided, including a memory, a processor, and a computer executable program stored in the memory and running on the processor, wherein the processor, when executing the computer executable program, implements a method for processing a database query statement according to any of the preceding claims.

[0013] According to another aspect of the present invention, a computer-readable storage medium is also provided, on which a computer-executable program is stored, which, when executed by a processor, implements the processing method for a database query statement according to any one of the preceding claims.

[0014] According to another aspect of the present invention, a computer program product is also provided, comprising a computer executable program that, when executed by a processor, implements a method for processing a database query statement according to any of the preceding claims.

[0015] The database query processing method of this invention, upon receiving a query statement, extracts the truncation value expression corresponding to the truncation operation if the query statement contains sorting and truncation operations. This truncation value expression is then set as plan-level metadata, and the sorting algorithm used for the sorting operation is selected based on the truncation value expression. In other words, a field or attribute for storing the truncation value expression is added at the global level of the execution plan's data structure. This field or attribute does not belong to any specific plan node but is associated with the entire execution plan, allowing any plan node in the execution plan to obtain the truncation value expression by accessing this plan-level metadata. In this way, when the optimizer generates a physical execution plan containing sorting operators, the sorting operator node can, during the initialization phase, access the truncation value expression stored in the plan-level metadata, thereby recognizing that the current query contains a truncation operation and the specific form (constant or expression) of the truncation value. Therefore, the sorting operator node can evaluate the applicability of the TOP-N sorting algorithm based on the truncation value expression during the cost estimation and algorithm selection stages, thereby avoiding the unnecessary overhead caused by the optimizer being unable to perceive the truncation semantics and having to use the full sorting algorithm on the sorting operator in the existing technology.

[0016] The above and other objects, advantages and features of the present invention will become more apparent to those skilled in the art from the following detailed description of specific embodiments of the invention in conjunction with the accompanying drawings. Attached Figure Description

[0017] The following sections will describe some specific embodiments of the invention in detail by way of example and not limitation, with reference to the accompanying drawings. The same reference numerals in the drawings denote the same or similar parts or portions. Those skilled in the art should understand that these drawings are not necessarily drawn to scale. In the drawings: Figure 1 This is a schematic flowchart of a database query statement processing method according to an embodiment of the present invention; Figure 2 This is a schematic flowchart illustrating the sorting algorithm used in the sorting operation based on the truncation value expression in a database query statement processing method according to an embodiment of the present invention. Figure 3 This is a schematic diagram of a computer device according to an embodiment of the present invention; Figure 4 This is a schematic diagram of a computer-readable storage medium according to an embodiment of the present invention; Figure 5This is a schematic diagram of a computer program product according to an embodiment of the present invention. Detailed Implementation

[0018] Those skilled in the art should understand that the embodiments described below are merely a part of the embodiments of the present invention, and not all of the embodiments of the present invention. These partial embodiments are intended to explain the technical principles of the present invention and are not intended to limit the scope of protection of the present invention. Based on the embodiments provided by the present invention, all other embodiments obtained by those skilled in the art without creative effort should still fall within the scope of protection of the present invention.

[0019] It should be noted that the logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of executable instructions for implementing logical functions, and can be specifically implemented in any computer-readable medium for use by, or in conjunction with, an instruction execution system, apparatus or device (such as a computer-based system, a processor-included system or other system that can fetch and execute instructions from, an instruction execution system, apparatus or device).

[0020] The flowcharts provided in this invention are not intended to indicate that the operations of the method will be performed in any particular order, or that all operations of the method are included in every case. Furthermore, the method may include additional operations. Within the scope of the technical concept provided by the method in this embodiment, additional variations can be made to the above method.

[0021] like Figure 1 As shown, in one embodiment of the present invention, the method for processing database query statements generally includes: Step S101: Receive the query statement.

[0022] Specifically, a query statement is a query statement sent from a client application or database management tool to the database server.

[0023] Step S102: If the query statement contains sorting and truncation operations, extract the truncation value expression corresponding to the truncation operation.

[0024] Specifically, upon receiving a query statement, the database parser performs lexical and syntactic analysis on the query statement to generate an abstract syntax tree. During the syntactic analysis process, the parser identifies whether the query statement contains sorting operations (ORDER BY clause) and truncation operations (such as LIMIT clause or FETCH FIRST clause).

[0025] If both sorting and truncation operations exist, the syntax node corresponding to the truncation operation is located in the abstract syntax tree, and the truncation value expression is extracted from that node.

[0026] Specifically, this step includes: obtaining the original expression for the truncation operation, and simplifying the original expression to obtain the truncation value expression.

[0027] Specifically, the original expression for the truncation operation can take many forms, including constants (such as 10 in "LIMIT 10"), parameters, bind variables (such as "?" in "LIMIT ?"), subqueries (such as the subquery in "LIMIT (SELECT max_limit FROM config)"), or arithmetic expressions (such as "LIMIT 2"). 10+5”).

[0028] During extraction, the parser traverses the abstract syntax tree, locates the truncation operation node, and retrieves the original expression from that node. Then, the original expression is simplified; simplification is the process of converting complex expressions into simpler forms, such as constant folding and function evaluation. Constant folding involves directly calculating constants that can be mathematically computed, for example, folding "2..." "10+5" is simplified to 25. Function calculations yield the result of the function; for example, "LENGTH('abcde')" is simplified to 5. The simplified expression is used as a truncation expression. Expressions that were originally constant values ​​remain constant values ​​after simplification. In other words, truncation expressions are either constant values ​​(originally constant values ​​or simplified to constant values) or non-constant values.

[0029] Step S103: Set the truncation value expression as the plan-level metadata.

[0030] Specifically, plan-level metadata refers to global attributes or constraints bound to the entire execution plan, which differs from local information belonging only to a specific plan node. In a database system, the execution plan is represented by a tree data structure, with each node representing a specific execution operation (such as scan, sort, join, etc.). Setting the truncation value expression as plan-level metadata means adding a field or attribute at the global level of the data structure when the optimizer generates the execution plan, to store the truncation value expression extracted in step S102. This field or attribute does not belong to any specific operator node in the execution plan, but exists as an inherent attribute of the entire execution plan, allowing any operator node in the execution plan (including sorting operator nodes) to access the truncation value expression during optimization and execution.

[0031] By elevating the truncation value expression to plan-level meta-information, the truncation value is no longer a local piece of information visible only at the truncation operator node, but becomes a constraint that is globally visible throughout the entire execution plan.

[0032] Step S104: Select the sorting algorithm to be used in the sorting operation based on the truncation value expression.

[0033] Specifically, when the sorting operator node generates the physical execution plan, it first accesses the plan-level metadata of the execution plan. It checks if a truncation value expression exists; if so, it retrieves the stored truncation value expression.

[0034] The sorting operator node determines whether the TOP-N sorting algorithm is suitable for the current sorting operation based on the truncation value expression. The TOP-N sorting algorithm is a sorting algorithm that only needs to maintain a bounded data structure of capacity N (such as a min-heap or selection tree). It dynamically eliminates invalid elements while scanning the input data, thus avoiding sorting all the data. Its time complexity is O(M log N), where M is the input data size and N is the truncation value. When the truncation value N is much smaller than the input data size M, the performance of the TOP-N sorting algorithm is significantly better than the full sorting algorithm. After judging based on the truncation value expression, the sorting operator node selects either the TOP-N sorting algorithm or the full sorting algorithm as the execution algorithm for the current sorting operation.

[0035] It should be noted that the "TOP-N sorting algorithm" in the technical solution of this invention refers to a sorting algorithm based on a bounded data structure, the capacity of which is determined by the truncation value N, including but not limited to specific implementations such as TOP-N sorting based on a min-heap and TOP-N sorting based on a selection tree. Those skilled in the art should understand that any existing algorithm that uses the truncation value N to limit the amount of data to be sorted falls within the scope of the TOP-N sorting algorithm referred to in this invention.

[0036] Reference Figure 2 As shown, specifically, the step of selecting the sorting algorithm to be used in the sorting operation based on the truncation value expression further includes: Step S201: Check if the truncation value expression is a constant value. If yes, proceed to step S202; otherwise, proceed to step S203.

[0037] Specifically, after obtaining the truncation value expression, the sorting operator node analyzes the expression to determine whether it is a constant value.

[0038] Step S202: Estimate the cost of the TOP-N sorting algorithm based on constant values.

[0039] Specifically, when the truncation value expression is a constant value, that is, the sorting operator node can obtain a definite value N. Based on this definite value N, the sorting operator node calculates the execution cost of the TOP-N sorting algorithm, thereby determining the applicability of the TOP-N sorting algorithm. Since N is a definite value, this cost estimation result is relatively accurate.

[0040] It should be noted that the technique for estimating the cost of the algorithm is existing technology and will not be elaborated here.

[0041] Step S203: Estimate the cost of the TOP-N sorting algorithm based on the estimated value of the truncation value expression.

[0042] Specifically, when the truncation value expression is not a constant value, the sorting operator cannot obtain a definite value for N. In this case, the sorting operator estimates the return value of the truncation value expression based on the database's statistical information. For example, if the truncation value expression is a subquery "SELECT max_limit FROM config", the optimizer estimates its return value as a value N_est based on the statistical information of the tables involved in the subquery (such as the number of table rows, column value distribution, etc.). Based on this estimated value N_est, the sorting operator calculates the estimated cost of the TOP-N sorting algorithm in the same way as in step S202. Since N_est is an estimated value rather than a definite value, the cost estimation result has a certain degree of uncertainty, but it can still provide a reference for the selection of the sorting algorithm.

[0043] The specific method for obtaining the estimated value is as follows: The database optimizer maintains statistical information for each data table, including the number of rows in the table, the number of distinct values ​​in each column (cardinality), and histograms of column value distribution. For truncated value expressions in the form of subqueries, the optimizer estimates the number of rows returned by the subquery based on the query conditions and the statistical information of the relevant columns, using the cardinality estimation method. This estimated number of rows is then used as the estimated value of the truncated value expression. For truncated value expressions in the form of bind variables, since the values ​​of the bind variables are unknown during the optimization phase, the optimizer uses either the system's default value or statistical values ​​based on historical execution records as the estimated value.

[0044] In this embodiment, upon receiving a query statement, if the query statement contains both sorting and truncation operations, the truncation value expression corresponding to the truncation operation is extracted and set as plan-level metadata. The sorting algorithm used for the sorting operation is then selected based on the truncation value expression. In other words, a field or attribute is added at the global level of the execution plan's data structure to store the truncation value expression. This field or attribute does not belong to any specific plan node but is associated with the entire execution plan, allowing any plan node in the execution plan to obtain the truncation value expression by accessing this plan-level metadata. In this way, when the optimizer generates a physical execution plan containing sorting operators, the sorting operator node can, during the initialization phase, access the truncation value expression stored in the plan-level metadata to perceive that the current query contains a truncation operation and the specific form (constant or expression) of the truncation value. Therefore, the sorting operator node can evaluate the applicability of the TOP-N sorting algorithm based on this truncation value expression during the cost estimation and algorithm selection phases, thus avoiding the unnecessary overhead caused by the optimizer's inability to perceive truncation semantics and its reliance on a full sorting algorithm for the sorting operator, as in existing technologies.

[0045] Furthermore, when the truncation value expression is not a constant value, the cost of the TOP-N sorting algorithm is estimated based on the estimated value of the truncation value expression, thus providing a rough estimate of the cost of the TOP-N sorting algorithm, rather than directly abandoning the TOP-N sorting algorithm because a constant value cannot be obtained. This helps to avoid the situation of mistakenly abandoning the more efficient TOP-N sorting algorithm.

[0046] Continue to refer to Figure 2 As shown, further, after estimating the cost of the TOP-N sorting algorithm based on the truncation value expression, the process also includes: if the TOP-N sorting algorithm is initially selected as the sorting algorithm for the sorting operation based on the cost, Step S204: Set the truncation value expression to be evaluated first during the statement execution phase.

[0047] Specifically, after obtaining the estimated cost of the TOP-N sorting algorithm, the sorting operator node compares this estimated cost with the costs of other sorting algorithms. If the estimated cost of the TOP-N sorting algorithm is less than the costs of other sorting algorithms, the sorting operator node initially selects the TOP-N sorting algorithm as a candidate algorithm. However, this selection is still a preliminary decision, as the true value of the truncation value expression has not yet been determined.

[0048] After initially selecting the TOP-N sorting algorithm, the sorting operator node marks the truncated value expression as "preferred for calculation during statement execution". This mark, as an attribute in the execution plan, instructs the execution engine to prioritize the truncated value expression to obtain its true value when executing the query, before performing the scan operation of the main query.

[0049] Step S205: Check whether the truncation value expression can be calculated to obtain a constant value. If yes, proceed to step S202; otherwise, proceed to step S206.

[0050] Specifically, during the execution phase, before initiating the scan operation of the main query, the execution engine prioritizes executing the truncation value expression marked as "prioritized for statement execution phase". After executing this expression, a result is obtained. Then, the execution engine checks whether the result is a constant value. If a constant value is successfully obtained, step S202 is executed.

[0051] In other words, after obtaining the constant value, the cost of the TOP-N sorting algorithm is re-verified based on the constant value, thereby more accurately confirming the feasibility of the TOP-N sorting algorithm and avoiding errors in the early cost estimation using the estimated value.

[0052] If an error occurs or the return value is empty, it is determined that the constant value cannot be calculated, and step S206 is executed.

[0053] Step S206: Select a sorting algorithm other than the TOP-N sorting algorithm.

[0054] Specifically, if the truncation value expression cannot be calculated into a constant value during the execution phase, the sorting operator node abandons the TOP-N sorting algorithm and selects a sorting algorithm other than the TOP-N sorting algorithm, such as the full sorting algorithm, as the execution algorithm for the sorting operation.

[0055] In this embodiment, after estimating the cost of the TOP-N sorting algorithm based on the estimated value of the truncation value expression, if the initial decision is to adopt the TOP-N sorting algorithm, the actual value of the expression is calculated first during the execution phase after the initial decision is passed, and the cost is re-estimated accurately based on the actual value. This allows for a reasonable path selection even when the information is incomplete during the optimization phase, avoiding the erroneous rejection of the TOP-N sorting algorithm while ensuring the accuracy of the final decision.

[0056] Continue to refer to Figure 2 As shown, in one embodiment, after the step of estimating the cost of the TOP-N sorting algorithm based on constant values, the method further includes: Step S207: Determine whether the cost of the TOP-N sorting algorithm and the available memory meet the preset conditions. If yes, proceed to step S208; otherwise, proceed to step S206.

[0057] Specifically, after obtaining the cost estimate for the TOP-N sorting algorithm based on accurate constant values, the sorting operator node also needs to obtain the available memory of the current system. During execution, the TOP-N sorting algorithm requires maintaining a bounded data structure of capacity N, with a memory footprint of approximately N × RowSize, where RowSize is the size of a single row. If the available memory of the current system is insufficient to accommodate this bounded data structure (i.e., available memory < N × RowSize), the TOP-N sorting algorithm will be unable to complete the sorting operation in memory. Forcing execution will result in data spill to disk, thus reducing performance.

[0058] Therefore, the sorting operator node is set with the following preset conditions: the cost of the TOP-N sorting algorithm is less than the cost of other sorting algorithms, and the current available memory is not less than the memory usage required by the TOP-N sorting algorithm. When both conditions are met, the TOP-N sorting algorithm is selected as the sorting operation algorithm; when either condition is not met, another sorting algorithm is selected.

[0059] Step S208: Select the TOP-N sorting algorithm as the sorting algorithm used in the sorting operation.

[0060] In this embodiment, the decision on whether to ultimately adopt the TOP-N algorithm is made by comprehensively considering the cost and available memory, which improves the robustness of the solution in the actual operating environment.

[0061] After selecting the TOP-N sorting algorithm as the sorting algorithm, a bounded data structure with a constant capacity can be configured in memory for the sorting operation. This bounded data structure is then used to perform the sorting operation. In other words, during the scanning process, the bounded data structure is first filled, and then elements are dynamically removed from the bounded data structure based on comparisons according to the sorting criteria.

[0062] In another embodiment, if the truncated value expression can be used to calculate a constant value, the correspondence between the truncated value expression and the calculated constant value is stored in a preset buffer.

[0063] Specifically, the database system maintains a pre-defined cache in memory to store the correspondence between truncated value expressions and their calculated constant values. When the constant value of a truncated value expression is calculated during execution, the system uses the truncated value expression (or its hash value) as the key and the calculated constant value as the value, storing it in the pre-defined cache. The cache can be implemented using a hash table or a similar data structure to support fast lookup operations.

[0064] Accordingly, the steps to check whether the truncation value expression is a constant value include: Check if the truncated value expression itself is a constant value. Specifically, first check the syntax of the truncated value expression; if it is a constant value, then it is directly determined to be a constant value. If not, check if a constant value corresponding to the truncated value expression exists in the preset cache. Specifically, if the truncated value expression itself is not a constant value (for example, it is a subquery), then calculate the hash value of the expression and use this hash value as the key to look up the preset cache. If yes, the truncated value expression is determined to be a constant value; otherwise, the truncated value expression is determined to be not a constant value.

[0065] With the above caching mechanism, when the same truncation value expression is encountered in subsequent queries, its corresponding constant value can be directly obtained from the cache, without having to repeat the expression calculation, thus reducing redundant calculation overhead. It is especially suitable for optimizing a large number of repeated queries in parameterized query scenarios.

[0066] This embodiment also provides a computer device and a computer-readable storage medium. Figure 3 This is a schematic diagram of a computer device 10 according to an embodiment of the present invention. Figure 4 This is a schematic diagram of a computer-readable storage medium 20 according to an embodiment of the present invention.

[0067] The computer device 10 may include a memory 110, a processor 120, and a computer-executable program 11 stored on the memory 110 and running on the processor 120. When the processor 120 executes the computer-executable program 11, it implements the database query statement processing method of any of the above embodiments.

[0068] The computer-readable storage medium 20 stores a computer-executable program 11 thereon, which, when executed by a processor, implements the database query statement processing method of any of the above embodiments.

[0069] This embodiment also provides a computer program product. Figure 5 This is a schematic diagram of a computer program product 30 according to an embodiment of the present invention. The computer program product 30 includes a computer executable program 11, which, when executed by a processor 120, implements the processing method for any of the database query statements described above.

[0070] Specifically, the computer executable program 11 used to perform the operations of the present invention may be assembly instructions, instruction set architecture (ISA) instructions, computer instructions, computer-related instructions, microcode, firmware instructions, status setting data, or source code or object code written in any combination of one or more programming languages.

[0071] For the purposes of this embodiment, the computer-readable storage medium 20 can be any means capable of containing, storing, communicating, propagating, or transmitting a program for use by or in conjunction with an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of computer-readable media include: an electrical connection having one or more wires (electronic device), a portable computer disk drive (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and editable read-only memory (EPROM or flash memory), fiber optic devices, and portable optical disc read-only memory (CDROM). Furthermore, the computer-readable storage medium 20 can even be paper or other suitable media on which the program can be printed, since the program can be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, interpreting, or otherwise processing as necessary, and then stored in a computer memory.

[0072] It should be understood that various parts of the present invention can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiments, multiple steps or methods can be implemented using software or firmware stored in memory and executed by a suitable instruction execution system.

[0073] Computer device 10 can be, for example, a server, desktop computer, laptop computer, tablet computer, or smartphone. In some examples, computer device 10 can be a cloud acquisition node. Computer device 10 can be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Typically, program modules can include routines, programs, object programs, components, logic, data structures, etc., that perform specific tasks or implement specific abstract data types. Computer device 10 can be implemented in a distributed cloud acquisition environment where tasks are performed by remote processing devices linked via a communication network. In a distributed cloud acquisition environment, program modules can reside on local or remote acquisition system storage media, including storage devices.

[0074] Computer device 10 may include a processor 120 adapted to execute stored instructions and a memory 110 that provides temporary storage space for the operation of said instructions during operation. Processor 120 may be a single-core processor, a multi-core processor, an acquisition cluster, or any other configuration. Memory 110 may include random access memory (RAM), read-only memory, flash memory, or any other suitable storage system.

[0075] The processor 120 can be connected via a system interconnect (e.g., PCI, PCI-Express, etc.) to an I / O interface (input / output interface) suitable for connecting the computer device 10 to one or more I / O devices (input / output devices). I / O devices may include, for example, a keyboard and indicating devices, where indicating devices may include a touchpad or touchscreen, etc. I / O devices may be built into the computer device 10 or may be external devices connected to the acquisition device.

[0076] The processor 120 may also be linked via a system interconnect to a display interface suitable for connecting the computer device 10 to a display device. The display device may include a display screen that is a built-in component of the computer device 10. The display device may also include an external computer monitor, television, or projector connected to the computer device 10. Furthermore, a network interface controller (NIC) may be adapted to connect the computer device 10 to a network via a system interconnect. In some embodiments, the NIC may use any suitable interface or protocol (such as an Internet Minicomputer System Interface) to transmit data. The network may be a cellular network, a radio network, a wide area network (WAN), a local area network (LAN), or the Internet, etc. Remote devices may connect to the computer device via the network.

[0077] Therefore, those skilled in the art should recognize that although numerous exemplary embodiments of the present invention have been shown and described in detail herein, many other variations or modifications conforming to the principles of the present invention can be directly determined or derived from the disclosure of the present invention without departing from the spirit and scope of the invention. Thus, the scope of the present invention should be understood and construed as covering all such other variations or modifications.

Claims

1. A method for processing database query statements, comprising: Received query statement; Given that the query statement contains sorting and truncation operations, extract the truncation value expression corresponding to the truncation operation; Set the truncation value expression as plan-level metadata; The sorting algorithm used in the sorting operation is selected based on the truncation value expression.

2. The method for processing database query statements according to claim 1, wherein... The step of selecting the sorting algorithm for the sorting operation based on the truncation value expression includes: Check whether the truncation value expression is a constant value. If it is, estimate the cost of the TOP-N sorting algorithm based on the constant value. If not, estimate the cost of the TOP-N sorting algorithm based on the estimated value of the truncation value expression.

3. The method for processing database query statements according to claim 2, wherein... The step of estimating the cost of the TOP-N sorting algorithm based on the truncation value expression includes: If the TOP-N sorting algorithm is initially selected as the sorting algorithm for the sorting operation based on cost, Set the truncated value expression to be evaluated first during the statement execution phase; Check whether the truncation value expression can be calculated to obtain a constant value. If so, execute the step of estimating the cost of the TOP-N sorting algorithm based on the constant value. If not, select a sorting algorithm other than the TOP-N sorting algorithm.

4. The method for processing database query statements according to claim 3, wherein... The step of estimating the cost of the TOP-N sorting algorithm based on constant values ​​is followed by: Determine whether the cost of the TOP-N sorting algorithm and the available memory meet the preset conditions. If yes, select the TOP-N sorting algorithm as the sorting algorithm used in the sorting operation. If no, select a sorting algorithm other than the TOP-N sorting algorithm.

5. The method for processing database query statements according to claim 4, wherein... The step of selecting the TOP-N sorting algorithm as the sorting algorithm to be used in the sorting operation includes: Configure a bounded data structure in memory with a capacity equal to the constant value for the sorting operation; The sorting operation is performed using the bounded data structure.

6. The method for processing database query statements according to claim 1, wherein... The step of extracting the truncation value expression corresponding to the truncation operation includes: Obtain the original expression for the truncation operation; The original expression is simplified to obtain the truncation value expression.

7. The method for processing database query statements according to claim 3, wherein... Under the condition that the truncated value expression can be used to calculate a constant value, the correspondence between the truncated value expression and the constant value obtained in the stage is stored in a preset buffer. The step of checking whether the truncation value expression is a constant value includes: Check whether the truncated value expression itself is a constant value. If it is, determine that the truncated value expression is a constant value. If not, check whether there is a constant value corresponding to the truncated value expression in the preset buffer. If it is, determine that the truncated value expression is a constant value. If not, determine that the truncated value expression is not a constant value.

8. A computer device comprising a memory, a processor, and a computer-executable program stored in the memory and running on the processor, wherein the processor, when executing the computer-executable program, implements a method for processing a database query statement according to any one of claims 1 to 7.

9. A computer-readable storage medium having a computer-executable program stored thereon, wherein the computer-executable program, when executed by a processor, implements the method for processing a database query statement according to any one of claims 1 to 7.

10. A computer program product comprising a computer executable program, wherein the computer executable program, when executed by a processor, implements the method for processing database query statements according to any one of claims 1 to 7.