Method and apparatus for processing database query statement, electronic device, computer-readable storage medium, and computer program product

By encoding database query statements and matching them with a case library, and using a pre-trained language model to generate target rule sequences, the problems of wasted computing resources and degraded query performance in existing technologies are solved, achieving efficient query optimization.

WO2026237601A1PCT designated stage Publication Date: 2026-11-19TENCENT TECHNOLOGY (SHENZHEN) CO LTD
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
PCT/CN2026/089381
Authority / Receiving Office
WO · WO
Patent Type
Applications
Current Assignee / Owner
Priority Date
2025-05-13
Filing Date
2026-04-08
Publication Date
2026-11-19

AI Technical Summary

Technical Problem

Existing technologies cannot adapt to complex and ever-changing query scenarios when optimizing and rewriting database query statements, resulting in wasted computing resources and decreased query performance.

Method used

By encoding the original query statement and statistical information, an original statement vector is generated. Candidate statement vectors and rewriting rules are obtained from the case library. A pre-trained language model is used to generate a sequence of target rules to dynamically optimize the query statement.

Benefits of technology

It improves the utilization of computing resources, generates high-quality target rule sequences, reduces invalid calculations, reduces disk input/output times and memory usage, and shortens query time.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN2026089381_19112026_PF_FP_ABST
    Figure CN2026089381_19112026_PF_FP_ABST
Patent Text Reader

Abstract

Embodiments of the present application provide a method and apparatus for processing a database query statement, an electronic device, a computer-readable storage medium, and a computer program product. The method comprises: encoding an original query statement and statistical information of the original query statement to obtain an original statement vector; acquiring, from a case repository, a plurality of candidate statement vectors matching the original statement vector, and combining candidate rewriting rules associated with the respective candidate statement vectors in the case repository into a first rewriting rule set; constructing a prompt on the basis of the original query statement, the statistical information, and the first rewriting rule set; and on the basis of the prompt, calling a pre-trained language model to generate a target rule sequence, wherein the target rule sequence comprises a plurality of target rewriting rules arranged in an execution order.
Need to check novelty before this filing date? Find Prior Art

Description

Methods, apparatuses, electronic devices, computer-readable storage media, and computer program products for processing database query statements.

[0001] Cross-reference to related applications

[0002] This application is based on and claims priority to Chinese Patent Application No. 202510615362.X, filed on May 13, 2025, the entire contents of which are incorporated herein by reference. Technical Field

[0003] This application relates to cloud technology, specifically to a method, apparatus, electronic device, computer-readable storage medium, and computer program product for processing database query statements. Background Technology

[0004] Structured Query Language (SQL) allows users to express the same query intent in different ways. For example, complex logic can be implemented through nested subqueries, common expressions, or multiple joins. While these query expressions are logically equivalent, different query statements can lead to significant differences in database query efficiency. When optimizing and rewriting original query statements, related techniques are usually based on a fixed set of rewriting rules and their preset execution order. This approach is unsuitable for complex query statements in varied and complex query scenarios, or it may incorrectly apply rewriting rules that degrade query performance, resulting in the consumption of large amounts of computing resources without producing high-quality query statements. Summary of the Invention

[0005] This application provides a method, apparatus, electronic device, computer-readable storage medium, and computer program product for processing database query statements, which can dynamically generate high-quality target rule sequences adapted to the original query statements, thereby improving the utilization of computing resources.

[0006] The technical solution of this application embodiment is implemented as follows:

[0007] In a first aspect, embodiments of this application provide a method for processing database query statements, the method being executed by an electronic device, the method comprising:

[0008] The original query statement and its statistical information are encoded to obtain the original statement vector;

[0009] Multiple candidate statement vectors that match the original statement vector are obtained from the case library, and the candidate rewriting rules associated with each candidate statement vector in the case library are combined into a first rewriting rule set.

[0010] Based on the original query statement, the statistical information, and the first set of rewriting rules, a prompt word is constructed, wherein the prompt word is used to indicate that, based on the statistical information, multiple target rewriting rules for optimizing the original query statement are selected from the first set of rewriting rules in order of execution.

[0011] Based on the prompt words, a pre-trained language model is invoked to generate a sequence of target rules, wherein the sequence of target rules includes a plurality of target rewriting rules arranged in the order of execution.

[0012] Accordingly, embodiments of this application provide a database query statement processing apparatus, the apparatus comprising:

[0013] The encoding module is configured to encode the original query statement and its statistical information to obtain the original statement vector.

[0014] The combination module is configured to obtain multiple candidate statement vectors that match the original statement vector from the case library, and combine the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set.

[0015] The construction module is configured to construct prompt words based on the original query statement, the statistical information, and the first set of rewriting rules, wherein the prompt words are used to indicate that, based on the statistical information, multiple target rewriting rules for optimizing the original query statement are selected from the first set of rewriting rules in order of execution.

[0016] The generation module is configured to generate a target rule sequence by calling a pre-trained language model based on the prompt words, wherein the target rule sequence includes the plurality of target rewriting rules arranged in the order of execution.

[0017] Secondly, embodiments of this application provide a method for processing database query statements, the method being executed by an electronic device, the method comprising:

[0018] Obtain the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewrite rules;

[0019] According to the order of the target rewriting rules in the target rule sequence, the target rewriting rules are read one by one, and the original query statement is rewritten according to the currently read target rewriting rule to obtain the rewritten query statement;

[0020] In response to the fact that the original query statement and the rewritten query statement have the same semantics, it is determined that the current target rewriting rule is verified and the next target rewriting rule is read.

[0021] In response to the successful verification of the multiple target rewriting rules, the verification of the target rule sequence is confirmed. The rewritten query statement obtained by rewriting the last target rewriting rule is taken as the final target query statement obtained by rewriting the target rule sequence, and the final target query statement replaces the original query statement.

[0022] Accordingly, embodiments of this application provide a database query statement processing apparatus, the apparatus comprising:

[0023] The acquisition module is configured to acquire the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewriting rules;

[0024] The rewriting module is configured to read the target rewriting rules one by one in the target rule sequence, and rewrite the original query statement according to the currently read target rewriting rule to obtain the rewritten query statement;

[0025] The determination module is configured to, in response to the semantic similarity between the original query statement and the rewritten query statement, determine that the current target rewriting rule has been verified and continue reading the next target rewriting rule of the current target rewriting rule; in response to the verification of multiple target rewriting rules, determine that the target rule sequence has been verified and take the rewritten query statement obtained by rewriting through the last target rewriting rule as the final target query statement obtained by rewriting through the target rule sequence, and replace the original query statement with the final target query statement.

[0026] This application provides an electronic device, the electronic device comprising:

[0027] Memory is used to store executable instructions or computer programs.

[0028] The processor, when executing computer-executable instructions or computer programs stored in the memory, implements the database query statement processing method provided in the embodiments of this application.

[0029] This application provides a computer-readable storage medium storing a computer program or computer-executable instructions, which, when executed by a processor, implements the database query statement processing method provided in this application.

[0030] This application provides a computer program product, including a computer program or computer executable instructions. When the computer program or computer executable instructions are executed by a processor, they implement the database query statement processing method provided in this application.

[0031] The embodiments of this application have the following beneficial effects:

[0032] Based on the original statement vector obtained by encoding the original query statement and statistical information, multiple candidate statement vectors that match can be obtained from the case library. This can accurately identify candidate statement vectors in the case library that are similar to the original query statement in terms of logical semantics and underlying data features, thereby reducing mismatches caused by single semantic or expression differences and avoiding ineffective consumption of computing resources caused by applying inappropriate rules.

[0033] The candidate rewriting rules associated with each candidate statement vector in the case library are combined into the first rewriting rule set. In other words, the rewriting rules associated with the candidate statement vector can be automatically obtained from the case library, which realizes effective filtering of the huge global rule library of the database without manual intervention, greatly improves the efficiency of rewriting rule generation, and reduces the processor operation time when the database optimizer blindly searches the rule library.

[0034] Based on the original query statement, statistical information, and the first set of rewriting rules, prompt words are constructed to instruct the pre-trained language model to select the most suitable sequence of target rules from the first set of rewriting rules to rewrite the original query statement. With the assistance of the language model, the rewriting rules can be identified and selected more accurately, reducing errors and omissions that may occur due to manual operation. This breaks through the limitation of the fixed execution order of traditional database optimizers, enhances the flexibility and adaptability of the rewriting process, and can automatically generate a high-quality sequence of target rules that fits the original query statement. Ultimately, when the database executes the rewritten target query statement in the future, it can effectively reduce the number of disk input / output operations and memory usage, and shorten the overall query time. Attached Figure Description

[0035] Figure 1 is a schematic diagram of the architecture of the database query statement processing system 100 provided in an embodiment of this application;

[0036] Figure 2A is a schematic diagram of the structure of the electronic device 500-1 provided in an embodiment of this application;

[0037] Figure 2B is a schematic diagram of the structure of the electronic device 500-2 provided in an embodiment of this application;

[0038] Figure 3A is a first flowchart illustrating the method for processing database query statements provided in an embodiment of this application;

[0039] Figure 3B is a second flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0040] Figure 3C is a third flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0041] Figure 3D is a fourth flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0042] Figure 3E is a fifth flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0043] Figure 3F is a sixth flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0044] Figure 3G is a seventh flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0045] Figure 3H is a schematic diagram of the eighth process of the database query statement processing method provided in the embodiments of this application;

[0046] Figure 3I is a ninth flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0047] Figure 3J is a tenth flowchart illustrating the database query statement processing method provided in the embodiments of this application;

[0048] Figure 3K is a schematic diagram of the eleventh step of the database query statement processing method provided in the embodiments of this application;

[0049] Figure 3L is a schematic diagram of the twelfth step of the database query statement processing method provided in the embodiments of this application;

[0050] Figure 4 is a schematic diagram of the thirteenth step of the database query statement processing method provided in the embodiments of this application;

[0051] Figure 5 is a schematic diagram of the query statement rewriting provided in the embodiments of this application.

[0052] It should be noted that the terms "first" and "second" mentioned above are only used to distinguish between different options and do not represent the degree of superiority or inferiority of the options or their priority in the implementation process. Detailed Implementation

[0053] To make the objectives, technical solutions, and advantages of this application clearer, the application will be further described in detail below with reference to the accompanying drawings. The described embodiments should not be regarded as limitations on this application. All other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.

[0054] In the following description, references are made to “some embodiments,” which describe a subset of all possible embodiments. However, it is understood that “some embodiments” may be the same subset or different subsets of all possible embodiments and may be combined with each other without conflict.

[0055] In the following description, the terms "first / second / third" are used merely to distinguish similar objects and do not represent a specific ordering of objects. It is understood that "first / second / third" may be interchanged in a specific order or sequence where permitted, so that the embodiments of this application described herein can be implemented in an order other than that illustrated or described herein.

[0056] In this application embodiment, the terms "module" or "unit" refer to a computer program or part of a computer program that has a predetermined function and works with other related parts to achieve a predetermined goal, and can be implemented wholly or partially using software, hardware (such as processing circuitry or memory), or a combination thereof. Similarly, a processor (or multiple processors or memory) can be used to implement one or more modules or units. Furthermore, each module or unit can be part of an overall module or unit that includes the functionality of that module or unit.

[0057] Unless otherwise specified, "at least one" as used below refers to one or more cases, and "multiple" can refer to two or more cases.

[0058] Unless otherwise defined, all technical and scientific terms used in the embodiments of this application have the same meaning as commonly understood by one of ordinary skill in the art. The terminology used in the embodiments of this application is for the purpose of describing the embodiments of this application only and is not intended to limit this application.

[0059] In the implementation of this application, the collection and processing of relevant data should strictly comply with the requirements of relevant laws and regulations, obtain the informed consent or separate consent of the personal information subject, and carry out subsequent data use and processing within the scope of laws and regulations and the authorization of the personal information subject.

[0060] Before providing a further detailed description of the embodiments of this application, the nouns and terms involved in the embodiments of this application will be explained, and the nouns and terms involved in the embodiments of this application shall be interpreted as follows.

[0061] 1) Query statement: In the database field, query statement usually refers to SQL statement, which is a standard language used to interact with relational databases.

[0062] 2) Statistics: In database query optimization and performance analysis, statistics are important information used by the optimizer to evaluate query costs and select the best execution plan. Statistics may include selectivity, cardinality, number of rows in a table, number of blocks in a table, index information, histogram, index depth, and average row length of a table.

[0063] 3) Rewriting Rules: In the process of rewriting query statements, rewriting rules refer to a set of guiding principles or operational steps on how to modify the original query statement to improve query performance, enhance readability, or adapt to new database schemas. Rewriting rules can be divided into two categories: redundant expression removal rules and statistical information injection rules. For example, redundant expression removal rules may involve simplifying predicate logic and eliminating redundant calculations; statistical information injection rules may involve aggregate condition pull-ups, query reordering, etc.

[0064] 4) Target rule sequence: The language model outputs a sequence of multiple target rewriting rules arranged in the order of execution, which are used to rewrite the query statement to obtain the rewritten query statement.

[0065] 5) Performance metrics parameters: These are a series of metrics used to measure the execution efficiency of query statements. For example, performance metrics parameters may include execution time, the difference between the number of rows scanned and the number of rows returned, input / output counts, processor usage, memory usage, lock wait time, temporary tables, and sorting.

[0066] 6) Responding to: used to indicate the conditions or states on which the operation is performed. When the conditions or states on which the operation is performed are met, one or more operations may be performed in real time or with a set delay. Unless otherwise specified, there is no restriction on the order in which the multiple operations are performed.

[0067] 7) Human-computer interaction interface, an interface used to provide human-computer interaction functions. Examples include graphical user interfaces (GUIs), augmented reality (AR) interfaces, virtual reality (VR) interfaces, voice user interfaces (VUIs), interactive projection interfaces (using projection technology to display information on a flat surface), eye-tracking interfaces (interfaces controlled by detecting the user's gaze), holographic interfaces (three-dimensional holograms formed by projecting images using holographic projection technology, allowing viewing of stereoscopic images without special glasses), multimodal interfaces (interfaces combining multiple interaction methods, such as tactile, visual, and auditory interaction), and brain-machine interfaces (BMIs).

[0068] When optimizing and rewriting original query statements, related technologies are usually based on a fixed set of rewriting rules and their preset execution order. This makes them unsuitable for complex query statements in varied and complex query scenarios, or they may incorrectly apply rewriting rules that lead to a decrease in query performance, resulting in the consumption of a large amount of computing resources and the inability to rewrite high-quality query statements.

[0069] Based on the above analysis, the applicant found that the database query statement processing methods of related technologies cannot automatically generate high-quality target rule sequences adapted to the original query statements, resulting in significant consumption of computing resources. In response to the above problems, this application provides a database query statement processing method, apparatus, electronic device, computer-readable storage medium, and computer program product that can automatically generate high-quality target rule sequences adapted to the original query statements, thereby improving the utilization rate of computing resources.

[0070] The following describes exemplary applications of the electronic devices provided in the embodiments of this application. These electronic devices can be implemented as various types of terminals such as laptops, tablets, desktop computers, set-top boxes, smartphones, smart speakers, smartwatches, smart TVs, and in-vehicle terminals, or as servers. Exemplary applications of the electronic devices as terminals or servers will be described below.

[0071] Referring to Figure 1, which is a schematic diagram of the architecture of a database query statement processing system 100 provided in an embodiment of this application, in order to realize the application of supporting the processing of a database query statement, the terminal 400 (exemplarily showing the human-computer interaction interface 411) connects to the server 200 through the network 300. The network 300 can be a wide area network or a local area network, or a combination of both.

[0072] Terminal 400 is used to obtain the original query statement and its statistical information, encode the original query statement and statistical information to obtain the original statement vector; obtain multiple candidate statement vectors that match the original statement vector from the case library, combine the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set; construct prompt words based on the original query statement, statistical information and the first rewriting rule set; send the prompt words to server 200, server 200 is used to call a pre-trained language model based on the prompt words to generate a target rule sequence, and send it to terminal 400 to rewrite the original query statement by applying the target rule sequence.

[0073] In one embodiment, the terminal 400 is used to obtain the original query statement and statistical information of the original query statement, encode the original query statement and statistical information to obtain the original statement vector; obtain multiple candidate statement vectors that match the original statement vector from the case library, combine the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set; construct prompt words based on the original query statement, statistical information and the first rewriting rule set; and, based on the prompt words, call a pre-trained language model to generate a target rule sequence, so as to apply the target rule sequence to rewrite the original query statement.

[0074] The database query statement processing method provided in this application embodiment is applicable to various application scenarios. Taking the performance optimization scenario as an example, when the database query response time is too long, causing users to wait for a long time, the original query statement and statistical information can be obtained through the terminal 400. Based on the original statement vector obtained by encoding the original query statement and statistical information, a set of rewriting rules is determined from the case library. By constructing prompt words, the target rule sequence is generated by calling the pre-trained language model to rewrite the original query statement, thereby obtaining the rewritten query statement, thereby reducing the amount of data access, improving query efficiency, and shortening the response time.

[0075] Taking a database migration scenario as an example, the process involves migrating from database system A to another database system B, where the two systems support different SQL query statements. The process involves obtaining the original query statement and statistical information of the target database B via terminal 400 to generate an original query statement vector. A set of rewriting rules suitable for database system B is determined from a case library, and a target rule sequence is generated by constructing prompt words and calling a pre-trained language model. The original query statement is then rewritten using the target rule sequence to generate a query statement adapted to database system B, thus ensuring functional consistency and meeting performance requirements.

[0076] Taking data consistency and integrity assurance as an example, the application needs to ensure the consistency and integrity of data operations. The original query statement and statistical information involving data operations are obtained through terminal 400. Based on the original statement vector obtained by encoding the original query statement and statistical information, a set of rewriting rules is determined from the case library. Then, by constructing prompt words, a pre-trained language model is called to generate a sequence of target rules to rewrite the original query statement, resulting in the rewritten query statement, thereby ensuring data consistency.

[0077] Taking a load balancing scenario as an example, a database system may have multiple nodes, and query statements may be executed on different nodes. To ensure load balancing across nodes, the original query statements and their statistical information, involving data distribution and load conditions, can be obtained through a terminal 400. The original query statements and statistical information are encoded to generate original statement vectors that reflect query characteristics and database load conditions. A set of rewriting rules suitable for the current database load conditions is determined from a case library, corresponding prompt words are constructed, a pre-trained language model is invoked, and a target rule sequence is generated to rewrite the original query statements, producing new query statements. This achieves load balancing of the database system, improving overall performance and user satisfaction.

[0078] In some embodiments, server 200 can be a standalone physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, content delivery networks (CDNs), and big data and artificial intelligence platforms. Terminals and servers can be connected directly or indirectly via wired or wireless communication, which is not limited in this embodiment.

[0079] The electronic device implementing the database query statement processing method provided in this application embodiment can be the terminal 400 or server 200 in FIG1. ​​Referring to FIG2A, FIG2A is a schematic diagram of the structure of the electronic device 500-1 provided in this application embodiment. The electronic device 500-1 shown in FIG2A includes: at least one processor 510-1, at least one network interface 520-1, a user interface 530-1, and a memory 550-1. The various components in the electronic device 500-1 are coupled together through a bus system 540-1. It is understood that the bus system 540-1 is used to realize the connection and communication between these components. In addition to the data bus, the bus system 540-1 also includes a power bus, a control bus, and a status signal bus. However, for the sake of clarity, all buses are labeled as bus system 540-1 in FIG2A.

[0080] The processor 510-1 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor can be a microprocessor or any conventional processor, etc.

[0081] User interface 530-1 includes one or more output devices 531-1 that enable the presentation of media content, including at least one of one or more speakers and one or more visual displays. User interface 530-1 also includes one or more input devices 532-1, including user interface components that facilitate user input, such as a keyboard, mouse, microphone, touch screen display, camera, other input buttons and controls.

[0082] The memory 550-1 can be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state storage, hard disk drives, optical disk drives, etc. The memory 550-1 may optionally include one or more storage devices physically located away from the processor 510-1.

[0083] The memory 550-1 may include volatile memory or non-volatile memory, or both. The non-volatile memory may be read-only memory (ROM), and the volatile memory may be random access memory (RAM). The memory 550-1 described in this application embodiment is intended to include any suitable type of memory.

[0084] In some embodiments, memory 550-1 is capable of storing data to support various operations, examples of which include programs, modules, and data structures or subsets or supersets thereof, as illustrated below.

[0085] Operating system 551-1 includes system programs for handling various basic system services and performing hardware-related tasks, such as the framework layer, core library layer, and driver layer, for implementing various basic business functions and handling hardware-based tasks;

[0086] The network communication module 552-1 is used to reach other electronic devices via one or more (wired or wireless) network interfaces 520-1, such as Bluetooth, WiFi, and Universal Serial Bus (USB).

[0087] Presentation module 553-1 is used to enable the presentation of information (e.g., a user interface for operating peripheral devices and displaying content and information) via one or more output devices 531-1 (e.g., a display screen, a speaker, etc.) associated with user interface 530-1;

[0088] The input processing module 554-1 is used to detect and translate one or more user inputs or interactions from one or more input devices 532-1.

[0089] In some embodiments, the apparatus provided in this application can be implemented in software. Figure 2A shows a processing apparatus 555-1 for query statements of a database stored in memory 550-1. This apparatus can be software in the form of programs and plug-ins, including the following software modules: encoding module 5551-1, combination module 5552-1, construction module 5553-1, and generation module 5554-1. These modules are logically related and can therefore be arbitrarily combined or further split according to the functions they implement. The functions of each module will be described below.

[0090] This application embodiment also provides a method for processing database query statements to verify a target rule sequence of a given original query statement. Here, the electronic device implementing the database query statement processing method can be the terminal 400 or server 200 in FIG1. ​​Referring to FIG2B, FIG2B is a schematic diagram of the structure of the electronic device 500-2 provided in this application embodiment. The electronic device 500-2 shown in FIG2B includes: at least one processor 510-2, at least one network interface 520-2, a user interface 530-2, and a memory 550-2. The various components in the electronic device 500-2 are coupled together through a bus system 540-2. It is understood that the bus system 540-2 is used to realize the connection and communication between these components. In addition to a data bus, the bus system 540-2 also includes a power bus, a control bus, and a status signal bus. However, for clarity, all buses are labeled as bus system 540-2 in FIG2B.

[0091] The processor 510-2 can be an integrated circuit chip with signal processing capabilities, such as a general-purpose processor, a digital signal processor (DSP), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. Among them, the general-purpose processor can be a microprocessor or any conventional processor, etc.

[0092] User interface 530-2 includes one or more output devices 531-2 that enable the presentation of media content, including at least one of one or more speakers and one or more visual displays. User interface 530-2 also includes one or more input devices 532-2, including user interface components that facilitate user input, such as a keyboard, mouse, microphone, touch screen display, camera, other input buttons and controls.

[0093] The memory 550-2 may be removable, non-removable, or a combination thereof. Exemplary hardware devices include solid-state storage, hard disk drives, optical disk drives, etc. The memory 550-2 may optionally include one or more storage devices physically located away from the processor 510-2.

[0094] The memory 550-2 may include volatile memory or non-volatile memory, or both. The non-volatile memory may be read-only memory (ROM), and the volatile memory may be random access memory (RAM). The memory 550-2 described in this application embodiment is intended to include any suitable type of memory.

[0095] In some embodiments, memory 550-2 is capable of storing data to support various operations, examples of which include programs, modules, and data structures or subsets or supersets thereof, as illustrated below.

[0096] In some embodiments, the database query statement processing apparatus provided in this application can be implemented in software. Figure 2B shows a database query statement processing apparatus 555-2 stored in memory 520-2, which can be software in the form of programs and plug-ins, including the following software modules: an acquisition module 5551-2, a rewriting module 5552-2, and a determination module 5553-2. These modules are logically related and can therefore be arbitrarily combined or further split according to the functions they implement. The functions of each module will be described below.

[0097] In some embodiments, the terminal or server can implement the database query statement processing method provided in this application embodiment by running various computer-executable instructions or computer programs. For example, computer-executable instructions can be microprogram-level commands, machine instructions, or software instructions. Computer programs can be native programs or software modules in an operating system; they can be native applications (APPs), i.e., programs that need to be installed in the operating system to run; or they can be applets that can be embedded in any APP, i.e., programs that only need to be downloaded to a browser environment to run. In summary, the aforementioned computer-executable instructions can be any form of instruction, and the aforementioned computer programs can be any form of application, module, or plugin.

[0098] The method for processing database query statements provided in this application will be described in conjunction with exemplary applications and implementations of the terminals provided in the embodiments of this application.

[0099] The following describes the database query statement processing method provided in the embodiments of this application. As mentioned above, the electronic device implementing the database query statement processing method of the embodiments of this application can be a terminal or a server, or a combination of both. Therefore, the executing entity of each step will not be described again below.

[0100] Referring to Figure 3A, which is a first flowchart of the database query statement processing method provided in the embodiment of this application, the steps shown in Figure 3A will be described in conjunction with the terminal as the execution subject.

[0101] In step 101, the original query statement and its statistical information are encoded to obtain the original statement vector.

[0102] Here, statistical information refers to the important basis used by the optimizer to evaluate query costs and select the best execution plan in database query optimization and performance analysis. Statistical information may include selectivity, cardinality, number of rows in a table, number of blocks in a table, index information, histogram, index depth, and average row length of a table.

[0103] In some embodiments, referring to FIG3B, FIG3B is a second flowchart of the database query statement processing method provided in the embodiments of this application. Step 101 in FIG3A can be implemented by steps 1011 to 1013 in FIG3B, which will be described in detail below.

[0104] In step 1011, the original query statement is divided into multiple words, each word is encoded to obtain a first sub-vector, and the first sub-vectors corresponding to multiple words are merged to obtain a first vector.

[0105] Here, the original query statement refers to the initial structured query statement used as input for encoding processing. A word refers to the basic language unit obtained by segmenting the original query statement according to its syntactic structure. The first sub-vector is the numerical vector representation obtained after encoding each word. The first vector is the comprehensive vector representing the overall semantics of the original query statement obtained by fusing the first sub-vectors corresponding to multiple words.

[0106] In some embodiments, the original query statement can be segmented into multiple words.

[0107] In some embodiments, the original query statement can be segmented according to spaces and preset structured query language keywords to generate a word sequence containing multiple words. A fixed-dimensional floating-point vector corresponding to each word can be obtained as the first sub-vector by consulting a pre-trained word embedding mapping table. Finally, the average or maximum value of each element in the vector dimension of the multiple first sub-vectors is calculated, and the result is used as the first vector, ensuring that the dimension of the first vector is consistent with the dimension of each first sub-vector.

[0108] As an example, the original query is "SELECT name, age FROM users WHERE age>30 AND country='A'". The original query can be divided into multiple words: "SELECT", "name", "age", "FROM", "users", "WHERE", "age", ">", "30", "AND", "country", "=", and "'A'".

[0109] In some embodiments, the above-mentioned fusion of the first sub-vectors corresponding to multiple words to obtain the first vector can be achieved by concatenating the first sub-vectors corresponding to multiple words; or by obtaining a preset weight for each word and then weighting and summing the first sub-vectors corresponding to multiple words according to the preset weight to obtain the first vector.

[0110] In step 1012, at least one indicator parameter included in the statistical information of the original query statement is encoded to obtain a second sub-vector for each indicator parameter. The second sub-vectors corresponding to multiple indicator parameters are then merged to obtain a second vector.

[0111] Here, statistical information refers to the set of parameters describing the underlying data distribution and physical execution characteristics associated with the execution of the original query statement in the database. A metric parameter is a single quantifiable, specific data item included in the statistical information. Metric parameters include at least one of the following: selectivity, cardinality, number of rows in the table, number of blocks in the table, index information (such as index type and index columns), histogram, index depth, and average row length of the table. The second sub-vector refers to the numerical vector representation obtained after encoding each metric parameter. The second vector is a comprehensive vector representing the overall characteristics of the statistical information obtained by fusing multiple second sub-vectors.

[0112] In some embodiments, the above-mentioned fusion of the second sub-vectors corresponding to multiple indicator parameters to obtain the second vector can be achieved by concatenating the second sub-vectors corresponding to multiple indicator parameters; or by obtaining a preset weight for each indicator parameter, and then weighting and summing the second sub-vectors corresponding to multiple indicator parameters according to the preset weight to obtain the second vector.

[0113] In step 1013, the first vector and the second vector are merged to obtain the original statement vector.

[0114] In some embodiments, referring to FIG3C, FIG3C is a third flowchart of the database query statement processing method provided in the embodiments of this application. Step 1013 in FIG3B can be implemented by step 10131 or step 10132 in FIG3C, as described in detail below.

[0115] In step 10131, the first vector and the second vector are concatenated to obtain the original statement vector.

[0116] As an example, if the first vector is c and the second vector is d, then concatenating the first vector and the second vector will give us the original statement vector, which can be represented as [c; d].

[0117] In step 10132, based on the first weight preset for the first vector and the second weight preset for the second vector, the first vector and the second vector are weighted and summed to obtain the original statement vector.

[0118] As an example, the first weight of the first vector is set to 0.6, and the second weight of the second vector can be set to 0.4. If the first vector is c and the second vector is d, then the first vector and the second vector are weighted and summed according to the first weight and the second weight, and the original statement vector can be represented as 0.6*c + 0.4*d.

[0119] This application's embodiments divide the original query statement into multiple words, encode each word to obtain a first sub-vector, and merge multiple first sub-vectors into a first vector. This first vector transforms the logical semantics of the query statement into machine-quantifiable numerical features. Next, the indicator parameters in the statistical information of the original query statement are encoded to obtain a second sub-vector, and multiple second sub-vectors are merged into a second vector. This second vector also transforms the underlying physical execution cost of the query statement into numerical features. Finally, the first and second vectors are merged to obtain the original statement vector. The merged original statement vector contains the semantic information of the query statement as well as statistical information related to query performance, making the subsequent matching of candidate statement vectors of sample query statements from the case library more accurate. This solves the technical problem in related technologies where the fragmented processing of the semantic and physical execution features of the query statement leads to incomplete feature representation.

[0120] Please refer to Figure 3A for a continuation of step 101 above.

[0121] In step 102, multiple candidate statement vectors that match the original statement vector are obtained from the case library, and the candidate rewriting rules associated with each candidate statement vector in the case library are combined into a first rewriting rule set.

[0122] In some embodiments, referring to FIG3D, FIG3D is a fourth flowchart of the database query statement processing method provided in the embodiments of this application. The step 102 in FIG3A, "obtaining multiple candidate statement vectors that match the original statement vector from the case library", can be implemented through steps 1021 to 1022 in FIG3D, which will be described in detail below.

[0123] In step 1021, the similarity between each candidate statement vector in the case library and the original statement vector is determined. The case library includes multiple candidate statement vectors and candidate rewriting rules associated with each candidate statement vector.

[0124] Here, the original query vector refers to the numerical vector feature obtained by encoding the original query statement and its statistical information. The case library refers to a database storing multiple pre-processed candidate query vectors and the associated rule data. A candidate query vector is a single numerical vector in the case library used to characterize the features of historical sample queries. Similarity is a quantified value measuring the closeness between each candidate query vector and the original query vector in a multi-dimensional feature space. Candidate rewriting rules are the rule parameters in the case library that have a mapping relationship with each candidate query vector and are used to optimize the query statement.

[0125] In some embodiments, the similarity between each candidate statement vector in the case library and the original statement vector can be determined using a similarity algorithm. For example, the similarity algorithm can be cosine similarity, Euclidean distance, Manhattan distance, Chebyshev distance, edit distance, etc.

[0126] Taking cosine similarity as an example, if the candidate statement vector is m and the original statement vector is n, we determine the vector product of the candidate statement vector and the original statement vector, the product of the length of the candidate statement vector and the length product of the original statement vector, and the ratio of the vector product to the length product, which is used as the similarity. For example, the vector product of the candidate statement vector and the original statement vector is m*n. The length of the candidate statement vector is |m|, the length of the original statement vector is |n|, and the length product is |m|*|n|, then the similarity is (m*n) / (|m|*|n|).

[0127] In step 1022, each candidate statement vector in the case library is sorted in descending order of similarity, and the multiple candidate statement vectors starting from the first in the sorting result are taken as multiple candidate statement vectors that match the original statement vector.

[0128] In some embodiments, a predetermined number of candidate statement vectors, starting from the first position in the sorting result, can be used as multiple candidate statement vectors that match the original statement vector.

[0129] As an example, the similarity scores of the three candidate statement vectors in the case library (candidate statement vector 1, candidate statement vector 2, and candidate statement vector 3) with the original statement vector are 0.6, 0.8, and 0.5, respectively. Therefore, the sorting result by similarity in descending order is candidate statement vector 2, candidate statement vector 1, and candidate statement vector 3. If the preset number is 2, then candidate statement vector 2 and candidate statement vector 1 will be selected as candidate statement vectors to match the original statement vector.

[0130] This application's embodiments automatically determine candidate statement vectors that match the original query statement's original statement vector from a case library based on the similarity between the original statement vector and candidate statement vectors. Similarity calculation more accurately identifies candidate statements semantically similar to the original query statement. Finally, subsequent processing is performed based on the matched candidate statement vectors. Numerical similarity accurately identifies the semantic similarity between the original statement vector and candidate statement vectors, avoiding mismatches caused by conventional text comparison, thereby improving matching accuracy. The automated matching process reduces manual intervention and significantly improves the efficiency of matching massive amounts of query statements, especially when processing a large number of queries. This solves the technical problem in related technologies where semantic deviation leads to low matching accuracy when processing a large number of query statements.

[0131] In some embodiments, referring to FIG3E, FIG3E is a fifth flowchart of the database query statement processing method provided in the embodiments of this application. The step 102 in FIG3A, "combining the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set", can be implemented by steps 1023 to 1024 in FIG3E, which will be described in detail below.

[0132] In step 1023, the candidate rewriting rules associated with each candidate statement vector in the case library are processed by taking the union of the sets to obtain the filtered candidate rewriting rules.

[0133] Here, the union operation refers to the mechanism of merging elements from multiple sets together and removing duplicate elements. The filtered candidate rewriting rule refers to the only valid rule data retained after removing redundant duplicates through the union operation.

[0134] In some embodiments, when multiple candidate statement vectors are associated with the same candidate rewriting rule in the case library, the duplicate candidate rewriting rules can be processed by taking the union of the duplicate candidate rewriting rules, that is, the duplicate candidate rewriting rules are recorded only once.

[0135] As an example, the candidate rewriting rules associated with candidate statement vector 1 are rule 1, rule 3 and rule 2, and the candidate rewriting rules associated with candidate statement vector 2 are rule 2, rule 3 and rule 4. Then, by taking the union of the candidate rewriting rules associated with candidate statement vector 1 and candidate statement vector 2 respectively, the filtered candidate rewriting rules are rule 1, rule 2, rule 3 and rule 4.

[0136] In step 1024, the filtered candidate rewriting rules are combined into a first rewriting rule set.

[0137] Here, the first rewrite rule set refers to the data set constructed from the combination of filtered candidate rewrite rules, which is used to provide a rule benchmark for optimizing the original query statement.

[0138] As an example, rules 1, 2, 3 and 4 in the above example can be combined into the first set of rewrite rules.

[0139] This application embodiment obtains filtered candidate rewriting rules by performing a union operation on the candidate rewriting rules associated with multiple candidate statement vectors. These filtered candidate rewriting rules are then combined to form a first rewriting rule set. By performing a union operation on multiple candidate rewriting rules, the computational redundancy of simply stacking candidate rewriting rules is eliminated. This process plays a refining role in rule merging, ensuring that the resulting rewriting rule set covers more optimization strategies, thereby improving the diversity and comprehensiveness of the rewriting rules. It also removes duplicate rewriting rules, retaining only valid ones, effectively preventing invalid computations during processing by electronic devices. For example, after removing four duplicate candidate rewriting rules from ten candidate rewriting rules, six filtered candidate rewriting rules are retained, avoiding wasted computational resources and improving the quality of the rewriting rules. Simultaneously, it optimizes the allocation of database resources, such as reducing input / output operations and processor resource usage, thus solving the technical problems of search space redundancy and incomplete strategy coverage that may result from merging candidate rewriting rules in related technologies.

[0140] In some embodiments, referring to FIG3F, FIG3F is a sixth flowchart of the database query statement processing method provided in the embodiments of this application. The above-mentioned case library can be constructed through steps 201 to 206 of FIG3F, which are described in detail below.

[0141] In step 201, the sample query statement is obtained.

[0142] Here, the sample query statement refers to the pre-obtained structured query statement used as the basis for performance analysis and rewrite verification.

[0143] In some embodiments, step 201 above can be obtained by: acquiring historical query statements from the database that meet the filtering conditions, and using the historical query statements that meet the filtering conditions as sample query statements, wherein the filtering conditions include at least one of the following: the performance index parameters of the historical query statements are statistically analyzed to include at least one of the following: the execution time of the historical query statement is greater than a first time threshold, the difference between the number of rows scanned and the number of rows returned in the historical query statement is greater than a row number threshold, the number of inputs / outputs of the historical query statement is greater than a number threshold, the processor usage of the historical query statement is greater than a first resource threshold, the memory usage of the historical query statement is greater than a second resource threshold, and the lock wait time of the historical query statement is greater than a second time threshold.

[0144] Here, historical query statements refer to structured query statements that have been executed in the database within a preset time period and whose physical execution records have been retained. Filtering criteria refer to pre-defined boundary standards for filtering statements from a large number of historical queries that present performance bottlenecks. Frequency thresholds refer to pre-defined limits on the number of read / write operations to determine their frequency. The first resource threshold refers to pre-defined limits on computing resources to measure whether processor usage is excessive. The second resource threshold refers to pre-defined limits on storage space to measure whether memory usage exceeds limits. The second time threshold refers to pre-defined limits on time to determine whether blocking latency is excessive.

[0145] As an example, if the execution time of a historical query statement is 20 seconds, with a first time threshold of 10 seconds; the difference between the number of rows scanned and the number of rows returned in a historical query statement is 8, with a row count threshold of 5; the number of inputs / outputs in a historical query statement is 80, with a count threshold of 50; the processor usage of a historical query statement is 100 MB, with a first resource threshold of 50 MB; the memory usage of a historical query statement is 80 MB, with a second resource threshold of 50 MB; and the lock wait time for a historical query statement is 10 seconds, with a second time threshold of 8 seconds, then the corresponding historical query statement can be used as a sample query statement.

[0146] Here, performance metrics refer to a series of indicators used to measure the execution efficiency of query statements. For example, performance metrics can be execution time, the difference between the number of rows scanned and the number of rows returned, the number of input / output operations, processor usage, memory usage, lock wait time, temporary tables, and sorting.

[0147] Execution time refers to the total time consumed by the database from starting to process historical query statements to outputting query results. The first time threshold is a pre-set time limit to measure whether the execution time exceeds the normal range. Scanned rows refer to the total number of underlying data rows actually read by the database when executing historical query statements. Returned rows refer to the total number of data rows finally output by the database to the caller after executing historical query statements. Row difference is the difference between the scanned rows and the returned rows. Row threshold is a pre-set threshold to determine whether the row difference is too large. Input / output count refers to the total number of read / write operations performed by the database on the underlying disk or storage device during the execution of historical query statements. Processor usage refers to the proportion of computing resources consumed by the central processing unit when processing historical query statements. Memory usage refers to the capacity of internal buffer storage space allocated and consumed by the database when executing historical query statements. Lock wait time refers to the blocking delay caused by waiting for other concurrent transactions to release data locks during the execution of historical query statements. Temporary tables and sorting refer to the operations performed by the database during the execution of query statements, such as dynamically allocating storage space to create temporary data tables to cache intermediate calculation results, and rearranging the positions of read data rows according to specific fields.

[0148] In some embodiments, various performance metrics parameters of historical query statements can be read from the database execution log. A pre-configured first time threshold, row count threshold, count threshold, first resource threshold, second resource threshold, and second time threshold are extracted. The execution time of the historical query statement is compared with the first time threshold. The row count difference obtained by subtracting the number of rows scanned and returned by the historical query statement is calculated and compared with the row count threshold. The number of input / output operations of the historical query statement is compared with the count threshold. The processor usage of the historical query statement is compared with the first resource threshold. The memory usage of the historical query statement is compared with the second resource threshold. The lock wait time of the historical query statement is compared with the second time threshold. If any of the comparison results, such as the execution time of the historical query statement being greater than the first time threshold or the row count difference being greater than the row count threshold, is true, the performance metrics parameters of the historical query statement are determined to meet the filtering conditions.

[0149] This application uses historical query statements that meet preset performance index parameters as sample query statements. By filtering out query statements with long execution times, large differences between the number of rows scanned and returned, numerous input / output operations, and high processor or memory usage, the execution characteristics of these slow queries can be analyzed. This allows for targeted selection of rewriting rules to optimize these slow queries, providing a basis for query optimization. Through this hardware index filtering mechanism, normal statements with low consumption of underlying computing resources are excluded, ensuring that the extracted sample query statements have high analytical and optimization value. This avoids interference from invalid data in subsequent processing and significantly improves the hardware execution efficiency for subsequent targeted mining of effective rewriting rules. This solves the technical problems in related technologies where blindly extracting historical data leads to low sample quality and a lack of targeted optimization in subsequent processes.

[0150] In step 202, multiple subsets of rewriting rules are extracted from the second set of rewriting rules.

[0151] Here, the second set of rewriting rules refers to the full set containing multiple global candidate rewriting rules. A subset of rewriting rules refers to a set of rewriting rules extracted from the second set of rewriting rules.

[0152] In some embodiments, multiple subsets of rewriting rules may be randomly extracted from the second set of rewriting rules.

[0153] As an example, the second set of rewriting rules includes four subsets of rewriting rules, namely rewriting rule subset 1, rewriting rule subset 2, rewriting rule subset 3 and rewriting rule subset 4. Then, rewriting rule subset 1 and rewriting rule subset 3 can be extracted.

[0154] In step 203, a subset of effective rewrite rules that improves the performance metrics parameters of the sample query statement is determined from multiple subsets of rewrite rules.

[0155] Here, performance metrics refer to the metrics used to measure the execution efficiency of the sample query statement in the database. The effective rewrite rule subset refers to the subset of rewrite rules that can improve the performance metrics of the sample query statement.

[0156] After extracting multiple rewrite rule subsets from the second rewrite rule set, the effective rewrite rule subsets that improve the performance metrics of the sample query statement can be determined by comparing the performance metrics parameters of each rewrite rule subset during the execution process. This will be explained in detail below.

[0157] In some embodiments, referring to FIG3G, FIG3G is a seventh flowchart of the database query statement processing method provided in the embodiments of this application. For each subset of rewrite rules, step 203 in FIG3F can be implemented by steps 2031 to 2033 in FIG3G, as described in detail below.

[0158] In step 2031, the sample query statement is rewritten based on the subset of rewriting rules to obtain the first rewritten sample query statement.

[0159] In some embodiments, the sample query statement can be rewritten according to the execution order of the sample rewriting rules in the rewriting rule subset to obtain the first rewritten sample query statement.

[0160] As an example, there are 3 sample rewriting rules in the rewriting rule subset, namely rule 1, rule 2 and rule 3. First, rule 1 is used to rewrite the sample query statement A to obtain query statement B. Then, rule 2 is used to rewrite query statement B to obtain query statement C. Finally, rule 3 is used to rewrite query statement C to obtain the first rewritten sample query statement.

[0161] In step 2032, the first performance index parameter of the database when executing the sample query statement and the second performance index parameter of the database when executing the first rewritten sample query statement are obtained.

[0162] Here, the first rewritten sample query statement refers to the query statement output after rewriting the sample query statement based on a subset of rewriting rules. The first performance metric parameter refers to the hardware performance metric parameter measured by the database when executing the unrewritten sample query statement. The second performance metric parameter refers to the hardware performance metric parameter measured by the database when executing the first rewritten sample query statement.

[0163] In some embodiments, firstly, a sample query statement is rewritten based on a subset of rewriting rules to obtain a first rewritten sample query statement. Secondly, the sample query statement is sent to the database and executed by the database. When the database completes the execution of the sample query statement, a performance testing tool is invoked to obtain a first performance indicator parameter during the execution of the sample query statement. Then, a second performance indicator parameter is obtained when the database executes the first rewritten sample query statement. Finally, in response to the second performance indicator parameter being superior to the first performance indicator parameter, the subset of rewriting rules is considered the valid subset of rewriting rules.

[0164] In some embodiments, the "obtaining the first performance index parameters of the database when executing the sample query statement" in step 2032 above can be achieved in the following way: sending the sample query statement to the database and executing the sample query statement through the database; when the database finishes executing the sample query statement, calling a performance testing tool to obtain the first performance index parameters during the execution of the sample query statement by the database, wherein the first performance index parameters include at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table and sorting.

[0165] In some embodiments, the "obtaining the second performance index parameters of the database when executing the first rewritten sample query statement" in step 2032 above can be implemented in the following way: sending the first rewritten sample query statement to the database and executing the first rewritten sample query statement through the database; when the database finishes executing the first rewritten sample query statement, calling a performance testing tool to obtain the second performance index parameters during the execution of the first rewritten sample query statement by the database, wherein the second performance index parameters include at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table and sorting.

[0166] Here, performance testing tools refer to software modules or independent programs used to collect and quantify various hardware performance metrics of the database during query execution. These tools can be built into the database or independent of it. Execution time refers to the total time spent from the start to the end of a query, typically including processor processing time and waiting time. The difference between the number of rows scanned and the number of rows returned refers to the difference between the number of data rows actually scanned by the database during query execution and the number of rows in the result set actually returned to the user after the query is executed. Input / output operations refer to the number of input / output operations performed on disk or storage devices during the query process, including data read and write operations. Processor usage refers to the proportion of CPU resources consumed during query execution, reflecting the degree of computational resource utilization. Memory usage refers to the amount of memory resources consumed during query execution, including cache and temporary storage space used during query processing. Lock wait time refers to the delay caused by the query waiting for database locks to be released. Temporary tables and sorting refer to the number of temporary tables created and used during the query process; temporary tables may be used for sorting, joining, aggregation, and other operations.

[0167] In step 2033, in response to the second performance metric parameter being superior to the first performance metric parameter, the subset of rewriting rules is taken as the valid subset of rewriting rules.

[0168] As an example, firstly, the sample query statement is rewritten based on a subset of rewriting rules containing two sample rewriting rules, resulting in the first rewritten sample query statement. Secondly, the first performance metric parameter of the database when executing the sample query statement is obtained, for example, an execution time of 15 seconds and memory usage of 512 megabytes. Then, the second performance metric parameter of the database when executing the first rewritten sample query statement is obtained, for example, an execution time of 8 seconds and memory usage of 256 megabytes. Finally, in response to the fact that the 8-second execution time of the second performance metric parameter is better than the 15-second execution time of the first performance metric parameter, the subset of rewriting rules is considered the valid subset of rewriting rules.

[0169] This application first rewrites a sample query statement based on a subset of rewriting rules to obtain a first rewritten sample query statement. Next, it obtains a first performance indicator parameter of the database when executing the sample query statement and a second performance indicator parameter of the database when executing the first rewritten sample query statement. Finally, in response to the second performance indicator parameter being superior to the first performance indicator parameter, the subset of rewriting rules is selected as a valid subset of rewriting rules. By executing the query statements before and after rewriting in a real database environment and directly comparing the quantified first and second performance indicator parameters, a feedback verification loop based on empirical data is constructed. This feedback verification loop ensures that only the subset of rewriting rules that can genuinely reduce execution time, processor usage, or memory consumption is selected and retained, avoiding the incorrect adoption of theoretically effective but poorly performing rules, significantly improving the accuracy and reliability of subsequent selection of valid rewriting rules. This solves the technical problem in related technologies where blindly applying rewriting rules may lead to a decrease in query performance instead of an increase.

[0170] Please refer to Figure 3F for further explanation following step 203 above.

[0171] In step 204, sample rewriting rules that improve the performance metrics of the sample query statement are determined from the subset of valid rewriting rules, and the determined sample rewriting rules are used as candidate rewriting rules for the sample query statement.

[0172] Here, a sample rewriting rule refers to a single rewriting rule contained within the subset of effective rewriting rules. A candidate rewriting rule refers to a sample rewriting rule that has been verified and determined to improve the performance metrics of the sample query statement and has been retained.

[0173] In some embodiments, referring to FIG3H, FIG3H is an eighth flowchart of the database query statement processing method provided in the embodiments of this application. Step 204 in FIG3F can be implemented by steps 2041 to 2044 in FIG3H, which will be described in detail below.

[0174] In step 2041, N replica subsets of the valid rewrite rule subset are created, where N is an integer greater than or equal to 2.

[0175] Here, the effective rewriting rule subset can be directly copied to obtain N copy subsets of the effective rewriting rule subset, that is, the number of sample rewriting rules included in the effective rewriting rule subset is N.

[0176] As an example, if the effective rewrite rule subset includes three sample rewrite rules, namely rule 1, rule 2 and rule 3, then three replica subsets of the effective rewrite rule subset are created, each replica subset including rule 1-replica, rule 2-replica and rule 3-replica.

[0177] In some embodiments, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than N, step 2041 above can be implemented as follows: arbitrarily select N sample rewriting rules from the effective rewriting rule subset, and create N replica subsets of the effective rewriting rule subset based on the selected N sample rewriting rules, wherein each replica subset includes N sample rewriting rules.

[0178] Here, if N is 3, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than N, N sample rewriting rules can be arbitrarily selected from the effective rewriting rule subset to create N replica subsets of the effective rewriting rule subset, where each replica subset includes N sample rewriting rules.

[0179] As an example, the effective rewrite rule subset includes four sample rewrite rules, namely rule 1, rule 2, rule 3 and rule 4. Then, any three sample rewrite rules can be selected from the effective rewrite rule subset, such as rule 1, rule 2 and rule 3, to create three replica subsets of the effective rewrite rule subset. Each replica subset includes rule 1-replica, rule 2-replica and rule 3-replica.

[0180] In some embodiments, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than or equal to 2N, step 2041 above can be implemented in the following way: sorting the sample rewriting rules in the effective rewriting rule subset, wherein the similarity between any two adjacent sample rewriting rules is greater than the similarity between any two non-adjacent sample rewriting rules; selecting N sample rewriting rules from the sorting results by interval sampling, and creating N replica subsets of the effective rewriting rule subset based on the selected N sample rewriting rules, wherein each replica subset includes N sample rewriting rules.

[0181] Here, if N is 3, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than or equal to 2N, N sample rewriting rules can be selected from the sorted sample rewriting rules by interval sampling to create N replica subsets of the effective rewriting rule subset, where each replica subset includes N sample rewriting rules.

[0182] As an example, the effective rewriting rule subset includes 6 sample rewriting rules, namely rule 1, rule 2, rule 3, rule 4, rule 5 and rule 6. If the sorting result according to similarity is rule 3, rule 2, rule 1, rule 4, rule 6 and rule 5, then any 3 sample rewriting rules can be selected from the sorting result, such as rule 3, rule 1 and rule 6, to create 3 replica subsets of the effective rewriting rule subset. Each replica subset includes rule 3-replica, rule 1-replica and rule 6-replica.

[0183] In this context, a duplicate subset refers to a temporary set of copies of the effective rewriting rules created for iterative rule filtering and verification. Sorting refers to the process of rearranging the sample rewriting rules in the effective rewriting rule subset according to specific rules, either from high to low or from low to high similarity scores. Similarity is a quantitative value used to measure the degree of functional or structural similarity between any two sample rewriting rules. Interval sampling refers to a sampling method that selects elements non-contiguously at fixed positional intervals from the sorted sequence of sample rewriting rules.

[0184] In some embodiments, firstly, it is determined whether the number of sample rewriting rules included in the effective rewriting rule subset is greater than N. Secondly, if the number of sample rewriting rules included in the effective rewriting rule subset is greater than N, N sample rewriting rules are arbitrarily selected from the effective rewriting rule subset, and N replica subsets of the effective rewriting rule subset are created based on the selected N sample rewriting rules. Alternatively, if the number of sample rewriting rules included in the effective rewriting rule subset is greater than or equal to 2N, the sample rewriting rules in the effective rewriting rule subset are sorted. Then, N sample rewriting rules are selected from the sorted results using an interval sampling method. Finally, N replica subsets of the effective rewriting rule subset are created based on the selected N sample rewriting rules.

[0185] In some embodiments, for the process of sorting the sample rewriting rules in the effective rewriting rule subset, the text description of each sample rewriting rule is converted into a word vector of fixed dimension. The cosine distance or Euclidean distance between the word vectors corresponding to any two sample rewriting rules is calculated, and the reciprocal of the calculated distance value is taken as the similarity between the two sample rewriting rules. All sample rewriting rules are sorted in descending order based on the calculated similarity value. For the process of selecting N sample rewriting rules from the sorted results using interval sampling, a sampling interval value is calculated. The sampling interval value is equal to the number of sample rewriting rules included in the effective rewriting rule subset divided by N and rounded down. Starting from the sample rewriting rule with index position zero in the sorted results, sample rewriting rules with index positions that are integer multiples of the sampling interval value are selected sequentially until N sample rewriting rules are selected, ensuring that the selected sample rewriting rules are evenly distributed in the sorted results.

[0186] As an example, firstly, when N equals five, the effective subset of rewriting rules includes eight sample rewriting rules, satisfying the condition that eight is greater than N+5. Five sample rewriting rules are randomly selected from these eight, and five replica subsets of the effective subset of rewriting rules are created based on these five selected rules. Alternatively, when N equals five, the effective subset of rewriting rules includes twelve sample rewriting rules, satisfying the condition that twelve is greater than or equal to 2N+. The twelve sample rewriting rules are sorted. Then, five sample rewriting rules are selected from the sorted results using an interval of two. Finally, five replica subsets of the effective subset of rewriting rules are created based on these five selected rules.

[0187] This application addresses three comparison relationships between the number of sample rewriting rules included in the effective rewriting rule subset and N (i.e., the number of sample rewriting rules is N, the number of sample rewriting rules is greater than N, or the number of sample rewriting rules is greater than or equal to 2N). For each of these relationships, methods for creating N replica subsets of the effective rewriting rule subset are defined. During rule subset segmentation and sampling, the above schemes ensure that subset selection is random or based on a certain strategy to avoid selection bias. First, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than N, N sample rewriting rules are arbitrarily selected to create N replica subsets. This method effectively controls the computational scale by setting a selection upper limit, avoiding unnecessary consumption of computing resources by electronic devices. Second, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than or equal to 2N, the sample rewriting rules are sorted and N sample rewriting rules are selected using an interval sampling method. This method ensures that the selected sample rewriting rules are functionally diverse and representative. Two conditional branching mechanisms for creating replica subsets balance computational efficiency and sample quality, avoiding wasted processor cycles due to redundant rules in electronic devices, improving the reliability of subsequent rule validation processes, and resolving technical issues in related technologies such as excessive computational overhead or insufficient sample representativeness during rule validation sampling. Furthermore, the optimization effects of different subsets can be evaluated to select the optimal combination of rewriting rules. In this way, the efficiency and effectiveness of query rewriting can be improved while reducing the complexity of the optimization process.

[0188] In some embodiments, let n be a positive integer, 1≤n≤N, and for the nth replica subset of the N replica subsets in step 2041 above, perform the processing steps 2042 to 2044.

[0189] In step 2042, the nth sample rewriting rule is filtered out from the nth replica subset. Based on the remaining sample rewriting rules in the nth replica subset, the sample query statement is rewritten to obtain the second rewritten sample query statement.

[0190] In some embodiments, for the nth sample rewriting rule, if the performance metrics of the rewritten sample query obtained by executing the remaining sample rewriting rules in the nth replica subset are better than the performance metrics of the rewritten sample query obtained by executing all sample rewriting rules in the nth replica subset, it indicates that the nth sample rewriting rule has an improvement effect. Here, the remaining sample rewriting rules are the sample rewriting rules in the nth replica subset other than the nth sample rewriting rule.

[0191] As an example, the nth replica subset includes rules 1, 2, 3, and 4. When the second sample rewriting rule (i.e., rule 2) is removed, rules 1, 3, and 4 are executed to rewrite sample query statement A, resulting in rewritten sample query statement B (i.e., the second rewritten sample query statement).

[0192] In step 2043, the third performance metric parameter of the database is obtained when executing the second rewritten sample query statement.

[0193] In some embodiments, the second rewritten sample query statement is sent to the database and executed by the database. When the execution is completed, the performance testing tool built into the database (or independent of the database) is invoked to obtain the third performance indicator parameter during the execution of the second rewritten sample query statement by the database.

[0194] As an example, continuing from the previous example, the sample query statement A is rewritten using rules 1, 3, and 4 to obtain the rewritten sample query statement B. During the execution of the rewritten sample query statement B in the database, the memory usage is 100 MB (i.e., the third performance metric parameter).

[0195] In step 2044, in response to the second performance metric parameter being better than the third performance metric parameter, the nth sample rewriting rule is taken as the sample rewriting rule with an improvement effect.

[0196] Here, the second performance metric parameter is the performance metric parameter of the rewritten sample query statement obtained by executing all sample rewriting rules in the nth replica subset.

[0197] As an example, using rules 1, 2, 3, and 4 to rewrite sample query statement A, we obtain rewritten sample query statement C. During the execution of rewriting sample query statement C in the database, the memory usage was 80 MB (i.e., the second performance metric). This indicates that after eliminating rule 2, the memory usage performance metric of the rewritten sample query statement B is lower than that of the rewritten sample query statement C. Therefore, rule 2 is selected as the sample rewriting rule that improves the performance metric parameter of sample query statement A, and thus, rule 2 is considered a candidate rewriting rule for the sample query statement. The same process is performed on rules 1, 3, and 4 in the subset of valid rewriting rules to determine the candidate rewriting rules for the sample query statements.

[0198] This application embodiment creates N replica subsets of the effective rewriting rule subset, filters out sample rewriting rules from the replica subsets, compares the performance execution parameters of the rewritten sample query statements obtained by rewriting the sample query statements with the sample rewriting rules before and after filtering, and determines the sample rewriting rules with improvement effect in the effective rewriting rule subset. By analyzing the impact of each sample rewriting rule on the performance improvement of the sample query statement, the truly effective sample rewriting rules can be identified, avoiding the use of sample rewriting rules that cause query performance regression, thereby improving the quality of subsequent query statement rewriting.

[0199] Please refer to Figure 3F for further explanation following step 204 above.

[0200] In step 205, the sample query statements and their statistical information are encoded to obtain a candidate statement vector.

[0201] Here, statistical information refers to the underlying physical data information related to the execution context of the sample query statement. The candidate statement vector refers to the numerical vector features generated after feature encoding of the sample query statement and statistical information.

[0202] In some embodiments, step 205 above can be implemented as follows: The sample query statement is divided into multiple words, each word is encoded to obtain a third sub-vector, and the third sub-vectors corresponding to multiple words are fused to obtain a third vector. At least one indicator parameter included in the statistical information of the sample query statement is encoded to obtain a fourth sub-vector for each indicator parameter, and the fourth sub-vectors corresponding to multiple indicator parameters are fused to obtain a fourth vector. The third vector and the fourth vector are fused to obtain a candidate statement vector. For the specific implementation process, please refer to steps 1011 to 1013 above, which will not be repeated here.

[0203] In step 206, a case library is constructed based on the candidate statement vector and the candidate rewriting rules of the sample query statement.

[0204] In some embodiments, candidate statement vectors can be associated with candidate rewriting rules of sample query statements to build a case library. For example, each association can be represented as <candidate statement vector, candidate rewriting rule>.

[0205] Here, the case library refers to a matching benchmark library composed of performance-verified candidate statement vectors and corresponding candidate rewrite rules mapped together.

[0206] This application first extracts multiple subsets of rewriting rules from a second set of rewriting rules. Then, it identifies effective subsets of rewriting rules that improve the performance metrics of the sample query statement from these subsets. Next, it precisely identifies sample rewriting rules from the effective subsets that substantially improve the performance metrics of the sample query statement, using these as candidate rewriting rules for the sample query statement. Finally, it encodes the sample query statement and statistical information to obtain a candidate statement vector, and constructs a case library based on the candidate statement vector and candidate rewriting rules. This layered performance verification mechanism eliminates invalid rules that lower performance metrics. It deeply binds the candidate rewriting rules, which have undergone rigorous performance metric verification, with the candidate statement vector, which incorporates statistical information, to form a case library. This provides prior knowledge with underlying data features and high-value rule mapping relationships for subsequent query rewriting, significantly improving the reliability of high-quality rule retrieval by electronic devices and the hardware computing efficiency. It solves the technical problems in related technologies where directly applying unverified rewriting rules leads to query performance regression and a lack of high-quality matching benchmarks.

[0207] Please refer to Figure 3A for further explanation following step 102 above.

[0208] In step 103, based on the original query statement, statistical information and the first set of rewriting rules, a prompt word is constructed. The prompt word is used to indicate that, based on the statistical information, multiple target rewriting rules are selected from the first set of rewriting rules in the order of execution to optimize the original query statement.

[0209] In some embodiments, prompt words can be constructed based on the original query statement, statistical information, and a first set of rewriting rules to instruct the language model to select and combine a sequence of target rules that improve query performance from the first set of rewriting rules based on statistical information.

[0210] As an example, the prompt could be: "The original query is..., the statistics are..., the first set of rewriting rules is..., please select and combine the target rule sequence that improves the query performance of the original query based on the statistics."

[0211] In step 104, based on the prompt words, a pre-trained language model is invoked to generate a sequence of target rules, wherein the sequence of target rules includes multiple target rewriting rules arranged in the order of execution.

[0212] Here, prompt words refer to the text data used to input instructions and contextual information into the pre-trained language model. The pre-trained language model refers to a deep learning model trained on large-scale text data, possessing the ability to understand and generate natural language. The target rule sequence refers to the set of rules generated by the pre-trained language model and arranged in the order of execution, used to optimize the query statement. Target rewriting rules refer to individual, specific rules within the target rule sequence used to modify the structure of the query statement.

[0213] As examples, language models could be Baidu Language Model (BLM) or Alibaba Language Model (ALM).

[0214] In some embodiments, referring to FIG3I, FIG3I is a ninth flowchart of the database query statement processing method provided in the embodiments of this application. After step 104, steps 301 to 302 of FIG3I can be executed, which will be described in detail below.

[0215] In step 301, starting from the first target rewriting rule in the target rule sequence, the target rewriting rules are read one by one in the order of execution. The query statement to be rewritten is rewritten according to the read current rewriting rule to obtain the rewritten query statement. Wherein, if the current rewriting rule is the first target rewriting rule, the query statement to be rewritten is the original query statement. If the current rewriting rule is the second target rewriting rule or a subsequent target rewriting rule, the query statement to be rewritten is the rewritten query statement obtained by rewriting the previous target rewriting rule.

[0216] Here, the query statement to be rewritten is the rewritten query statement obtained by rewriting the query statement to be rewritten by the previous target rewriting rule. The current rewriting rule refers to the target rewriting rule currently being executed during the sequential reading of the target rule sequence. The query statement to be rewritten is the query statement that is processed as input when the current rewriting rule is applied. The rewritten query statement is the query statement generated after processing the query statement to be rewritten by the current rewriting rule. The target query statement is the final query statement obtained after rewriting the query statement to be rewritten by the last target rewriting rule in the target rule sequence.

[0217] For example, a pre-trained language model is invoked based on prompt words to generate a sequence of target rules containing three target rewriting rules. The first target rewriting rule is read from the sequence and used to rewrite the original query statement, resulting in the first rewritten query statement. Then, the second target rewriting rule is read and used to rewrite the original query statement, resulting in the second rewritten query statement. Finally, the third target rewriting rule is read and used to rewrite the original query statement, resulting in the third rewritten query statement.

[0218] As an example, there are 3 target rewriting rules in the target rule sequence, namely rule 1, rule 2 and rule 3. First, rule 1 is used to rewrite the original query statement A to obtain query statement B. Then, rule 2 is used to rewrite query statement B to obtain query statement C. Finally, rule 3 is used to rewrite query statement C to obtain query statement D.

[0219] In step 302, the rewritten query statement obtained by rewriting the last target rewriting rule is used as the final target query statement obtained by rewriting the target rule sequence.

[0220] As an example, the above rewrites query statement C using rule 3 to obtain query statement D, that is, query statement D is used as the final target query statement obtained by rewriting through the target rule sequence.

[0221] This application's embodiments start with the first target rewriting rule in the target rule sequence, reading each target rewriting rule sequentially according to its execution order. The query statement to be rewritten is then rewritten based on the current rule, and the rewritten query statement is used as input for the next rewrite. This chain-like rewriting mechanism, applying and iteratively updating each rule, ensures that the logical dependencies between target rewriting rules are strictly satisfied, avoiding intermediate state errors caused by disordered rule execution order. By converting the dynamic sequence generated by the language model into precise machine execution steps, the final generated target query statement is structurally optimized, improving its hardware execution efficiency in the database. This solves the technical problem in related technologies where fixed or parallel rule application logic leads to poor rewriting results or semantic errors.

[0222] In some embodiments, referring to FIG3J, FIG3J is a tenth flowchart of the database query statement processing method provided in the embodiments of this application. After step 104, steps 303 to 304 of FIG3J can be executed, which will be described in detail below.

[0223] In step 303, the original query statement is transformed into a first query plan tree, and the target query statement is transformed into a second query plan tree.

[0224] Here, the first query plan tree is a tree-structured representation of the underlying operation steps and data flow of the database executing the original query statement. The second query plan tree is a tree-structured representation of the underlying operation steps and data flow of the database executing the target query statement.

[0225] In some embodiments, a query statement can be transformed into a query plan tree as follows: First, the query statement is parsed using a parser to analyze its syntactic structure, decompose it into a series of tokens, and construct an initial parse tree. Then, the individual components of the parse tree are parsed to transform it into a query plan tree.

[0226] In step 304, in response to the expression of the first query plan tree having the same semantics as the expression of the second query plan tree, it is determined that the verification of the target rule sequence has passed.

[0227] Here, "expression" refers to the relational algebra or logical operation represented by each operation node and its connection relationship in the query plan tree. "Validation passed" means confirming that the rewriting operation generated by the target rule sequence has not changed the original query intent, thus confirming the final judgment state.

[0228] In some embodiments, when the expression of the first query plan tree is semantically identical to the expression of the second query plan tree, indicating that the original query statement and the target query statement are semantically identical, the target rule sequence is determined to have passed verification.

[0229] In some embodiments, the first and second query plan trees can be normalized, for example, by pushing down filter conditions and reordering join operations to generate two normalized query plan trees. Then, starting from the root node, the node types, operation parameters, and child node structures of the two normalized query plan trees are recursively compared layer by layer. In response to the complete consistency of the overall structure of the two normalized query plan trees, it is determined that the expressions of the first and second query plan trees are semantically identical.

[0230] As an example, firstly, the original query statement containing three table join operations is transformed into a first query plan tree with ten operation nodes, and the rewritten target query statement containing two table join operations is transformed into a second query plan tree with eight operation nodes. Secondly, the expressions of the first query plan tree with ten operation nodes are compared with the expressions of the second query plan tree with eight operation nodes to determine if they are semantically equivalent. Finally, in response to the logical equivalence of the expressions in the two query plan trees, the validation of the target rule sequence is determined to be successful.

[0231] This application's embodiments transform the original query statement and the target query statement into a first query plan tree and a second query plan tree. Then, it verifies the semantics by comparing the expressions of the two query plan trees. Finally, in response to semantic similarity, it determines that the verification of the target rule sequence has passed. This method transforms fuzzy text-level comparisons into structured logical expression equivalence judgments, constructing a deterministic automated verification mechanism. This automated verification mechanism ensures that the correctness of the rewritten target query statement is verified before being delivered to the database engine for execution, avoiding electronic devices from outputting erroneous data or wasting hardware computing resources due to executing semantically incorrect query statements, significantly improving the reliability of the query rewriting process. It solves the technical problem in related technologies where semantic errors are introduced due to the lack of rigorous verification during the query statement rewriting process.

[0232] In other embodiments, referring to Figure 3K, which is an eleventh flowchart of the database query statement processing method provided in the embodiments of this application. After step 104, steps 401 to 403 of Figure 3K can also be executed, as described in detail below.

[0233] In step 401, target rewriting rules are read one by one from the target rule sequence according to the execution order. The original query statement is rewritten according to the read target rewriting rule to obtain the rewritten query statement.

[0234] In some embodiments, when verifying the target rule sequence, the target rewriting rules can be read one by one from the target rule sequence in the order of execution, and the original query statement can be rewritten according to the read target rewriting rule to obtain the rewritten query statement, so as to verify the read target rewriting rules one by one.

[0235] As an example, the target rule sequence includes three target rewriting rules, namely rule 1, rule 2 and rule 3. First, rule 1 is read and used to rewrite the sample query statement A to obtain query statement B; then rule 2 is read and used to rewrite query statement B to obtain query statement C; finally, rule 3 is read and used to rewrite query statement C to obtain query statement D.

[0236] In step 402, in response to the fact that the original query statement and the rewritten query statement have the same semantics, it is determined that the current target rewriting rule has passed the verification, and the next target rewriting rule of the current target rewriting rule is read.

[0237] As an example, continuing from the previous example, we rewrite the sample query statement A using rule 1 to obtain query statement B. Then, we verify rule 1. If rule 1 passes verification, we rewrite query statement B using rule 2 to obtain query statement C. We then verify rule 2. If rule 2 passes verification, we finally rewrite query statement C using rule 3 to obtain the target query statement. We then verify rule 3. If rule 3 passes verification, it indicates that the target rule sequence has passed verification.

[0238] In some embodiments, referring to FIG3L, FIG3L is a schematic diagram of the twelfth step of the database query statement processing method provided in the embodiments of this application. The above step 402 can be implemented by steps 4021 to 4023 in FIG3L, which are described in detail below.

[0239] In step 4021, the original query statement is transformed into a third query plan tree, and the rewritten query statement is transformed into a fourth query plan tree.

[0240] As an example, the original query statement is:

[0241] "SELECT c.customer_name,o.order_date,p.product_name

[0242] FROM customers c

[0243] JOIN orders o ON c.customer_id=o.customer_id

[0244] JOIN order_items oi ON o.order_id=oi.order_id

[0245] JOIN products p ON oi.product_id=p.product_id

[0246] WHERE c.customer_id=1001".

[0247] The first query plan tree of the original query statement is:

[0248] The rewritten target query statement is:

[0249] The second query plan tree for the target query statement is:

[0250] In step 4022, the difference region between the fourth query plan tree and the third query plan tree is determined.

[0251] In some embodiments, step 4022 above can be implemented in the following way: starting from the level where the root node is located, compare the nodes of the fourth query plan tree and the third query plan tree at the same level layer by layer. If the nodes of the fourth query plan tree and the third query plan tree at any level meet the difference conditions, the level is taken as the difference region. The difference conditions include at least one of the following: node operation type mismatch, predicate expression structure difference, child node number mismatch, and projection column order inversion.

[0252] In step 4023, in response to the expression in the difference region of the third query plan tree having the same semantics as the expression in the difference region of the fourth query plan tree, it is determined that the verification of the current target rewrite rule read has passed.

[0253] As an example, in the example above, since the newly added filter condition only changes one subtree of the third query plan tree (corresponding to the difference area mentioned above), that is, the subtree of the third query plan tree before the rewrite is "TableScan[orders as o]", and the subtree of the fourth query plan tree after the rewrite is "Filter[o.customer_id=1001]TableScan[orders as o]", for this changed subtree, verifying equivalence only requires determining whether the added filter condition "o.customer_id=1001" can be inferred from the join condition "c.customer_id=o.customer_id" and the WHERE condition "c.customer_id=1001" of the original query statement. It only needs to determine whether the semantics of this local rewrite are equivalent, without comparing the entire query plan tree. Since “o.customer_id=1001” can be inferred from the join condition “c.customer_id=o.customer_id” and the WHERE condition “c.customer_id=1001” in the original query statement, the query statements before and after the rewrite are equivalent, and the verification passes.

[0254] Please refer to Figure 3K for further explanation following step 402 above.

[0255] In step 403, in response to the successful verification of multiple target rewriting rules, the verification of the target rule sequence is confirmed; the rewritten query statement obtained by rewriting through the last target rewriting rule is taken as the final target query statement obtained by rewriting through the target rule sequence.

[0256] As an example, the target rule sequence includes three target rewriting rules: Rule 1, Rule 2, and Rule 3. First, Rule 1 is used to rewrite the sample query statement A, resulting in query statement B. Rule 1 is then validated. If Rule 1 passes validation, Rule 2 is used to rewrite query statement B, resulting in query statement C. Rule 2 is then validated again. If Rule 2 passes validation, finally, Rule 3 is used to rewrite query statement C, resulting in query statement D. Rule 3 is then validated. If Rule 3 passes validation, the target rule sequence has been validated. Query statement D, obtained by rewriting query statement C using Rule 3, is then used as the target query statement.

[0257] This embodiment reads the target rewriting rules one by one in the target rule sequence, rewriting the original query statement to obtain the rewritten query statement. Each rewritten query statement is then verified. If the original query statement and the rewritten query statement have the same semantics, the current target rewriting rule is considered verified. This step-by-step verification and adjustment reduces errors caused by rule mistakes or improper order. Each rule can be verified independently during the rewriting process, facilitating problem location and debugging. If multiple target rewriting rules pass verification, the target rule sequence is considered verified. The rewritten query statement obtained by rewriting the last target rewriting rule is then used as the final target query statement obtained by rewriting the target rule sequence, replacing the original query statement. Only when all target rewriting rules pass verification is the validity of the target rule sequence finally determined. This avoids invalid or erroneous rewriting rules affecting the final result, thus optimizing the rewriting process.

[0258] In summary, this application's embodiments, based on the original statement vector obtained by encoding the original query statement and statistical information, retrieve multiple matching candidate statement vectors from the case library. This accurately identifies candidate statement vectors in the case library that are similar to the original query statement in terms of logical semantics and underlying data features, thereby reducing mismatches caused by single semantic or expression differences and avoiding ineffective computational resource consumption caused by applying inappropriate rules. Furthermore, the candidate rewriting rules associated with each candidate statement vector in the case library are combined into a first rewriting rule set. In other words, rewriting rules associated with candidate statement vectors can be automatically retrieved from the case library, achieving effective filtering of the vast global rule base of the database without manual intervention. This significantly improves the efficiency of rewriting rule generation and reduces database optimization costs. The algorithm reduces the processor computation time when blindly searching the rule base. Based on the original query statement, statistical information, and the first set of rewriting rules, prompt words are constructed to instruct the pre-trained language model to select the most suitable sequence of target rules from the first set of rewriting rules to rewrite the original query statement. With the assistance of the language model, the algorithm can more accurately identify and select rewriting rules, reducing errors and omissions that may occur due to manual operation. This breaks through the limitation of the fixed execution order of traditional database optimizers, enhances the flexibility and adaptability of the rewriting process, and can automatically generate a high-quality sequence of target rules that fits the original query statement. Ultimately, when the database executes the rewritten target query statement in the future, it can effectively reduce the number of disk input / output operations and memory usage, and shorten the overall query time.

[0259] The following describes a method for processing database query statements for verifying a given target rule sequence, as provided in an embodiment of this application. As mentioned earlier, the electronic device implementing the database query statement processing method of this application can be a terminal, a server, or a combination of both. Therefore, the executing entity of each step will not be described again below.

[0260] Referring to Figure 4, which is a schematic diagram of the thirteenth step of the database query statement processing method provided in the embodiments of this application, taking the terminal as the execution subject as an example, the steps 501 to 504 shown in Figure 4 will be described.

[0261] In step 501, the target rule sequence of the original query statement is obtained, wherein the target rule sequence includes multiple target rewriting rules.

[0262] As an example, the target rule sequence of the original query statement includes three target rewriting rules, namely rule 1, rule 2 and rule 3.

[0263] In step 502, the target rewriting rules are read one by one in the order of the target rewriting rules in the target rule sequence, and the original query statement is rewritten according to the current target rewriting rule read to obtain the rewritten query statement.

[0264] In some embodiments, when verifying the target rule sequence, the target rewriting rules can be read one by one from the target rule sequence in the order of execution, and the original query statement can be rewritten according to the read target rewriting rule to obtain the rewritten query statement, so as to verify the read target rewriting rules one by one.

[0265] As an example, the target rule sequence includes three target rewriting rules, namely rule 1, rule 2 and rule 3. First, rule 1 is read and used to rewrite the sample query statement A to obtain query statement B; then rule 2 is read and used to rewrite query statement B to obtain query statement C; finally, rule 3 is read and used to rewrite query statement C to obtain query statement D.

[0266] In step 503, in response to the fact that the original query statement and the rewritten query statement have the same semantics, it is determined that the current target rewriting rule has been verified and the next target rewriting rule of the current target rewriting rule is read.

[0267] In some embodiments, it can be determined whether the semantics of the original query statement and the rewritten query statement are the same by: transforming the original query statement into a third query plan tree and transforming the rewritten query statement into a fourth query plan tree; identifying the difference region between the fourth query plan tree and the third query plan tree; and determining that the expression in the difference region of the third query plan tree is semantically the same as the expression in the difference region of the fourth query plan tree, thus confirming that the verification of the current target rewriting rule has passed. The specific implementation process is described in steps 4021 to 4023 above, and will not be repeated here.

[0268] In step 504, in response to the successful verification of multiple target rewriting rules, the verification of the target rule sequence is confirmed. The rewritten query statement obtained by rewriting the last target rewriting rule is used as the final target query statement obtained by rewriting the target rule sequence, and the final target query statement replaces the original query statement.

[0269] As an example, the target rule sequence includes three target rewriting rules: Rule 1, Rule 2, and Rule 3. First, Rule 1 is used to rewrite the sample query statement A, resulting in query statement B. Rule 1 is then validated. If Rule 1 passes validation, Rule 2 is used to rewrite query statement B, resulting in query statement C. Rule 2 is then validated again. If Rule 2 passes validation, finally, Rule 3 is used to rewrite query statement C, resulting in query statement D. Rule 3 is then validated. If Rule 3 passes validation, the target rule sequence has been validated. Query statement D, obtained by rewriting query statement C using Rule 3, is then used as the target query statement.

[0270] In summary, this embodiment reads the target rewriting rules one by one in the target rule sequence to rewrite the original query statement, resulting in a rewritten query statement. If the original query statement and the rewritten query statement have the same semantics, the current target rewriting rule is verified, and the next target rewriting rule is read. Through step-by-step verification and adjustment, errors caused by rule errors or improper order are reduced. Each rule can be verified independently during the rewriting process, facilitating problem location and debugging. If multiple target rewriting rules are verified, the target rule sequence is verified. The rewritten query statement obtained by rewriting the last target rewriting rule is used as the final target query statement obtained by rewriting the target rule sequence, replacing the original query statement. Only when all target rewriting rules are verified is the validity of the target rule sequence finally determined. This avoids invalid or erroneous rewriting rules affecting the final result, thus optimizing the rewriting process.

[0271] The following will describe an exemplary application of the embodiments of this application in a real-world application scenario.

[0272] One of the core tasks of a Database Management System (DBMS) is to efficiently execute user-submitted queries. Users typically use declarative queries to describe the data they want to retrieve from the database, rather than specifying the concrete steps to obtain the data. The database query processor is responsible for transforming these declarative queries into an efficient physical execution plan. This transformation process usually includes stages such as parsing, query optimization, and query execution. The initial stage of query optimization involves rewriting the logical plan, i.e., rewriting the query statement. Rewriting the query statement is a crucial process in database query optimization, aiming to improve the database's query execution performance metrics by changing the structure of the original query statement without altering its semantics.

[0273] Rewriting query statements creates possibilities for selecting the optimal plan. Structured Query Language (SQL) allows users to express the same query intent in different ways. A complex logic can be implemented through nested subqueries, common expressions, or multiple joins. While these expressions are logically equivalent, they can lead to the database optimizer generating execution plans with vastly different efficiencies. Although the database cost-based optimizer (CBO) possesses basic cost estimation capabilities, its search space is enormous and it relies on cardinality estimation and cost models. Cardinality estimation helps the CBO predict how many rows of data each query operation (such as table scans, index lookups, joins, etc.) will process. The CBO needs to estimate the cost of different query execution plans, which typically includes disk I / O, Central Processing Unit (CPU) processing time, and memory usage. Certain query structures can cause the optimizer's cost estimation error to exceed a preset threshold, preventing the find of the optimal plan. Rewriting query statements can circumvent these problems by converting them into an "optimizer-friendly" form (e.g., eliminating redundant calculations, expanding subqueries into joins, simplifying predicate logic, etc.).

[0274] To ensure semantic consistency of the query before and after rewriting, some related technologies use validated rewriting rules to rewrite the original query, resulting in a rewritten query. However, this method applies rewriting rules in a fixed order, which means that after the previously applied rules change the query structure, subsequent rules that could have been applied may become inapplicable, thus missing potentially better rewriting paths. Furthermore, the application of rewriting rules usually depends on the database's cost estimation model. Since cost estimation models are based on statistical information, they can have significant errors, especially when dealing with complex queries or skewed data distributions. Incorrect cost estimation may cause the optimizer to reject beneficial rewriting rules or incorrectly apply rules that degrade performance, resulting in large estimation errors and preventing the rewriting of the optimal query. Additionally, predefined rule sets are finite and may not cover all effective or innovative rewriting methods in complex scenarios.

[0275] Related technologies for query optimization mainly fall into two research directions: discovering new rewriting rules and selecting existing rewriting rules. Regarding the discovery of new rewriting rules, Querybooster allows for the suggestion of new rules through a rule language, while Wetune transforms potential rewriting templates into actionable rules by compiling them and identifying constraints. While these methods have shown value in handling small workloads, they have the following limitations: Querybooster depends on the rewriting capability of the user-proposed rules, while Wetune is less effective with complex queries. Furthermore, these methods focus on expanding the rule set itself, failing to effectively address the core challenges of rule application order and rule selection based on inaccurate cost estimates.

[0276] To address the limitations of fixed execution order and simple heuristic selection in selecting existing rewrite rules, machine learning-based methods have been proposed. For example, Monte Carlo Tree Search (MCTS) can be used to optimize the rule selection process. This method treats each query as a query plan tree, and applying a rule is seen as modifying the tree structure. A machine learning-based cost model is used to predict the potential impact of applying specific rules, and MCTS is used to search for and select an optimal rule application sequence. Alternatively, the capabilities of Large Language Models (LLMs) can be leveraged to assist query rewriting. Rule-based methods can directly use LLMs to identify the best combination of rules in the optimizer that improves query performance and their application order.

[0277] However, the above methods cannot effectively optimize the query performance of specific databases. The lack of database statistics and insufficient understanding of the rewriting defects in specific databases can lead to the selection of many invalid rewriting rules or even performance regression.

[0278] The relevant technology selects rewriting rules from the Apache Calcite optimizer and allows the Calcite optimizer to apply the selected rules to generate rewritten query statements. However, the Calcite optimizer currently has significant correctness issues in both rule application and query generation. The generated query statements cannot guarantee semantic consistency, necessitating a determination of the equivalence between the pre- and post-rewriting query statements to remove erroneous rewriting rules.

[0279] To address the aforementioned technical problems, this application provides a query rewriting method based on a language model for processing database query statements. This method generates a sequence of target rules to be applied by calling a pre-trained language model. To improve the recommendation performance of the language model, this application analyzes the original query statement and its statistical information, and matches candidate query statements similar to the original query statement from a case library. The union of the rewriting rules associated with the candidate query statements is then used as a rule set and provided to the language model. Furthermore, this application improves the verification method by determining the equivalence of the query statements before and after applying the rewriting rules in stages. This verifies whether the semantics of the query statements before and after rewriting are the same, improving the success rate of verification and reducing the computational cost of verification. This application can be applied to scenarios where slow queries occur due to insufficient query rewriting capabilities in the target cloud database load.

[0280] The following describes the method for processing database query statements provided in the embodiments of this application. Referring to Figure 5, which is a schematic diagram illustrating the principle of query statement rewriting provided in the embodiments of this application, a specific explanation will be given using a terminal as the execution subject.

[0281] As shown in Figure 5, firstly, the original query statement is input into the case manager, which determines the rule set (corresponding to the first rewriting rule set mentioned above) from the case library. Secondly, the rule set, the original query statement, and the statistical information of the original query statement are sent to the pre-trained language model, and prompt words are constructed based on the original query statement, statistical information, and rule set. Then, the pre-trained language model is invoked to generate a target rule sequence based on the prompt words. Finally, the original query statement is rewritten using the target rule sequence to obtain the rewritten query statement. The validator is invoked to verify the rewritten query statement, and the rewritten query statement and verification results are output. The verification results indicate whether the semantics of the rewritten query statement are consistent with the original query statement.

[0282] In some embodiments, this application utilizes in-context learning (ICL) to integrate the language model into the query rewriting system, thereby avoiding fine-tuning of the language model. The language model's cue words instruct it to select and combine rewriting rule sequences that improve query performance based on statistical information from the rule set. Ultimately, this enables the optimizer to rewrite a better query statement according to the target rewriting sequence output by the language model.

[0283] In some embodiments, the rewriting rules in the rule set are divided into two categories: redundant expression removal rules and statistical information injection rules. Each type of rewriting rule contains a rewriting example <query before rewriting, query after rewriting>. Redundant expression removal rules are used to remove duplicate or redundant expression calculations from query statements. Applying this type of rewriting rule can improve query performance. The query examples described by this type of query only need to provide the query statements before and after rewriting to help the language model better understand the rule, without adding statistical information. Whether applying statistical information injection rules can improve query performance depends on the statistical information in the query statement, such as the selectivity of filter conditions. Therefore, it is necessary to add the statistical information in the query statement to the rewriting examples of this type of rewriting rule.

[0284] As an example, a redundant expression removal rule could be a simplified predicate logic rule. The rule set describes the simplified predicate logic rule as "Simplify predicate logic: Simplify the predicate logic in the query, including the simplification of Boolean expressions, constant folding, etc."

[0285] As an example, the simplified rewrite of predicate logic rules is as follows:

[0286] The original query statement before rewriting was:

[0287] SELECT order_id, order_date

[0288] FROM orders

[0289] WHERE order_amount>1000AND order_amount<5000

[0290] AND NOT(order_amount<=1000OR order_amount>=5000)”

[0291] The second condition NOT(order_amount<=1000OR order_amount>=5000) in the original query statement above is equivalent to order_amount>1000AND order_amount<5000, which is completely duplicated from the first condition and can be removed directly.

[0292] Therefore, the rewritten query statement is:

[0293] SELECT order_id, order_date

[0294] FROM orders

[0295] WHERE order_amount>1000AND order_amount<5000".

[0296] As an example, a statistics injection rule could be an aggregation condition pull-up rule. The rule set describes an aggregation condition pull-up rule as "Aggregation condition pull-up: Based on the selectivity of the aggregation condition and the join condition, determine whether the aggregation operation is executed before or after the join."

[0297] As an example, the rewrite of the aggregation condition pull-up rule is as follows:

[0298] The original query statement before rewriting was:

[0299] The statistics for the original query statement are as follows:

[0300] (1) Selectivity of GROUP BY order_id: 0.667

[0301] (2) Number of rows in the order_items table: 150,000

[0302] (3) Number of rows in the orders table: 100,000

[0303] The rewritten query statement is:

[0304] "SELECT o.customer_id,SUM(oi.quantity*oi.unit_price)as total_spending

[0305] FROM orders o

[0306] JOIN order_items oi ON o.order_id=oi.order_id

[0307] GROUP BY o.customer_id.

[0308] In some embodiments, the use of a case manager to select high-quality query example statements relevant to the current input query statement can effectively guide the language model's output and reduce the likelihood of it generating irrelevant or erroneous content. Therefore, dynamically selecting the most suitable query example statement for the input query statement is an important optimization problem.

[0309] First, a case library is generated using a case manager. Specifically, a Monte Carlo search algorithm is used to randomly select sample rewriting rules from the complete rule set (corresponding to the second rewriting rule set mentioned above), and these rules are applied to slow queries encountered online. The SQL queries before and after rewriting are re-executed in the database, retaining the queries with improved performance. Then, an iterative process is used to randomly discard the sample rewriting rules corresponding to each performance-improving query, rewriting and evaluating the query performance. If performance regresses, the sample rewriting rule is considered effective and is retained. Finally, truly effective sample rewriting rules are obtained as candidate rewriting rules. Finally, these sample queries and their statistical information are encoded to obtain candidate statement vectors. A case library is constructed based on these candidate statement vectors and candidate rewriting rules, represented as <candidate statement vector, candidate rewriting rule>.

[0310] Then, rewriting rules are dynamically selected from the rule set through the case manager. In this embodiment, the input original query statement and its statistical information are encoded to obtain an original statement vector. The k-Nearest Neighbors Algorithm (KNN) is used to select K (e.g., 10) query statements most similar to the input query statement from the case library, and the union of the rule sets of these query statements is taken as the rule set in the prompt words.

[0311] Since directly judging the semantic equivalence of two complex SQL queries before and after rewriting is computationally expensive, this application provides a phased local verification method to optimize the judge in order to reduce the computational cost of determining the semantic equivalence of the queries before and after rewriting and to improve the verification success rate when dealing with complex queries. By progressively applying the rewriting rules in the target rewriting sequence, it determines whether the local regions affected by each rewriting rule are equivalent, thereby decomposing the verification process of a complex query into sub-problems. This reduces the judgment overhead and increases the verification success rate.

[0312] In some embodiments, the criterion for determining whether the query statements before and after rewriting are equivalent is: if the relational algebraic expression of one SQL query statement can derive another SQL query statement through a series of relational algebraic equivalence rules, then the two SQL query statements are equivalent.

[0313] As an example, the original query statement is:

[0314] "SELECT c.customer_name,o.order_date,p.product_name

[0315] FROM customers c

[0316] JOIN orders o ON c.customer_id=o.customer_id

[0317] JOIN order_items oi ON o.order_id=oi.order_id

[0318] JOIN products p ON oi.product_id=p.product_id

[0319] WHERE c.customer_id=1001".

[0320] The query plan tree for the original query statement is as follows:

[0321] The rewritten query statement is:

[0322] The query plan tree for the rewritten query statement is as follows:

[0323] Since the newly added filter condition only changes one subtree in the query plan tree (corresponding to the difference area mentioned above), that is, the subtree before the rewrite is "TableScan[orders as o]", and the subtree after the rewrite is "Filter[o.customer_id=1001]TableScan[orders as o]", for this changed subtree, verifying equivalence only requires determining whether the added filter condition "o.customer_id=1001" can be inferred from the join condition "c.customer_id=o.customer_id" and the WHERE condition "c.customer_id=1001" in the original query statement. It only needs to determine whether the semantics of this partial rewrite are equivalent, without comparing the entire query plan tree. Since "o.customer_id=1001" can be inferred from the join condition "c.customer_id=o.customer_id" and the WHERE condition "c.customer_id=1001" in the original query statement, the query statements before and after the rewrite are equivalent, and the verification passes.

[0324] This application's embodiments generate a sequence of target rules to be applied by calling a pre-trained language model. To improve the recommendation performance of the language model, the original query statement and its statistical information are analyzed, and candidate query statements similar to the original query statement are matched from the case library. The union of the rewriting rules associated with the candidate query statements is used as a rule set and provided to the language model. This reduces the possibility of generating irrelevant or erroneous content, ensures the reliability of the rewriting results, effectively improves the execution performance of complex queries, and significantly reduces the execution latency of slow online queries. Furthermore, the verification method is improved by determining the equivalence of the query statements before and after applying the rewriting rules in stages to verify whether the semantics of the query statements before and after rewriting are the same, improving the success rate of verification and reducing the computational cost of verification.

[0325] The following description continues to illustrate the exemplary structure of the database query statement processing device 555-1 provided in this application embodiment as a software module. In some embodiments, as shown in FIG2A, the software module in the database query statement processing device 555-1 stored in memory 550-1 may include:

[0326] The encoding module 5551-1 is configured to encode the original query statement and its statistical information to obtain the original statement vector.

[0327] The combination module 5552-1 is configured to obtain multiple candidate statement vectors that match the original statement vector from the case library, and combine the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set.

[0328] The construction module 5553-1 is configured to construct prompt words based on the original query statement, statistical information and the first set of rewriting rules. The prompt words are used to indicate that multiple target rewriting rules for optimizing the original query statement are selected from the first set of rewriting rules in order of execution based on the statistical information.

[0329] The generation module 5554-1 is configured to generate a sequence of target rules based on prompt words by calling a pre-trained language model. The sequence of target rules includes multiple target rewriting rules arranged in the order of execution.

[0330] In some embodiments, the combination module 5552-1 is further configured to determine the similarity between each candidate statement vector in the case library and the original statement vector, wherein the case library includes multiple candidate statement vectors and candidate rewriting rules associated with each candidate statement vector; sort each candidate statement vector in the case library in descending order according to similarity, and take the multiple candidate statement vectors starting from the first in the sorting result as multiple candidate statement vectors that match the original statement vector.

[0331] In some embodiments, the combination module 5552-1 is further configured to perform union processing on the candidate rewriting rules associated with each candidate statement vector in the case library to obtain the filtered candidate rewriting rules; and combine the filtered candidate rewriting rules into a first rewriting rule set.

[0332] In some embodiments, the combination module 5552-1 is further configured to: obtain a sample query statement; extract multiple subsets of rewriting rules from the second rewriting rule set; determine a subset of effective rewriting rules from the multiple subsets of rewriting rules that improve the performance index parameters of the sample query statement; determine sample rewriting rules from the subset of effective rewriting rules that improve the performance index parameters of the sample query statement, and use the determined sample rewriting rules as candidate rewriting rules for the sample query statement; encode the sample query statement and its statistical information to obtain a candidate statement vector; and construct a case library based on the candidate statement vector and the candidate rewriting rules for the sample query statement.

[0333] In some embodiments, the combination module 5552-1 is further configured to obtain historical query statements from the database that meet the filtering conditions, and use the historical query statements that meet the filtering conditions as sample query statements, wherein the filtering conditions include at least one of the following: the performance index parameters of the historical query statements are statistically analyzed to include at least one of the following: the execution time of the historical query statement is greater than a first time threshold, the difference between the number of rows scanned and the number of rows returned in the historical query statement is greater than a row number threshold, the number of inputs / outputs of the historical query statement is greater than a number threshold, the processor usage of the historical query statement is greater than a first resource threshold, the memory usage of the historical query statement is greater than a second resource threshold, and the lock wait time of the historical query statement is greater than a second time threshold.

[0334] In some embodiments, the combination module 5552-1 is further configured to perform the following processing for each subset of rewriting rules: rewrite the sample query statement based on the subset of rewriting rules to obtain a first rewritten sample query statement; obtain a first performance indicator parameter of the database when executing the sample query statement, and a second performance indicator parameter of the database when executing the first rewritten sample query statement; and, in response to the second performance indicator parameter being better than the first performance indicator parameter, use the subset of rewriting rules as a valid subset of rewriting rules.

[0335] In some embodiments, the combination module 5552-1 is further configured to send a sample query statement to the database and execute the sample query statement through the database; when the database completes the execution of the sample query statement, a performance testing tool is invoked to obtain a first performance indicator parameter during the execution of the sample query statement by the database, wherein the first performance indicator parameter includes at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table and sorting.

[0336] In some embodiments, the combination module 5552-1 is further configured to send the first rewritten sample query statement to the database and execute the first rewritten sample query statement through the database; when the database finishes executing the first rewritten sample query statement, a performance testing tool is invoked to obtain a second performance indicator parameter during the period when the database executes the first rewritten sample query statement, wherein the second performance indicator parameter includes at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table and sorting.

[0337] In some embodiments, the combination module 5552-1 is further configured to send the first rewritten sample query statement to the database and execute the first rewritten sample query statement through the database; when the database finishes executing the first rewritten sample query statement, a performance testing tool is invoked to obtain a second performance indicator parameter during the period when the database executes the first rewritten sample query statement, wherein the second performance indicator parameter includes at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table and sorting.

[0338] In some embodiments, the combination module 5552-1 is further configured to create N replica subsets of valid rewrite rule subsets, where N is an integer greater than or equal to 2; let n be a positive integer, and for the nth replica subset, perform the following processing: filter out the nth sample rewrite rule from the nth replica subset, rewrite the sample query statement based on the remaining sample rewrite rules in the nth replica subset to obtain a second rewritten sample query statement; obtain the third performance index parameter of the database when executing the second rewritten sample query statement; in response to the second performance index parameter being better than the third performance index parameter, regard the nth sample rewrite rule as a sample rewrite rule with an improvement effect.

[0339] In some embodiments, the combination module 5552-1 is further configured to: when the number of sample rewriting rules included in the effective rewriting rule subset is greater than N, arbitrarily select N sample rewriting rules from the effective rewriting rule subset, and create N replica subsets of the effective rewriting rule subset based on the selected N sample rewriting rules, wherein each replica subset includes N sample rewriting rules; or, when the number of sample rewriting rules included in the effective rewriting rule subset is greater than or equal to 2N, sort the sample rewriting rules in the effective rewriting rule subset, wherein the similarity between any two adjacent sample rewriting rules is greater than the similarity between any two non-adjacent sample rewriting rules; select N sample rewriting rules from the sorting result in an interval sampling manner, and create N replica subsets of the effective rewriting rule subset based on the selected N sample rewriting rules, wherein each replica subset includes N sample rewriting rules.

[0340] In some embodiments, the generation module 5554-1 is further configured to read target rewriting rules one by one in the order of execution, starting from the first target rewriting rule of a sequence of multiple target rules, and rewrite the query statement to be rewritten according to the read current rewriting rule to obtain the rewritten query statement. Wherein, if the current rewriting rule is the first target rewriting rule, the query statement to be rewritten is the original query statement; if the current rewriting rule is the second or subsequent target rewriting rule, the query statement to be rewritten is the rewritten query statement obtained by rewriting the previous target rewriting rule; and the rewritten query statement obtained by rewriting the last target rewriting rule is taken as the target query statement finally obtained by rewriting the target rule sequence.

[0341] In some embodiments, the generation module 5554-1 is further configured to transform the original query statement into a first query plan tree and the target query statement into a second query plan tree; in response to the expression of the first query plan tree having the same semantics as the expression of the second query plan tree, the verification of the target rule sequence is determined to be successful.

[0342] In some embodiments, the generation module 5554-1 is further configured to read target rewriting rules one by one from the target rule sequence in the order of execution, rewrite the original query statement according to the read current target rewriting rule to obtain the rewritten query statement; in response to the semantics of the original query statement and the rewritten query statement being the same, determine that the current target rewriting rule has been verified, and continue to read the next target rewriting rule of the current target rewriting rule; in response to the verification of multiple target rewriting rules being passed, determine that the verification of the target rule sequence has been passed; and take the rewritten query statement obtained by rewriting through the last target rewriting rule as the target query statement finally obtained by rewriting through the target rule sequence.

[0343] In some embodiments, the generation module 5554-1 is further configured to convert the original query statement into a third query plan tree, and convert the rewritten query statement into a fourth query plan tree; determine the difference region between the fourth query plan tree and the third query plan tree; and, in response to an expression in the difference region of the third query plan tree having the same semantics as an expression in the difference region of the fourth query plan tree, determine that the verification of the current target rewriting rule read has passed.

[0344] In some embodiments, the generation module 5554-1 is further configured to compare the nodes of the fourth query plan tree and the third query plan tree at the same level, starting from the level where the root node is located. If the nodes of the fourth query plan tree and the third query plan tree at any level meet the difference conditions, the level is taken as the difference region. The difference conditions include at least one of the following: node operation type mismatch, predicate expression structure difference, child node number mismatch, and projection column order inversion.

[0345] In some embodiments, the encoding module 5551-1 is further configured to divide the original query statement into multiple words, encode each word to obtain a first sub-vector, fuse the first sub-vectors corresponding to the multiple words respectively to obtain a first vector; encode at least one indicator parameter included in the statistical information of the original query statement to obtain a second sub-vector for each indicator parameter, fuse the second sub-vectors corresponding to the multiple indicator parameters respectively to obtain a second vector; and fuse the first vector and the second vector to obtain the original statement vector.

[0346] In some embodiments, the encoding module 5551-1 is further configured to concatenate the first vector and the second vector to obtain the original statement vector; or, based on a first weight preset for the first vector and a second weight preset for the second vector, to perform a weighted summation of the first vector and the second vector to obtain the original statement vector.

[0347] The following description continues to illustrate the exemplary structure of the database query statement processing device 555-2 provided in this application embodiment as a software module. In some embodiments, as shown in FIG2B, the software module in the database query statement processing device 555-2 stored in the memory 550-2 may include:

[0348] The acquisition module 5551-2 is configured to acquire the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewriting rules.

[0349] The rewriting module 5552-2 is configured to read the target rewriting rules one by one in the target rule sequence, and rewrite the original query statement according to the current target rewriting rule to obtain the rewritten query statement.

[0350] The module 5553-2 is configured to, in response to the semantic similarity between the original query statement and the rewritten query statement, determine that the current target rewriting rule has been verified and continue reading the next target rewriting rule of the current target rewriting rule; in response to the verification of multiple target rewriting rules, determine that the verification of the target rule sequence has been verified, take the rewritten query statement obtained by rewriting through the last target rewriting rule as the final target query statement obtained by rewriting through the target rule sequence, and replace the original query statement with the final target query statement.

[0351] This application provides a computer program product, which includes a computer program or computer-executable instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer-executable instructions from the computer-readable storage medium and executes the computer-executable instructions, causing the electronic device to perform the database query statement processing method described above in this application.

[0352] This application provides a computer-readable storage medium storing computer-executable instructions or computer programs. When the computer-executable instructions or computer programs are executed by a processor, the processor will execute the database query statement processing method provided in this application, such as the database query statement processing method shown in FIG3A.

[0353] In some embodiments, the computer-readable storage medium may be a memory such as RAM, ROM, flash memory, magnetic surface memory, optical disk, or CD-ROM; or it may be a variety of devices including one or any combination of the above-mentioned memories.

[0354] In some embodiments, computer-executable instructions may take the form of programs, software, software modules, scripts, or code, written in any form of programming language (including compiled or interpreted languages, or declarative or procedural languages), and may be deployed in any form, including as stand-alone programs or as modules, components, subroutines, or other units suitable for use in a computing environment.

[0355] As an example, computer-executable instructions may, but do not necessarily, correspond to files in a file system. They may be stored as part of a file that holds other programs or data, for example, in one or more scripts in a Hyper Text Markup Language (HTML) document, in a single file dedicated to the program in question, or in multiple co-located files (e.g., files that store one or more modules, subroutines, or code sections).

[0356] As an example, computer-executable instructions can be deployed to execute on a single electronic device, or on multiple electronic devices located at one location, or on multiple electronic devices distributed across multiple locations and interconnected via a communication network.

[0357] In summary, this application's embodiments, based on the original statement vector obtained by encoding the original query statement and statistical information, retrieve multiple matching candidate statement vectors from the case library. This accurately identifies candidate statement vectors in the case library that are similar to the original query statement in terms of logical semantics and underlying data features, thereby reducing mismatches caused by single semantic or expression differences and avoiding ineffective computational resource consumption caused by applying inappropriate rules. Furthermore, the candidate rewriting rules associated with each candidate statement vector in the case library are combined into a first rewriting rule set. In other words, rewriting rules associated with candidate statement vectors can be automatically retrieved from the case library, achieving effective filtering of the vast global rule base of the database without manual intervention. This significantly improves the efficiency of rewriting rule generation and reduces database optimization costs. The algorithm reduces the processor computation time when blindly searching the rule base. Based on the original query statement, statistical information, and the first set of rewriting rules, prompt words are constructed to instruct the pre-trained language model to select the most suitable sequence of target rules from the first set of rewriting rules to rewrite the original query statement. With the assistance of the language model, the algorithm can more accurately identify and select rewriting rules, reducing errors and omissions that may occur due to manual operation. This breaks through the limitation of the fixed execution order of traditional database optimizers, enhances the flexibility and adaptability of the rewriting process, and can automatically generate a high-quality sequence of target rules that fits the original query statement. Ultimately, when the database executes the rewritten target query statement in the future, it can effectively reduce the number of disk input / output operations and memory usage, and shorten the overall query time.

[0358] The above description is merely an embodiment of this application and is not intended to limit the scope of protection of this application. Any modifications, equivalent substitutions, and improvements made within the spirit and scope of this application are included within the scope of protection of this application.

Claims

1. A method for processing a database query statement, the method being executed by an electronic device, the method comprising: The original query statement and its statistical information are encoded to obtain the original statement vector; Multiple candidate statement vectors that match the original statement vector are obtained from the case library, and the candidate rewriting rules associated with each candidate statement vector in the case library are combined into a first rewriting rule set. Based on the original query statement, the statistical information, and the first set of rewriting rules, a prompt word is constructed, wherein the prompt word is used to indicate that, based on the statistical information, multiple target rewriting rules for optimizing the original query statement are selected from the first set of rewriting rules in order of execution. Based on the prompt words, a pre-trained language model is invoked to generate a sequence of target rules, wherein the sequence of target rules includes a plurality of target rewriting rules arranged in the order of execution.

2. The method of claim 1, wherein, The step of obtaining multiple candidate statement vectors that match the original statement vector from the case library includes: Determine the similarity between each candidate statement vector in the case library and the original statement vector, wherein the case library includes multiple candidate statement vectors and a candidate rewriting rule associated with each candidate statement vector; Each candidate statement vector in the case library is sorted in descending order according to the similarity, and the multiple candidate statement vectors starting from the first in the sorting result are taken as multiple candidate statement vectors that match the original statement vector.

3. The method of claim 1 or 2, wherein, The step of combining the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set includes: The candidate rewriting rules associated with each candidate statement vector in the case library are subjected to union processing to obtain the filtered candidate rewriting rules; The filtered candidate rewriting rules are combined into a first rewriting rule set.

4. The method according to any one of claims 1 to 3, wherein, The case library was constructed in the following way: Get the sample query statement; Extract multiple subsets of rewriting rules from the second set of rewriting rules; From multiple subsets of rewrite rules, determine the effective subset of rewrite rules that improves the performance metrics parameters of the sample query statement; From the subset of valid rewriting rules, identify sample rewriting rules that improve the performance metrics of the sample query statement, and use the identified sample rewriting rules as candidate rewriting rules for the sample query statement. The sample query statements and their statistical information are encoded to obtain a candidate statement vector; The case library is constructed based on the candidate statement vector and the candidate rewriting rules of the sample query statement.

5. The method of claim 4, wherein, The query statement for obtaining samples includes: Obtain historical query statements from the database that meet the filtering conditions, and use the historical query statements that meet the filtering conditions as sample query statements, wherein the filtering conditions include at least one of the following: The performance metrics of the historical query statement include at least one of the following: the execution time of the historical query statement is greater than a first time threshold; the difference between the number of rows scanned and the number of rows returned by the historical query statement is greater than a row count threshold; the number of inputs / outputs of the historical query statement is greater than a count threshold; the processor usage of the historical query statement is greater than a first resource threshold; the memory usage of the historical query statement is greater than a second resource threshold; and the lock wait time of the historical query statement is greater than a second time threshold.

6. The method of claim 4, wherein, The step of determining the effective subset of rewrite rules that improves the performance metrics of the sample query statement from multiple subsets of rewrite rules includes: For each subset of the rewrite rules, perform the following processing: The sample query statement is rewritten based on the subset of rewriting rules to obtain the first rewritten sample query statement; Obtain the first performance index parameter of the database when executing the sample query statement, and the second performance index parameter of the database when executing the first rewritten sample query statement; In response to the second performance metric parameter being superior to the first performance metric parameter, the subset of rewriting rules is selected as the valid subset of rewriting rules.

7. The method of claim 6, wherein, The step of obtaining the first performance metric parameter of the database when executing the sample query statement includes: The sample query statement is sent to the database, and the sample query statement is executed through the database. When the database completes the execution of the sample query statement, a performance testing tool is invoked to obtain the first performance indicator parameters during the execution of the sample query statement by the database. The first performance indicator parameters include at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table, and sorting.

8. The method of claim 6, wherein, The step of obtaining the second performance metric parameter of the database when executing the first rewritten sample query statement includes: The first rewritten sample query statement is sent to the database, and the first rewritten sample query statement is executed through the database. When the database completes the execution of the first rewritten sample query statement, a performance testing tool is invoked to obtain a second performance indicator parameter during the execution of the first rewritten sample query statement. The second performance indicator parameter includes at least one of the following: execution time, difference between the number of rows scanned and the number of rows returned, number of input / output operations, processor usage, memory usage, lock wait time, temporary table, and sorting.

9. The method according to any one of claims 4 to 8, wherein, The step of determining sample rewriting rules from the subset of valid rewriting rules that improve the performance metrics of the sample query statement includes: Create N duplicate subsets of the valid rewrite rule subset, where N is an integer greater than or equal to 2; Let n be a positive integer, 1≤n≤N. For the nth replica subset, perform the following processing: From the nth replica subset, the nth sample rewriting rule is filtered out. Based on the remaining sample rewriting rules in the nth replica subset, the sample query statement is rewritten to obtain the second rewritten sample query statement. Obtain the third performance metric parameter of the database when executing the second rewritten sample query statement; In response to the second performance metric parameter being superior to the third performance metric parameter, the nth sample rewriting rule is selected as a sample rewriting rule with an improvement effect.

10. The method of claim 9, wherein, The creation of N replica subsets of the effective rewrite rule subset includes: If the number of sample rewriting rules included in the effective rewriting rule subset is greater than N, N sample rewriting rules are arbitrarily selected from the effective rewriting rule subset, and N replica subsets of the effective rewriting rule subset are created based on the selected N sample rewriting rules, wherein each replica subset includes N sample rewriting rules; or, If the number of sample rewriting rules included in the effective rewriting rule subset is greater than or equal to 2N, the sample rewriting rules in the effective rewriting rule subset are sorted, wherein the similarity between any two adjacent sample rewriting rules is greater than the similarity between any two non-adjacent sample rewriting rules; N sample rewriting rules are selected from the sorted results by interval sampling, and N replica subsets of the effective rewriting rule subset are created based on the selected N sample rewriting rules, wherein each replica subset includes N sample rewriting rules.

11. The method according to any one of claims 1 to 10, wherein, After generating the target rule sequence by calling a pre-trained language model based on the prompt words, the method further includes: Starting with the first target rewriting rule in the target rule sequence, the target rewriting rules are read one by one in the order of execution. The query statement to be rewritten is rewritten according to the current rewriting rule read, resulting in a rewritten query statement. Wherein, if the current rewriting rule is the first target rewriting rule, the query statement to be rewritten is the original query statement. If the current rewriting rule is the second target rewriting rule or a subsequent target rewriting rule, the query statement to be rewritten is the rewritten query statement obtained by rewriting the previous target rewriting rule. The rewritten query statement obtained by rewriting the last target rewriting rule is taken as the final target query statement obtained by rewriting the target rule sequence.

12. The method of claim 11, wherein, After taking the rewritten query statement obtained by rewriting through the last target rewriting rule as the final target query statement obtained by rewriting through the target rule sequence, the method further includes: The original query statement is transformed into a first query plan tree, and the target query statement is transformed into a second query plan tree; In response to the expression in the first query plan tree having the same semantics as the expression in the second query plan tree, it is determined that the verification of the target rule sequence has passed.

13. The method according to any one of claims 1 to 10, wherein, After invoking the pre-trained language model to generate the target rule sequence, the method further includes: According to the execution order, the target rewriting rules are read one by one from the target rule sequence, and the original query statement is rewritten according to the currently read target rewriting rule to obtain the rewritten query statement; In response to the fact that the original query statement and the rewritten query statement have the same semantics, it is determined that the current target rewriting rule has passed the verification, and the next target rewriting rule of the current target rewriting rule is read. In response to the fact that all of the multiple target rewriting rules have been verified, the verification of the target rule sequence is determined to be successful; the rewritten query statement obtained by rewriting through the last target rewriting rule is taken as the final target query statement obtained by rewriting through the target rule sequence.

14. The method of claim 13, wherein, The step of determining that the target rewriting rule read has passed verification in response to the semantic similarity between the original query statement and the rewritten query statement includes: The original query statement is transformed into a third query plan tree, and the rewritten query statement is transformed into a fourth query plan tree. Determine the region of difference between the fourth query plan tree and the third query plan tree; In response to the expression in the difference region of the third query plan tree having the same semantics as the expression in the difference region of the fourth query plan tree, it is determined that the verification of the currently read target rewrite rule has passed.

15. The method of claim 14, wherein, Determining the difference region between the fourth query plan tree and the third query plan tree includes: Starting from the level of the root node, compare the nodes at the same level of the fourth query plan tree with those of the third query plan tree, level by level. If the fourth query plan tree and the third query plan tree satisfy the difference condition at any level of the nodes, the level is taken as the difference region. The difference condition includes at least one of the following: node operation type mismatch, predicate expression structure difference, child node number mismatch, and projection column order inversion.

16. The method of any one of claims 1 to 15, wherein, The process of encoding the original query statement and its statistical information to obtain the original statement vector includes: The original query statement is divided into multiple words, each word is encoded to obtain a first sub-vector, and the first sub-vectors corresponding to the multiple words are merged to obtain a first vector. The statistical information of the original query statement includes at least one indicator parameter, which is encoded to obtain a second sub-vector for each indicator parameter. The second sub-vectors corresponding to the multiple indicator parameters are then merged to obtain a second vector. The first vector and the second vector are merged to obtain the original statement vector.

17. The method of claim 16, wherein, The step of fusing the first vector and the second vector to obtain the original statement vector includes: Concatenate the first vector with the second vector to obtain the original statement vector; or, Based on the first weight preset for the first vector and the second weight preset for the second vector, the first vector and the second vector are weighted and summed to obtain the original statement vector.

18. A method for processing a database query statement, the method being executed by an electronic device, the method comprising: Obtain the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewrite rules; According to the order of the target rewriting rules in the target rule sequence, the target rewriting rules are read one by one, and the original query statement is rewritten according to the currently read target rewriting rule to obtain the rewritten query statement; In response to the fact that the original query statement and the rewritten query statement have the same semantics, it is determined that the current target rewriting rule is verified and the next target rewriting rule is read. In response to the successful verification of the multiple target rewriting rules, the verification of the target rule sequence is confirmed. The rewritten query statement obtained by rewriting the last target rewriting rule is taken as the final target query statement obtained by rewriting the target rule sequence, and the final target query statement replaces the original query statement.

19. A database query statement processing apparatus, the apparatus comprising: The encoding module is configured to encode the original query statement and its statistical information to obtain the original statement vector. The combination module is configured to obtain multiple candidate statement vectors that match the original statement vector from the case library, and combine the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set. The construction module is configured to construct prompt words based on the original query statement, the statistical information, and the first set of rewriting rules, wherein the prompt words are used to indicate that, based on the statistical information, multiple target rewriting rules for optimizing the original query statement are selected from the first set of rewriting rules in order of execution. The generation module is configured to generate a target rule sequence by calling a pre-trained language model based on the prompt words, wherein the target rule sequence includes the plurality of target rewriting rules arranged in the order of execution.

20. A database query statement processing apparatus, the apparatus comprising: The acquisition module is configured to acquire the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewriting rules; The rewriting module is configured to read the target rewriting rules one by one in the target rule sequence, and rewrite the original query statement according to the currently read target rewriting rule to obtain the rewritten query statement; The determination module is configured to, in response to the semantic similarity between the original query statement and the rewritten query statement, determine that the current target rewriting rule has been verified and continue reading the next target rewriting rule of the current target rewriting rule; in response to the verification of multiple target rewriting rules, determine that the verification of the target rule sequence has been verified, take the rewritten query statement obtained by rewriting through the last target rewriting rule as the final target query statement obtained by rewriting through the target rule sequence, and replace the original query statement with the final target query statement.

21. An electronic device, the electronic device comprising: Memory is used to store executable instructions or computer programs. A processor, when executing computer-executable instructions or computer programs stored in the memory, implements the database query statement processing method according to any one of claims 1 to 18.

22. A computer-readable storage medium storing computer-executable instructions or a computer program, wherein the computer-executable instructions or the computer program, when executed by a processor, implement the method for processing a database query statement as described in any one of claims 1 to 18.

23. A computer program product comprising computer-executable instructions or a computer program, wherein the computer-executable instructions or the computer program, when executed by a processor, implement the method for processing a database query statement as described in any one of claims 1 to 18.