Database query statement processing method and device, electronic equipment, computer readable storage medium and computer program product
Patent Information
- Application Number
- CN202510615362.X
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-05-13
- Publication Date
- 2026-10-30
AI Technical Summary
[0002]结构化查询语言(Structured Query Language,SQL)允许用户以不同的方式表达相同的查询意图,一个复杂的逻辑可以通过嵌套子查询、公用表达式或多个连接来实现,这些表达方式虽然逻辑等价,但不同的查询语句可能导致对数据库的查询效率差异巨大
[0033]根据对原始查询语句及统计信息进行编码得到的原始语句向量,从案例库中获取匹配的多个候选语句向量,可以精确地识别出案例库中与原始查询语句相似的候选语句向量,从而减少了因语义或表达差异导致的误匹配;
Smart Images

Figure CN123011939A_ABST
Abstract
Description
Technical Field
[0001] This application relates to cloud technology, and more particularly to a method, apparatus, electronic device, computer-readable storage medium, and computer program product for processing database query statements. Background Technology
[0002] 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, different query statements can lead to significant differences in database query efficiency. Related techniques, when rewriting the original query statement, apply rewriting rules in a fixed order, which cannot be applied to all complex queries or may incorrectly apply rewriting rules that degrade query performance, resulting in the inability to rewrite the optimal query statement. Summary of the Invention
[0003] 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 a high-quality target rule sequence adapted to the original query statement.
[0004] The technical solution of this application embodiment is implemented as follows:
[0005] This application provides a method for processing database query statements, the method including:
[0006] The original query statement and its statistical information are encoded to obtain the original statement vector;
[0007] 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.
[0008] 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.
[0009] 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.
[0010] In the above scheme, obtaining the second performance indicator parameter of the database when executing the first rewritten sample query statement includes:
[0011] The first rewritten sample query statement is sent to the database so that the database executes the first rewritten sample query statement.
[0012] 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.
[0013] This application provides a database query statement processing apparatus, the apparatus comprising:
[0014] The encoding module is used to encode the original query statement and its statistical information to obtain the original statement vector.
[0015] The combination module is used 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.
[0016] The construction module is used to construct prompt words based on the original query statement, the statistical information and the first rewriting rule set, wherein the prompt words are used to indicate that multiple target rewriting rules for optimizing the original query statement are selected from the first rewriting rule set in order of execution based on the statistical information.
[0017] The generation module is used 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.
[0018] This application provides a method for processing database query statements, the method including:
[0019] Obtain the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewrite rules;
[0020] 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;
[0021] 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.
[0022] In response to the successful verification of all 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 used as the target query statement finally obtained by rewriting the target rule sequence, and is used to replace the original query statement.
[0023] This application provides a database query statement processing apparatus, the apparatus comprising:
[0024] The acquisition module is used to acquire the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewriting rules;
[0025] The rewriting module is used to read the target rewriting rules one by one in the target rule sequence, and rewrite the original query statement according to the read target rewriting rule to obtain the rewritten query statement;
[0026] 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 the last target rewriting rule as the target query statement finally obtained by rewriting the target rule sequence, so as to replace the original query statement.
[0027] This application provides an electronic device, the electronic device comprising:
[0028] Memory is used to store executable instructions or computer programs.
[0029] 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.
[0030] 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.
[0031] 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.
[0032] The embodiments of this application have the following beneficial effects:
[0033] 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, thereby reducing mismatches caused by semantic or expression differences.
[0034] 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 without manual intervention, which greatly improves the efficiency of rewriting rule generation.
[0035] 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 target rule sequence 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, and enhancing the flexibility and adaptability of the rewriting process. This enables the automatic generation of a high-quality target rule sequence that is adapted to the original query statement. Attached Figure Description
[0036] Figure 1 This is a schematic diagram of the architecture of the database query statement processing system 100 provided in the embodiments of this application;
[0037] Figure 2A This is a schematic diagram of the structure of the electronic device 500-1 provided in the embodiments of this application;
[0038] Figure 2B This is a schematic diagram of the structure of the electronic device 500-2 provided in the embodiments of this application;
[0039] Figure 3A This is a first flowchart illustrating the method for processing database query statements provided in this application embodiment;
[0040] Figure 3B This is a second flowchart illustrating the database query statement processing method provided in the embodiments of this application;
[0041] Figure 3C This is a third flowchart illustrating the database query statement processing method provided in the embodiments of this application;
[0042] Figure 3D This is a fourth flowchart illustrating the database query statement processing method provided in this application embodiment;
[0043] Figure 3E This is a fifth flowchart illustrating the database query statement processing method provided in this application embodiment;
[0044] Figure 3F This is a sixth flowchart illustrating the database query statement processing method provided in this application embodiment;
[0045] Figure 3G This is a seventh flowchart illustrating the database query statement processing method provided in this application embodiment;
[0046] Figure 3H This is the eighth flowchart of the database query statement processing method provided in the embodiments of this application;
[0047] Figure 3I This is a ninth flowchart illustrating the database query statement processing method provided in this application embodiment;
[0048] Figure 3J This is a schematic diagram of the tenth process of the database query statement processing method provided in the embodiments of this application;
[0049] Figure 3K This is a schematic diagram of the eleventh step in the processing method of database query statements provided in the embodiments of this application;
[0050] Figure 3L This is a schematic diagram of the twelfth step of the database query statement processing method provided in the embodiments of this application;
[0051] Figure 4 This is a schematic diagram of the thirteenth step of the database query statement processing method provided in the embodiments of this application;
[0052] Figure 5 This is a schematic diagram illustrating the principle of query statement rewriting provided in the embodiments of this application.
[0053] 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
[0054] 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.
[0055] 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.
[0056] 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.
[0057] 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.
[0058] Unless otherwise specified, "at least one" as used below refers to one or more cases, and "multiple" can refer to two or more cases.
[0059] 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.
[0060] 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.
[0061] 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.
[0062] 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.
[0063] 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.
[0064] 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.
[0065] 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.
[0066] 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.
[0067] 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.
[0068] 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).
[0069] The relevant technologies apply rewriting rules in a fixed order when rewriting the original query statement, which cannot be applied to all complex query statements, or may incorrectly apply rewriting rules that lead to a decrease in query performance, resulting in the inability to rewrite the optimal query statement.
[0070] 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. 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.
[0071] 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.
[0072] See Figure 1 , Figure 1 This is a schematic diagram of the architecture of the database query statement processing system 100 provided in the embodiments 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.
[0073] 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.
[0074] 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.
[0075] 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.
[0076] 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.
[0077] 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.
[0078] 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.
[0079] In some embodiments, server 200 may 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.
[0080] The electronic device that implements the database query statement processing method provided in the embodiments of this application may be Figure 1 Terminal 400 or server 200. See also Figure 2A , Figure 2A This is a schematic diagram of the structure of the electronic device 500-1 provided in the embodiments of this application. Figure 2A The illustrated electronic device 500-1 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 via a bus system 540-1. It is understood that the bus system 540-1 is used to implement communication between these components. In addition to a data bus, the bus system 540-1 also includes a power bus, a control bus, and a status signal bus. However, for clarity, in… Figure 2A The general designated all buses as Bus System 540-1.
[0081] 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.
[0082] User interface 530-1 includes one or more output devices 531-1 that enable the presentation of media content, including one or more speakers and / or 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.
[0083] 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.
[0084] 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.
[0085] 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.
[0086] 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;
[0087] 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).
[0088] 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;
[0089] 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.
[0090] In some embodiments, the apparatus provided in this application can be implemented in software. Figure 2A A processing device 555-1 for query statements of a database stored in memory 550-1 is shown. This device can be software in the form of programs or plug-ins, and includes 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 separated according to the functions implemented. The functions of each module will be described below.
[0091] This application 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... Figure 1 Terminal 400 or server 200. See also Figure 2B , Figure 2B This is a schematic diagram of the structure of the electronic device 500-2 provided in the embodiments of this application. Figure 2B The illustrated electronic device 500-2 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 via a bus system 540-2. It is understood that the bus system 540-2 is used to implement 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, in… Figure 2B The general designated all buses as Bus System 540-2.
[0092] 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.
[0093] User interface 530-2 includes one or more output devices 531-2 that enable the presentation of media content, including one or more speakers and / or 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.
[0094] 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.
[0095] 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.
[0096] 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.
[0097] In some embodiments, the database query statement processing apparatus provided in this application can be implemented in software. Figure 2B A processing device 555-2 for query statements of a database stored in memory 520-2 is shown. This device can be software in the form of programs and plug-ins, and includes the following software modules: an acquisition module 5551-2, a rewriting module 5552-2, and a determination module 5553-2. These modules are logically linked and can therefore be arbitrarily combined or further separated according to the functions they implement. The functions of each module will be described below.
[0098] 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.
[0099] 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.
[0100] 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.
[0101] See Figure 3A , Figure 3A This is a first flowchart illustrating the database query statement processing method provided in this application embodiment. Taking the terminal as the execution subject as an example, it will be combined with... Figure 3A The steps shown are explained.
[0102] In step 101, the original query statement and its statistical information are encoded to obtain the original statement vector.
[0103] 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.
[0104] In some embodiments, see Figure 3B , Figure 3B This is a second flowchart illustrating the database query statement processing method provided in the embodiments of this application. Figure 3A Step 101 can be done Figure 3B Steps 1011 to 1013 are implemented, and the details are explained below.
[0105] 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.
[0106] In some embodiments, the original query statement can be segmented into multiple words.
[0107] As an example, the original query is "SELECT name, age FROM users WHERE age>30 AND country='USA'". The original query can be divided into multiple words: "SELECT", "name", "age", "FROM", "users", "WHERE", "age", "">", "30", "AND", "country", "=", and "'USA'".
[0108] 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.
[0109] 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.
[0110] Here, the 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, index columns), histogram, index depth, and average row length of the table.
[0111] 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.
[0112] In step 1013, the first vector and the second vector are merged to obtain the original statement vector.
[0113] In some embodiments, see Figure 3C , Figure 3C This is a schematic diagram of the third process of the database query statement processing method provided in the embodiments of this application. Figure 3BStep 1013 can be achieved through Figure 3C The steps 10131 or 10132 are implemented as follows, and are explained in detail below.
[0114] In step 10131, the first vector and the second vector are concatenated to obtain the original statement vector.
[0115] 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].
[0116] 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.
[0117] 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.
[0118] In this embodiment, the original query statement and its statistical information are encoded respectively, and the first and second vectors obtained by encoding are fused to obtain the original statement vector. The fused original statement vector contains the semantic information of the query statement and the statistical information related to query performance, which makes the subsequent matching of candidate statement vectors of sample query statements from the case library more accurate.
[0119] See also Figure 3A The following will be an explanation following step 101 above.
[0120] 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.
[0121] In some embodiments, see Figure 3D , Figure 3D This is a schematic diagram of the fourth process of the database query statement processing method provided in the embodiments of this application. Figure 3A Step 102, "obtaining multiple candidate statement vectors that match the original statement vector from the case library," can be achieved through... Figure 3D Steps 1021 to 1022 are implemented, and the details are explained below.
[0122] 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.
[0123] 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.
[0124] 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|).
[0125] 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.
[0126] 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.
[0127] 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.
[0128] Based on the similarity between the original statement vector and the candidate statement vector, this application embodiment can automatically determine the candidate statement vector that matches the original statement vector of the original query statement from the case library. The similarity calculation can more accurately identify candidate statements that are semantically similar to the original query statement, thereby improving the accuracy of matching. The automated matching process can reduce manual intervention and improve work efficiency, especially when processing a large number of query statements.
[0129] In some embodiments, see Figure 3E , Figure 3E This is a fifth flowchart illustrating the database query statement processing method provided in this application embodiment. Figure 3A Step 102, "combining the candidate rewriting rules associated with each candidate statement vector in the case library into a first rewriting rule set," can be achieved through... Figure 3E Steps 1023 to 1024 are implemented, and the details are explained below.
[0130] In step 1023, the union of the candidate rewriting rules associated with each candidate statement vector in the case library is performed to obtain the filtered candidate rewriting rules.
[0131] 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.
[0132] 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.
[0133] In step 1024, the filtered candidate rewriting rules are combined into a first rewriting rule set.
[0134] As an example, rules 1, 2, 3, and 4 from the above example can be combined into a first set of rewrite rules.
[0135] This application embodiment obtains filtered candidate rewriting rules by performing a union operation on the candidate rewriting rules associated with multiple candidate statement vectors, and then combines them to obtain a first rewriting rule set. By performing a union operation on multiple candidate rewriting rules, it can be ensured that the obtained rewriting rule set covers more optimization strategies, thereby improving the diversity and comprehensiveness of the rewriting rules. It can also remove duplicate rewriting rules and retain effective rewriting rules, thereby improving the quality of the rewriting rules. At the same time, it can optimize the allocation of database resources, such as reducing input / output operations and reducing processor resource usage.
[0136] In some embodiments, see Figure 3F , Figure 3F This is a schematic diagram of the sixth step in the processing method of database query statements provided in this application embodiment. The above-mentioned case library can be accessed through... Figure 3F The following is a detailed explanation of the process, which is described in steps 201 to 206.
[0137] In step 201, the sample query statement is obtained.
[0138] In some embodiments, step 201 above can be obtained by: acquiring historical query statements from the database that meet the filtering conditions, and using them 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.
[0139] 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.
[0140] 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.
[0141] In this embodiment, historical query statements that meet preset performance index parameters are used as sample query statements. By filtering out query statements with long execution time, large difference between the number of rows scanned and the number of rows returned, many input / output times, and high processor or memory usage, the execution characteristics of these slow queries can be analyzed. In subsequent rewriting rule selection, rewriting rules that can optimize these slow queries can be selected in a targeted manner, thereby providing a basis for query optimization.
[0142] In step 202, multiple subsets of rewriting rules are extracted from the second set of rewriting rules.
[0143] In some embodiments, multiple subsets of rewriting rules may be randomly extracted from the second set of rewriting rules.
[0144] 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.
[0145] 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.
[0146] 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.
[0147] In some embodiments, see Figure 3G , Figure 3G This is a schematic diagram of the seventh step in the database query statement processing method provided in this application embodiment. For each subset of rewriting rules, Figure 3F Step 203 can be achieved through Figure 3G Steps 2031 to 2033 are implemented, and the details are explained below.
[0148] In step 2031, the sample query statement is rewritten based on the subset of rewriting rules to obtain the first rewritten sample query statement.
[0149] 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.
[0150] 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.
[0151] 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.
[0152] In some embodiments, the "obtaining the first performance indicator 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 so that the database executes the sample query statement; when the database finishes executing the sample query statement, calling a performance testing tool to obtain the first performance indicator parameters during the execution of the sample query statement by the database, wherein 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.
[0153] 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 so that the database executes the first rewritten sample query statement; 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.
[0154] Here, performance testing tools can be built into the database or independent of the database. Execution time refers to the total time spent from the start to the completion 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 central processing unit resources consumed during query execution, reflecting the degree of utilization of computing resources by the query. 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.
[0155] 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.
[0156] See also Figure 3F The following will be an explanation following step 203 above.
[0157] 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 are used as candidate rewriting rules for the sample query statement.
[0158] In some embodiments, see Figure 3H , Figure 3H This is the eighth flowchart of the database query statement processing method provided in the embodiments of this application. Figure 3F Step 204 can be achieved through Figure 3H Steps 2041 to 2044 are implemented, and the details are explained below.
[0159] 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.
[0160] 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.
[0161] 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.
[0162] 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 sample rewriting rule includes N sample rewriting rules.
[0163] 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, wherein each sample rewriting rule includes N sample rewriting rules.
[0164] 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.
[0165] 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 sample rewriting rule includes N sample rewriting rules.
[0166] 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 means of interval sampling to create N replica subsets of the effective rewriting rule subset, wherein each sample rewriting rule includes N sample rewriting rules.
[0167] 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.
[0168] This application's embodiments address 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. When segmenting and sampling rule subsets, the above scheme ensures that subset selection is random or based on a certain strategy, thus avoiding selection bias. 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.
[0169] 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.
[0170] 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.
[0171] 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.
[0172] 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).
[0173] In step 2043, the third performance metric parameter of the database is obtained when executing the second rewritten sample query statement.
[0174] In some embodiments, a second rewritten sample query statement is sent to the database so that the database executes the second rewritten sample query statement. When the execution is completed, a performance testing tool built into the database (or independent of the database) is invoked to obtain a third performance indicator parameter during the execution of the second rewritten sample query statement.
[0175] 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).
[0176] 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.
[0177] 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.
[0178] 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.
[0179] 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.
[0180] See also Figure 3F The following will be an explanation following step 204 above.
[0181] In step 205, the sample query statements and their statistical information are encoded to obtain a candidate statement vector.
[0182] 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.
[0183] In step 206, a case library is constructed based on the candidate statement vector and the candidate rewriting rules of the sample query statement.
[0184] 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>.
[0185] See also Figure 3A The following will be an explanation following step 102 above.
[0186] 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.
[0187] 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.
[0188] 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."
[0189] 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.
[0190] As examples, language models could be Baidu Language Model (BLM) or Alibaba Language Model (ALM).
[0191] In some embodiments, see Figure 3I , Figure 3I This is a schematic diagram of the ninth step in the database query statement processing method provided in this application embodiment. After step 104, the following steps can be executed: Figure 3I Steps 301 to 302 are explained in detail below.
[0192] 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.
[0193] 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.
[0194] 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.
[0195] 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.
[0196] 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.
[0197] In some embodiments, see Figure 3J , Figure 3J This is a schematic diagram of the tenth step of the database query statement processing method provided in this application embodiment. After step 104, the following can be executed: Figure 3J Steps 303 to 304 are explained in detail below.
[0198] 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.
[0199] 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.
[0200] 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.
[0201] 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.
[0202] In other embodiments, see Figure 3K , Figure 3K This is a schematic diagram of the eleventh step of the database query statement processing method provided in this application embodiment. After step 104, the following steps can also be executed: Figure 3K Steps 401 to 403 are explained in detail below.
[0203] 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.
[0204] 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 current target rewriting rule read to obtain the rewritten query statement, so as to verify the read target rule one by one.
[0205] 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.
[0206] 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.
[0207] 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.
[0208] In some embodiments, see Figure 3L , Figure 3L This is a schematic diagram of the twelfth step of the database query statement processing method provided in this application embodiment. Step 402 above can be achieved through... Figure 3L Steps 4021 to 4023 are implemented, and the details are explained below.
[0209] 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.
[0210] As an example, the original query statement is:
[0211] "SELECTc.customer_name,o.order_date,p.product_name
[0212] FROM customers c
[0213] JOIN orders o ON c.customer_id=o.customer_id
[0214] JOIN order_items oi ONo.order_id=oi.order_id
[0215] JOIN products p ON oi.product_id=p.product_id
[0216] WHERE c.customer_id=1001".
[0217] The first query plan tree of the original query statement is:
[0218]
[0219] The rewritten target query statement is:
[0220] "SELECTc.customer_name,o.order_date,p.product_name
[0221] FROM customers c
[0222] JOIN (
[0223] SELECT*FROM orders WHERE customer_id=1001
[0224] )o ON c.customer_id=o.customer_id
[0225] JOIN order_items oi ONo.order_id=oi.order_id
[0226] JOIN products p ON oi.product_id=p.product_id
[0227] WHERE c.customer_id=1001".
[0228] The second query plan tree for the target query statement is:
[0229]
[0230] Can we infer from the original query's join condition "c.customer_id = o.customer_id" and WHERE condition "c.customer_id = 1001" that `TableScan[orders as o]`, for this modified subtree, equivalence verification only requires checking the added filter condition "o.customer_id = 1001"?
[0231] In step 4022, the difference region between the fourth query plan tree and the third query plan tree is determined.
[0232] 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.
[0233] 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.
[0234] 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]", it is only necessary 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" of the original query statement, the query statements before and after the rewrite are equivalent, and the verification passes.
[0235] See also Figure 3K The following will be an explanation following step 402 above.
[0236] 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.
[0237] 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.
[0238] 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.
[0239] In summary, this embodiment of the application obtains multiple matching candidate statement vectors from the case library based on the original statement vector obtained by encoding the original query statement and statistical information. This can accurately identify candidate statement vectors in the case library that are similar to the original query statement, thereby reducing mismatches caused by semantic or expression differences. 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, the rewriting rules associated with the candidate statement vectors can be automatically obtained from the case library without manual intervention, which greatly improves the efficiency of rewriting rule generation. Based on the original query statement, statistical information, and the first rewriting rule set, prompt words are constructed to instruct the pre-trained language model to select the most suitable target rule sequence for rewriting the original query statement from the first rewriting rule set. 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, enhancing the flexibility and adaptability of the rewriting process, and thus automatically generating a high-quality target rule sequence adapted to the original query statement.
[0240] 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.
[0241] See Figure 4 , Figure 4 This is a flowchart illustrating the thirteenth step of the database query statement processing method provided in this application embodiment. Taking the terminal as the execution subject as an example, it will be combined with... Figure 4 Steps 501 to 504 are described below.
[0242] In step 501, the target rule sequence of the original query statement is obtained, wherein the target rule sequence includes multiple target rewriting rules.
[0243] 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.
[0244] 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.
[0245] 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 current target rewriting rule read to obtain the rewritten query statement, so as to verify the read target rule one by one.
[0246] 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.
[0247] 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.
[0248] 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 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.
[0249] 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 through the last target rewriting rule is used as the target query statement finally obtained by rewriting through the target rule sequence, to replace the original query statement.
[0250] 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.
[0251] 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.
[0252] The following will describe an exemplary application of the embodiments of this application in a real-world application scenario.
[0253] 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.
[0254] Rewriting query statements creates the possibility of 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 efficiency. The database cost-based optimizer (CBO), while powerful, has a huge search space and 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, memory usage, etc. Certain query structures can cause the optimizer's cost estimation to be severely inaccurate, failing to find 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.).
[0255] 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.
[0256] 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.
[0257] 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 can be used to predict the potential impact of applying specific rules, and MCTS can be 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 be used directly with LLMs to identify the best combination of rules in the optimizer that improves query performance and their application order.
[0258] 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.
[0259] 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.
[0260] 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 under TXSQL load.
[0261] The following describes the method for processing database query statements provided in the embodiments of this application. See also... Figure 5 , Figure 5 This is a schematic diagram illustrating the principle of query statement rewriting provided in the embodiments of this application. A detailed explanation will be given using a terminal as the execution subject as an example.
[0262] like Figure 5 As shown, 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 validate the rewritten query statement, and the rewritten query statement and validation results are output. The validation results indicate whether the semantics of the rewritten query statement are consistent with the original query statement.
[0263] 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.
[0264] 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.
[0265] 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."
[0266] As an example, the simplified rewrite of predicate logic rules is as follows:
[0267] The original query statement before rewriting was:
[0268]
[0269] 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.
[0270] Therefore, the rewritten query statement is:
[0271] SELECT order_id, order_date
[0272] FROM orders
[0273] WHERE order_amount>1000AND order_amount<5000".
[0274] 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."
[0275] As an example, the rewrite of the aggregation condition pull-up rule is as follows:
[0276] The original query statement before rewriting was:
[0277]
[0278] The statistics for the original query statement are as follows:
[0279] (1) Selectivity of GROUP BY order_id: 0.667
[0280] (2) Number of rows in the order_items table: 150,000
[0281] (3) Number of rows in the orders table: 100,000
[0282] The rewritten query statement is:
[0283] "SELECT o.customer_id,SUM(oi.quantity*oi.unit_price)as total_spending
[0284] FROM orders o
[0285] JOIN order_items oi ONo.order_id=oi.order_id
[0286] GROUP BY o.customer_id.
[0287] 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.
[0288] 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>.
[0289] 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.
[0290] 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.
[0291] 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.
[0292] As an example, the original query statement is:
[0293] "SELECTc.customer_name,o.order_date,p.product_name
[0294] FROM customers c
[0295] JOIN orders o ON c.customer_id=o.customer_id
[0296] JOIN order_items oi ONo.order_id=oi.order_id
[0297] JOIN products p ON oi.product_id=p.product_id
[0298] WHERE c.customer_id=1001".
[0299] The query plan tree for the original query statement is as follows:
[0300]
[0301] The rewritten query statement is:
[0302] "SELECTc.customer_name,o.order_date,p.product_name
[0303] FROM customers c
[0304] JOIN (
[0305] SELECT*FROM orders WHERE customer_id=1001
[0306] )o ON c.customer_id=o.customer_id
[0307] JOIN order_items oi ONo.order_id=oi.order_id
[0308] JOIN products p ON oi.product_id=p.product_id
[0309] WHERE c.customer_id=1001".
[0310] The query plan tree for the rewritten query statement is as follows:
[0311]
[0312]
[0313] 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.
[0314] 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.
[0315] The following description continues to illustrate the exemplary structure of the database query statement processing device 555-1 provided in the embodiments of this application as a software module. In some embodiments, such as... Figure 2A As shown, the software module in the database query statement processing device 555-1 stored in memory 550-1 may include:
[0316] The encoding module 5551-1 is used to encode the original query statement and its statistical information to obtain the original statement vector.
[0317] The combination module 5552-1 is used 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.
[0318] The construction module 5553-1 is used to construct prompt words based on the original query statement, statistical information and the first rewriting rule set. The prompt words are used to indicate that multiple target rewriting rules for optimizing the original query statement are selected from the first rewriting rule set in order of execution based on the statistical information.
[0319] The generation module 5554-1 is used to generate a target rule sequence based on prompt words by calling a pre-trained language model. The target rule sequence includes multiple target rewriting rules arranged in the order of execution.
[0320] 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.
[0321] In some embodiments, the combination module 5552-1 is further configured to 1023 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.
[0322] 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 metrics parameters of the sample query statement; determine sample rewriting rules from the subset of effective rewriting rules that improve the performance metrics parameters of the sample query statement, 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 of the sample query statement.
[0323] In some embodiments, the combination module 5552-1 is further configured to obtain historical query statements from the database 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 by 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.
[0324] 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.
[0325] In some embodiments, the combination module 5552-1 is further configured to send a sample query statement to the database so that the database executes the sample query statement; when the database finishes executing the sample query statement, it calls a performance testing tool to obtain a first performance indicator parameter during the period when the database executes the sample query statement, 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.
[0326] In some embodiments, the combination module 5552-1 is further configured to send the first rewritten sample query statement to the database so that the database executes the first rewritten sample query statement; when the database finishes executing the first rewritten sample query statement, it calls a performance testing tool 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.
[0327] In some embodiments, the combination module 5552-1 is further configured to create N replica subsets of the effective rewriting rule subset, 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 rewriting rule from the nth replica subset, rewrite the sample query statement based on the remaining sample rewriting 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 rewriting rule as a sample rewriting rule with an improvement effect.
[0328] In some embodiments, the combination module 5552-1 is further configured to: ...
[0329] 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 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 used as the target query statement finally obtained by rewriting the target rule sequence.
[0330] 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.
[0331] 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 verified, 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 target query statement finally obtained by rewriting through the target rule sequence.
[0332] 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 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, determine that the verification of the current target rewriting rule read has passed.
[0333] 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 designated 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.
[0334] 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.
[0335] 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.
[0336] The following description continues to illustrate the exemplary structure of the database query statement processing device 555-2 provided in the embodiments of this application as a software module. In some embodiments, such as Figure 2B As shown, the software module in the database query statement processing device 555-2 stored in memory 550-2 may include:
[0337] The acquisition module 5551-2 is used to acquire the target rule sequence of the original query statement, wherein the target rule sequence includes multiple target rewriting rules.
[0338] The rewriting module 5552-2 is used 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 read to obtain the rewritten query statement.
[0339] The determination module 5553-2 is used to determine that the current target rewriting rule has been verified as valid in response to the fact that the original query statement and the rewritten query statement are semantically identical, and to continue reading the next target rewriting rule of the current target rewriting rule; in response to the fact that multiple target rewriting rules have been verified as valid, the target rule sequence has been verified as valid, and the rewritten query statement obtained by rewriting through the last target rewriting rule is used as the target query statement finally obtained by rewriting through the target rule sequence, so as to replace the original query statement.
[0340] 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.
[0341] This application provides a computer-readable storage medium storing computer-executable instructions or a computer program. When the computer-executable instructions or the computer program are executed by a processor, the processor will execute the database query statement processing method provided in this application embodiment, for example, such as... Figure 3A This illustrates the method for processing database query statements.
[0342] 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.
[0343] 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.
[0344] 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).
[0345] 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.
[0346] In summary, this embodiment of the application obtains multiple matching candidate statement vectors from the case library based on the original statement vector obtained by encoding the original query statement and statistical information. This can accurately identify candidate statement vectors in the case library that are similar to the original query statement, thereby reducing mismatches caused by semantic or expression differences. 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, the rewriting rules associated with the candidate statement vectors can be automatically obtained from the case library without manual intervention, which greatly improves the efficiency of rewriting rule generation. Based on the original query statement, statistical information, and the first rewriting rule set, prompt words are constructed to instruct the pre-trained language model to select the most suitable target rule sequence for rewriting the original query statement from the first rewriting rule set. 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, enhancing the flexibility and adaptability of the rewriting process, and thus automatically generating a high-quality target rule sequence adapted to the original query statement.
[0347] 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 database query statements, characterized in that, The method includes: 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 according to claim 1, characterized in that, 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 according to claim 1, characterized in that, 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 claim 1, characterized in that, 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 them 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 according to claim 4, characterized in that, The query statement for obtaining samples includes: Obtain historical query statements from the database that meet the filtering criteria, and use them as sample query statements, wherein the filtering criteria 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 according to claim 4, characterized in that, 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 according to claim 6, characterized in that, 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 so that the database executes the sample query statement. 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 according to claim 4, characterized in that, 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 fact that the second performance metric parameter is better than the third performance metric parameter, the nth sample rewriting rule is selected as the sample rewriting rule with an improvement effect.
9. The method according to claim 8, characterized in that, 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 sample rewriting rule 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 sample rewriting rule includes N sample rewriting rules.
10. The method according to claim 1, characterized in that, 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.
11. The method according to claim 10, characterized in that, 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.
12. The method according to claim 1, characterized in that, 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.
13. The method according to claim 12, characterized in that, 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.
14. The method according to claim 13, characterized in that, 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.
15. The method according to claim 1, characterized in that, 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.
16. The method according to claim 15, characterized in that, 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.
17. A database query statement processing apparatus, characterized in that, The device includes: The encoding module is used to encode the original query statement and its statistical information to obtain the original statement vector. The combination module is used 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 used to construct prompt words based on the original query statement, the statistical information and the first rewriting rule set, wherein the prompt words are used to indicate that multiple target rewriting rules for optimizing the original query statement are selected from the first rewriting rule set in order of execution based on the statistical information. The generation module is used 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.
18. An electronic device, characterized in that, The electronic device includes: 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 16.
19. A computer-readable storage medium storing computer-executable instructions or a computer program, characterized in that, When the computer-executable instructions or computer program are executed by a processor, they implement the database query statement processing method according to any one of claims 1 to 16.
20. A computer program product comprising computer-executable instructions or a computer program, characterized in that, When the computer-executable instructions or computer program are executed by a processor, they implement the database query statement processing method according to any one of claims 1 to 16.
