Artificial intelligence-based database statement optimization method and system
By acquiring metadata information of database statements, verifying candidate solutions in a simulation environment using heuristic optimization rules and an expert calibration knowledge base, and combining structured reflection with a large language model, the security risks and performance fallback issues of slow SQL statement optimization in existing technologies are resolved, achieving efficient and stable optimization results.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- JIANGSU CHANGSHU RURAL COMMERICAL BANK CO LTD
- Filing Date
- 2026-02-26
- Publication Date
- 2026-06-09
AI Technical Summary
Existing technologies lack an end-to-end closed-loop mechanism when optimizing slow SQL statements, which poses security and performance fallback risks, makes it difficult to cope with data and business changes, has limited generalization ability, and results in poor interpretability of optimization suggestions.
By acquiring metadata information of the database statements to be optimized, incremental retrieval analysis is performed using heuristic optimization rules and an expert calibration knowledge base to generate candidate optimization schemes. The performance is then verified in a simulated database environment, and structured reflection is conducted in conjunction with a large language model to form an optimization system of coverage analysis-verification-reflection-iteration.
It significantly improves the accuracy and stability of database statement optimization, reduces the risk of performance regression, enhances the adaptability and interpretability of optimization strategies, and constructs an autonomously evolving optimization knowledge system.
Smart Images

Figure CN122173528A_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database technology, and more specifically, to a database statement optimization method and system based on artificial intelligence. Background Technology
[0002] In the database field, slow Structured Query Language (SQL) refers to SQL statements that take longer than expected and are inefficient. Slow SQL statements consume a large amount of database resources, causing system slowdowns, severely impacting the performance of business systems and user experience, and may even lead to serious problems such as system crashes. To address the slow SQL problem, existing technologies mainly rely on monitoring tools to locate slow SQL statements and then rewrite them or add indexes based on empirical rules or optimization algorithms to improve performance. However, existing technologies do not form an end-to-end closed loop in the process, and some solutions are directly verified and optimized in the production environment, which can easily lead to security risks and business interruptions. Furthermore, existing technologies lack a mechanism for continuous tracking and feedback of the optimization effects, making it difficult to cope with performance regressions caused by data and business changes. They also suffer from high response latency, limited generalization ability, insufficient understanding of business context, and poor interpretability of optimization suggestions.
[0003] Therefore, improving the accuracy and effectiveness of database statement optimization is an urgent problem to be solved. Summary of the Invention
[0004] In view of this, the purpose of this application is to provide a database statement optimization method and system based on artificial intelligence, so as to improve the accuracy and effectiveness of database statement optimization.
[0005] Firstly, this application provides a database statement optimization method based on artificial intelligence, including: Obtain the database statement to be optimized and the metadata information matching the execution scenario of the database statement to be optimized. The metadata information includes table structure information, index configuration information, runtime environment parameters, and database performance indicators. Based on the heuristic optimization rule set and expert calibration knowledge base, the statement optimization analysis model is invoked to perform progressive retrieval analysis on the database statement to be optimized and the metadata information, generating a set of candidate optimization schemes; The candidate optimization schemes included in the candidate optimization scheme set are executed in a simulation database environment with parameters consistent with the production environment to obtain the test results of the candidate optimization schemes. The test results include statement execution results and execution performance. Based on the execution performance and the statement execution result, a target optimization scheme that is semantically equivalent to the database statement to be optimized is selected from the candidate optimization scheme set. The execution performance of the target optimization scheme is greater than or equal to a preset performance threshold and the statement execution result is a valid result. The target optimization scheme is invoked to perform structured reflection on the optimization process data using a large language model to obtain reflection results, and the reflection results are updated to the expert calibration knowledge base to update and optimize the optimization process.
[0006] Secondly, this application provides an artificial intelligence-based database statement optimization system, which includes a machine-readable storage medium and a processor. The machine-readable storage medium stores machine-executable instructions, and when the processor executes the machine-executable instructions, the artificial intelligence-based database statement optimization system implements the aforementioned artificial intelligence-based database statement optimization method.
[0007] This application provides an AI-based database statement optimization method and system. It acquires metadata information matching the database statement to be optimized and its execution scenario, and uses a statement optimization analysis model based on a heuristic optimization rule set and an expert-calibrated knowledge base to generate a candidate optimization scheme set through progressive retrieval analysis. The performance of the candidate schemes is then verified in a simulated database environment, and semantically equivalent and performance-compliant target optimization schemes are selected. Finally, a large language model is used to perform structured reflection on the optimization process and update the expert knowledge base, thus constructing an end-to-end closed-loop optimization system covering "analysis-verification-reflection-iteration". Compared to existing technologies that rely solely on empirical rules or simple algorithms for single-step optimization and lack continuous tracking mechanisms, this solution avoids the security risks of direct testing in production environments through simulation environment verification. The structured reflection mechanism enables dynamic accumulation of optimization knowledge, and the combination of metadata information and heuristic rules improves the adaptability of optimization strategies to business scenarios. This significantly improves the accuracy, stability, and generalization ability of database statement optimization, effectively reduces the risk of performance regression, and forms a self-evolving optimization knowledge system. Attached Figure Description
[0008] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with this application and, together with the description, serve to explain the principles of this application.
[0009] Figure 1 A flowchart illustrating an artificial intelligence-based database statement optimization method provided in this application embodiment; Figure 2 This is a schematic diagram of the structure of an artificial intelligence-based database statement optimization system provided in an embodiment of this application.
[0010] The accompanying drawings illustrate specific embodiments of this application, which will be described in more detail below. These drawings and descriptions are not intended to limit the scope of the concept in any way, but rather to illustrate the concept of this application to those skilled in the art through reference to particular embodiments. Detailed Implementation
[0011] To enable those skilled in the art to better understand the present invention, the technical solutions of the present invention will be clearly and completely described below with reference to the accompanying drawings of the embodiments of the present invention. Obviously, the described embodiments are only some embodiments of the present invention, and not all embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of the present invention.
[0012] Figure 1 This is a flowchart illustrating an artificial intelligence-based database statement optimization method provided in an embodiment of this application. It should be understood that in other embodiments, the order of some steps in the artificial intelligence-based database statement optimization method of this embodiment can be shared according to actual needs, or some steps can be omitted or maintained. Figure 1 As shown, the method may include the following steps: Step S110: Obtain the database statement to be optimized and the metadata information matching the execution scenario of the database statement to be optimized. The metadata information includes table structure information, index configuration information, runtime environment parameters, and database performance indicators.
[0013] In this embodiment, the process of obtaining database statements to be optimized can be achieved by filtering the database execution logs using a database auditing tool. The filtering rules are based on indicators such as the execution frequency and execution time of the statements. When the execution frequency of a statement exceeds a preset frequency threshold or the execution time exceeds a preset time threshold, it is marked as a database statement to be optimized. For example, the database statements to be optimized may include slow SQL statements.
[0014] For metadata information matching the execution scenario of the database statement to be optimized, obtaining table structure information requires connecting to the database's system tables to query information such as table field names, data types, field lengths, whether they are primary keys, foreign keys, and whether null values are allowed. This information is stored in structured data format, such as a dictionary set with field names as keys and data types, lengths, and constraints as values. Index configuration information is obtained by querying the database's index system tables, including index name, table name, fields included in the index, index type (e.g., B-tree index, hash index), whether the index is unique, and the index creation time, also organized in structured data format.
[0015] Obtaining runtime environment parameters involves both the operating system level and the database level. At the operating system level, system commands are used to obtain information such as the number of CPU cores, total memory, disk space, and network bandwidth. At the database level, system functions or management views provided by the database are used to obtain information such as the database version number, number of connections, cache size, and maximum number of concurrent transactions.
[0016] Database performance metrics are collected in real time through database performance monitoring tools, including query executions per second, average query response time, lock wait time, logical reads, physical reads, CPU utilization, and memory utilization. These metrics are stored in time series to analyze performance trends.
[0017] During the data acquisition process, data involving user privacy, such as sensitive user information, needs to be anonymized. For example, fields like user ID numbers and mobile phone numbers may require partial character replacement or encryption to ensure that user privacy is not leaked during data use. Metadata collection is conducted in real-time to ensure that the acquired information perfectly matches the execution scenario of the database statements to be optimized.
[0018] Step S120: Based on the heuristic optimization rule set and expert calibration knowledge base, call the statement optimization analysis model to perform progressive retrieval analysis on the database statement to be optimized and the metadata information to generate a set of candidate optimization schemes.
[0019] In this embodiment, the heuristic optimization rule set is a rule base containing multiple optimization rules. Each rule consists of two parts: rule conditions and rule actions. The rule conditions describe the characteristics of the database statement and metadata information. The heuristic optimization rule set includes rules for avoiding full table scans, rules for making reasonable use of indexes, rules for optimizing JOIN queries, rules for reducing nested subqueries, rules for optimizing the use of aggregate functions, and rules for avoiding invalid sorting.
[0020] The expert calibration knowledge base stores historical optimization cases and expert experience. Each case includes the original statement, the optimized statement, a performance comparison before and after optimization, and the rules and experience upon which the optimization was based. The statement optimization analysis model is a comprehensive analysis model integrating a rule engine and a machine learning model. The rule engine is used to match heuristic optimization rules, while the machine learning model is used to learn optimization experience from the expert calibration knowledge base and make predictions. When retrieving data from the expert calibration knowledge base, a progressive intelligent retrieval enhancement algorithm based on Markov chains can be used.
[0021] Step S121: Call the reinforcement learning scheduling module built into the statement optimization analysis model to complete the initialization operation, predefine the state space covering the statement feature dimension and metadata dimension, and the action space covering the adjustment operation of the heuristic optimization rule set and the historical optimization operation of the expert calibration knowledge base.
[0022] In this embodiment, the initialization operation of the reinforcement learning scheduling module includes setting the initial parameters of the agent, such as the exploration rate (used to control the probability of the agent exploring new actions), the learning rate (used to control the magnitude of parameter updates), and the discount factor (used to weigh the importance of current rewards and future rewards).
[0023] The predefined state space requires vectorizing statement characteristics and metadata information. Statement characteristic dimensions include the operation type (e.g., query, insert, update, delete), the number of tables involved, the number of fields involved, the number and type of JOIN operations, the number and type of conditions in the WHERE clause, the number of fields in the GROUP BY clause, the number of fields and sorting method in the ORDER BY clause, and the number of rows in the LIMIT clause, etc. Metadata dimensions include the number of records in each table, the data type and length of each field, the type and included fields of each index, the number of CPU cores in the database server, memory size, disk I / O performance, etc. Each dimension in the state space has a specific range of values; for example, the statement operation type can be represented by 0-3 to indicate query, insert, update, and delete respectively, and the number of tables can be represented by integers, etc.
[0024] For example, the predefined action space encodes the adjustment operations in the heuristic optimization rule set and the historical optimization operations in the expert calibration knowledge base, with each action corresponding to a unique action identifier. The state space S is defined as follows: the state s_t at any time step is the set of all subqueries and answers generated by the statement optimization analysis model (such as a large language model) from the initial time to the current time t, i.e., s_t = ∪(t = 0 to T_max) {(q_0, a_0), (q_1, a_1), ..., (q_t, a_t)}, where q_i is a subquery, a_i is the answer generated by the statement optimization analysis model, and T_max is the maximum allowed number of iterations. The initial state s_0 = (q, φ), where q is the initial query and φ represents an empty answer.
[0025] Define an action space A, where the set of possible actions under any state s_t is A = {generate termination state action, immediate response action, retrieve knowledge from the knowledge base to generate a response action}. The deterministic state transition function is φ(s_t, a_t) = s_{t+1}. For any state s_t = ∪(t=0 to T_max){(q_0, a_0), (q_1, a_1), ..., (q_t, a_t)}, the action selected by the statement optimization analysis model is a_t ∈ A. When a_t = generate termination state action, the statement optimization analysis model generates the termination state o = (q_t, a_final) based on the current query content q_t, and then updates the state space s_t = ∪(t=0 to T_max){(q_0, a_0), (q_1, a_1), ..., (q_t, a_t), o}.
[0026] When a_t = immediate response action, it indicates that the statement optimization analysis model does not rely on the expert calibration knowledge base and generates the response directly through its own knowledge. Otherwise, the statement optimization analysis model will retrieve knowledge from the expert calibration knowledge base to generate the response. Under non-terminating state actions, the state space update process is s_t = ∪(t = 0 to T_max) {(q_0, a_0), (q_1, a_1), ..., (q_t, a_t), (q_{t+1}, a_{t+1})}. In this process, at any time step, the update of the next state s_{t+1} depends on the current state s_t and the action a_t currently selected by the statement optimization analysis model, without needing earlier history, satisfying the Markov property.
[0027] Step S122: Map the database statement to be optimized, the table structure information included in the metadata information, the index configuration information, the runtime environment parameters, and the database performance indicators to the state space to generate a unique corresponding current state node.
[0028] In this embodiment, the database statement to be optimized is first parsed to extract its statement features. For example, the statement is decomposed into various syntactic units by a syntax parser to identify the operation type, the tables involved, fields, JOIN conditions, WHERE conditions, etc. Then, these features are converted into values of the corresponding dimensions in the state space.
[0029] For table structure information in metadata, the field information and constraint information of each table need to be converted into corresponding dimension values in the state space. For example, the number of records in a table can be directly used as the value of the table record count dimension in the state space, and the data type of the field can be converted into the corresponding numerical value according to the preset encoding rules. Index configuration information also needs to be encoded. For example, the index type can be represented by different numerical values, and the fields included in the index can be represented by the index of the field's position in the table.
[0030] Operating environment parameters and database performance metrics are directly used as values for the corresponding dimensions in the state space. If some metrics have different units of measurement, standardization is required. For example, CPU utilization (percentage) and memory utilization (percentage) are directly used as values, while disk I / O performance (such as MB / s) can be normalized by dividing by a preset maximum I / O performance value, making its value range between 0 and 1. After mapping all features to the state space, a high-dimensional state vector is obtained, which represents the current state node.
[0031] Step S1221: Extract the structural features and semantic features of the database statement to be optimized, as well as the attribute features corresponding to the table structure information, index configuration information, runtime environment parameters, and database performance indicators included in the metadata information.
[0032] In this embodiment, the structural features of the database statement to be optimized are extracted through a parser. The parser performs lexical and syntactic analysis on the statement to generate an abstract syntax tree (AST). From the AST, structural features such as operators, operands, and expression structures can be extracted, such as the list of projected fields in the SELECT statement, the list of table names in the FROM clause, the join conditions in the JOIN clause, and the filtering condition structure in the WHERE clause.
[0033] Semantic feature extraction requires combining the database's metadata information to analyze the semantic intent of the statement, such as determining whether the statement is used to query, update, or insert data, whether it queries single-table data or multi-table join data, and whether it involves aggregation operations, etc.
[0034] The attribute characteristics of table structure information include table name, field name, field data type, field length, field constraints (primary key, foreign key, NOT NULL, etc.), and table storage engine; the attribute characteristics of index configuration information include index name, table name to which the index belongs, fields included in the index, index type, index uniqueness, and index cardinality; the attribute characteristics of runtime environment parameters include CPU model, number of CPU cores, memory capacity, disk type (HDD / SSD), and network bandwidth; the attribute characteristics of database performance indicators include the current database's QPS (queries per second), TPS (transactions per second), average query response time, lock wait count, and cache hit rate.
[0035] Step S1222: Match the structural features, semantic features, and attribute features with the predefined dimensions of the state space one by one to determine the state dimension mapping relationship corresponding to each feature.
[0036] In this embodiment, the predefined dimensions of the state space have been determined in step S121, and each dimension has a clear meaning and value range.
[0037] During matching, a corresponding state dimension needs to be found for each extracted feature. For example, the operation type feature of the database statement to be optimized corresponds to the "statement operation type" dimension in the state space; the number of tables involved corresponds to the "number of tables" dimension; the number of table records in the table structure information corresponds to the "number of table records" dimension; the index type feature in the index configuration information corresponds to the "index type" dimension; the number of CPU cores in the runtime environment parameters corresponds to the "number of CPU cores" dimension; and the average query response time feature in the database performance metrics corresponds to the "average query response time" dimension, etc. For some complex features, it may be necessary to map them to multiple state dimensions. For example, the feature of JOIN operations needs to be mapped to both the "number of JOINs" dimension and the "JOIN type" dimension. Through this one-to-one matching method, the correspondence between features and state dimensions is established.
[0038] Step S1223: According to the standardized dimensional requirements of the state space, perform dimensional alignment processing on all features to generate a set of dimensional features that conforms to the state space dimensional specification.
[0039] In this embodiment, the standardized dimensions of the state space include the value range and data type of each dimension. During dimension alignment, for numerical features, if their value range does not match the requirements of the corresponding dimension in the state space, normalization or standardization is required. For example, the number of table records may be very large, and the value range of the "number of table records" dimension in the state space is 0 to 1. In this case, the number of table records can be divided by the maximum number of table records in the database to obtain the normalized value. For categorical features, one-hot encoding or label encoding is required. For example, for the "index type" dimension, a B-tree index can be encoded as 0, a hash index as 1, and a full-text index as 2, etc.
[0040] For text-based features, such as field names and table names, they need to be converted into numerical representations. This can be done by mapping the text to fixed-length numerical vectors using a hash function, or by converting the text into word vectors using a pre-trained word vector model. After dimension alignment, all features are converted into numerical values conforming to the state space dimension specification, forming a dimensional feature set.
[0041] Step S1224: Input the dimensional feature set into the feature encoding module built into the state space to complete the feature encoding operation and generate a state encoding vector with a unified dimension.
[0042] In this embodiment, the feature encoding module is implemented using a deep learning model, such as a multilayer perceptron (MLP) or a convolutional neural network (CNN). The dimensional feature set serves as the input to the feature encoding module, and after forward propagation computation, it outputs a fixed-length state encoding vector.
[0043] The function of the feature encoding module is to reduce the dimensionality of a high-dimensional feature set or fuse features to extract the key information that best represents the current state. For example, if the input feature set has 100 dimensions, after processing by the feature encoding module, it will output a 50-dimensional state encoding vector.
[0044] When training the feature encoding module, historical state data and corresponding optimization results are used as training samples. The model parameters are adjusted through the backpropagation algorithm so that the encoded state vector can accurately reflect the features of the state, and the encoded vectors corresponding to different states have good discriminative power.
[0045] Step S1225: Verify the dimensionality fit between the state encoding vector and the state space. If there is a missing dimension, supplement the default feature encoding of the corresponding dimension.
[0046] In this embodiment, the state space has a fixed number of dimensions, and the dimension of the state encoding vector must be consistent with the number of dimensions of the state space.
[0047] During the verification process, the first step is to check whether the number of dimensions of the state encoding vector is equal to the preset number of dimensions of the state space. If they are not equal, it indicates that there is a missing dimension or a redundant dimension.
[0048] For cases where dimensions are missing, default feature encodings need to be added based on the type of missing dimension. For example, if the "index type" dimension is missing, the default encoding is 0 (indicating no index); if the "CPU core count" dimension is missing, the default encoding is the normalized value corresponding to the average number of CPU cores in the system.
[0049] For cases of dimensional redundancy, the redundant dimensions need to be removed, retaining only the encoded vector portion corresponding to the state space dimensions. Through checksum and supplementation operations, it is ensured that the state encoded vector conforms to the dimensional requirements of the state space.
[0050] Step S1226: Mark the verified state encoding vector as the current state node and associate it with the identifier of the database statement to be optimized.
[0051] In this embodiment, the identifier of the database statement to be optimized is a unique string used to distinguish different statements. For example, it could be a hash value of the statement or a unique ID of the statement in the database. Associating the state encoding vector with the identifier of the database statement to be optimized facilitates finding the corresponding state node during the optimization process. It also facilitates mapping the generated candidate optimization schemes to the original statements. This association can be implemented by establishing a mapping table, where the key is the identifier of the database statement to be optimized, and the value is the corresponding state encoding vector.
[0052] Step S123: Based on the current state node, match the corresponding heuristic optimization rule adjustment operation in the action space, call the matched adjustment operation to perform fragmented adjustment on the database statement to be optimized, and generate an initial set of adjusted statement fragments.
[0053] In this embodiment, the current state node is a state encoding vector, and the action space is a set containing all possible optimization operations.
[0054] Specifically, the matching process can use a similarity calculation method to calculate the similarity between the current state node and the state features corresponding to the triggering conditions of each action in the action space. When the similarity is greater than a preset similarity threshold, the action is considered to match the current state node.
[0055] The status characteristics corresponding to the triggering conditions are predefined. For example, if the triggering condition for a certain heuristic optimization rule adjustment operation is "the number of table records is greater than 10,000 and the field that is frequently filtered in the query has no index", then the status characteristics corresponding to this triggering condition include the value of the "number of table records" dimension being greater than 10,000, and the index status of the corresponding field in the "index configuration" dimension being no index.
[0056] A match is determined by calculating the similarity between the state encoding vector of the current state node and the vector of the triggering condition state feature. Multiple adjustment operations may be generated from the match. These operations are ordered according to a preset priority and then applied sequentially to the corresponding segments of the database statement. For example, adjustment operations might include rewriting conditions in the WHERE clause, adjusting the order of JOIN tables, or adding an index to a field. Each adjustment operation modifies a specific segment of the statement, thus generating multiple initially adjusted statement segments.
[0057] Step S1231: Analyze all the dimensional features contained in the current state node and extract the action trigger condition identifiers corresponding to each dimensional feature.
[0058] In this embodiment, the dimensional features of the current state node include various aspects of statement features and metadata information.
[0059] Specifically, the parsing process involves restoring the state encoding vector to its original feature values for each dimension. Then, based on the meaning of each dimension's feature, it determines the possible action trigger condition identifiers. For example, if the feature value of the "number of table records" dimension is greater than a preset threshold, it might correspond to the action trigger condition identifier of "creating an index for a large table is needed"; if the feature value of the "number of WHERE clause conditions" dimension is high, it might correspond to the action trigger condition identifier of "optimizing the order of WHERE clause conditions." The action trigger condition identifier is a unique identifier used to associate a specific action in the action space. By parsing the dimensional features of the current state node, all possible action trigger condition identifiers are extracted, providing a basis for subsequent action matching.
[0060] Step S1232: Traverse the action entries stored in the action space that correspond to the set of heuristic optimization rules, and match the triggering conditions corresponding to the action entries with the action triggering condition identifiers to obtain an initial set of matched actions.
[0061] In this embodiment, the action entries stored in the action space include information such as action identifiers, trigger conditions, and action descriptions. Each action entry in the action space is traversed, and its trigger condition is compared with the action trigger condition identifier extracted from the current state node. If the trigger condition of an action entry completely matches an action trigger condition identifier, or satisfies the logical relationship in the trigger condition (such as an "AND" or "OR" relationship), then the action entry is added to the initial matching action set. For example, if the trigger condition of an action entry is "action trigger condition identifier A and action trigger condition identifier B," and if both action trigger condition identifiers A and B are extracted from the current state node, then the action entry is added to the initial matching action set. The initial matching action set may contain multiple action entries, which are initially selected optimization operations related to the current state.
[0062] Step S1233: Perform an applicability check on each action entry in the initial matching action set, remove action entries that conflict with the metadata-related dimension features of the current state node, and obtain a valid matching action set.
[0063] In this embodiment, the scope of application validation checks whether an action entry is applicable to the current metadata environment. For example, an action entry might be "create a hash index for field A," but if the current database's storage engine does not support hash indexes, or the data type of field A is unsuitable for creating a hash index (such as long text), then this action entry conflicts with the metadata-related dimension features and needs to be removed. During the validation process, the applicability conditions of the action entry need to be compared with the metadata dimension features of the current state node, such as database type, storage engine, and field data type. Only action entries that fully meet the applicability conditions can be retained in the set of valid matching actions.
[0064] Step S1234: Sort the action items in the effective matching action set according to the preset action priority sorting rules of the action space to obtain the action execution sequence.
[0065] In this embodiment, the action priority ranking rule is preset based on factors such as optimization effect, execution cost, and risk level. For example, the "create index" operation may have a higher priority because it can usually significantly improve query performance; while the "rewrite query statement structure" operation may have a lower priority because its execution cost and risk are relatively high.
[0066] Action priority ranking rules can employ a weighted scoring method. Multiple evaluation metrics (such as expected performance improvement, implementation difficulty, and resource consumption) are set for each action item, with each metric having a corresponding weight. Action items are ranked by calculating a weighted score. Action items with higher scores have higher priority and are placed at the top of the action execution sequence.
[0067] Step S1235: Perform the corresponding adjustment operations sequentially according to the action execution sequence to adjust the corresponding segments of the database statement to be optimized, and generate multiple adjusted statement segments.
[0068] In this embodiment, each action item corresponds to a specific adjustment operation. For example, the adjustment operation corresponding to "create a B-tree index for field A" is to execute the CREATEINDEX statement in the database; the adjustment operation corresponding to "adjust the order of JOIN tables" is to modify the order of tables in the FROM clause.
[0069] These adjustment operations are executed sequentially according to the action execution sequence, with each operation modifying a specific segment of the database statement to be optimized. For example, the operation "create an index for the user_level field" is executed first, modifying the database's index configuration; then the operation "adjust the JOIN table order" is executed, modifying the FROM clause in the statement; followed by the operation "optimize the WHERE clause conditions," modifying the conditional expressions in the WHERE clause, and so on. Each adjustment operation generates a new statement segment, so after executing all the action items, multiple adjusted statement segments are obtained.
[0070] Step S1236: Perform grammatical format verification on the adjusted statement fragments, remove statement fragments that do not meet the grammatical requirements, and obtain the initial set of adjusted statement fragments.
[0071] In this embodiment, syntax verification uses the database's syntax parser to perform syntax checks on the adjusted statement fragments. The syntax parser can identify syntax errors in the statements, such as missing keywords, mismatched parentheses, and incorrect field names. Statement fragments with syntax errors are removed from the adjusted statement fragment set. For example, if a missing field name appears in an adjusted statement fragment, the syntax parser will report an error, and the fragment will be removed. After syntax verification, the remaining statement fragments form the initial adjusted statement fragment set.
[0072] Step S124: Map the features of the initially adjusted set of sentence fragments to the state space, and complete the state iteration update to obtain the iterative state node.
[0073] In this embodiment, each statement fragment in the initially adjusted set of statement fragments has new features, which need to be remapped into the state space. The mapping process is similar to step S122. First, the structural features, semantic features, and attribute features of the corresponding metadata information of each statement fragment are extracted. Then, these features are matched with the predefined dimensions of the state space for dimension alignment. The input is then fed into the feature encoding module to generate a state encoding vector. After verifying the dimension adaptability, iterative state nodes are obtained. Each statement fragment corresponds to one iterative state node, resulting in multiple iterative state nodes.
[0074] Step S125: Based on the iterative state node, match the corresponding expert calibration knowledge base historical optimization operation in the action space, call the matched historical optimization operation to perform a secondary adjustment on the initially adjusted set of sentence fragments, and generate an optimized set of sentence fragments.
[0075] In this embodiment, the historical optimization operations in the expert calibration knowledge base are empirical operations summarized from past optimization cases. The matching process is similar to step S123, calculating the similarity between the iteration state node and the trigger condition state features of the historical optimization operations in the expert calibration knowledge base, and matching historical optimization operations suitable for the current iteration state.
[0076] For example, a historical optimization operation might be triggered by the condition that "in an order query scenario, when user level filtering is involved and the user table has a large amount of data, the JOIN order of the user table and the order table is adjusted so that the user table comes first." If the characteristics of the current iteration state node meet this trigger condition, the historical optimization operation is invoked to perform a secondary adjustment on the initially adjusted statement fragments. This secondary adjustment might include further optimizing the statement structure, adjusting index usage strategies, etc., generating a set of optimized statement fragments.
[0077] Step S126: Concatenate the optimized statement fragment set according to the original logical order of the database statements to be optimized to generate multiple sets of optimized statements to be verified.
[0078] In this embodiment, the original logical order of the database statement to be optimized includes the order of each clause (such as SELECT, FROM, JOIN, WHERE, GROUPBY, ORDERBY, LIMIT, etc.) and the relationships between tables. Each fragment in the optimized statement fragment set corresponds to a part of the original statement. These fragments need to be concatenated according to the original logical order to form a complete statement to be validated and optimized. For example, the structure of the original statement is... "SELECT...FROM...JOIN...WHERE...ORDERBY...LIMIT..." The optimized statement fragment set includes SELECT clause fragments, FROM and JOIN clause fragments, WHERE clause fragments, ORDER BY clause fragments, LIMIT clause fragments, etc. These fragments are concatenated in their original order to obtain the complete optimized statement to be validated. Since the optimized statement fragment set may contain multiple different combinations of fragments, multiple sets of optimized statements to be validated can be generated.
[0079] Step S127: Perform syntax compliance verification, semantic consistency verification, and deduplication on the multiple sets of statements to be verified and optimized to generate the candidate optimization scheme set.
[0080] In this embodiment, the syntax compliance check is similar to step S1236, using a syntax parser to check for syntax errors in the statement to be checked and optimized. Semantic consistency check ensures that the statement to be checked and optimized is semantically consistent with the database statement to be optimized, meaning the query results should be the same.
[0081] You can check whether the projection fields, filtering conditions, aggregate functions, sorting methods, etc. are consistent by comparing the abstract syntax tree (AST) structures of the statement to be verified and the database statement to be optimized, or by executing the two statements in the test environment and comparing whether their return results are the same.
[0082] Deduplication involves removing duplicate statements to be validated and optimized. For example, different adjustment operations may generate the same optimized statement, and these duplicate statements need to be removed. After these three processes, and by combining the analysis of slow SQL statements with multiple large language models in multiple statement optimization analysis models, the remaining statements to be validated and optimized form a set of candidate optimization schemes.
[0083] Step S130: Execute the candidate optimization schemes included in the candidate optimization scheme set in a simulation database environment with parameters consistent with the production environment, and obtain the test results of the candidate optimization schemes. The test results include statement execution results and execution performance.
[0084] In this embodiment, the simulated database environment is a database environment built using virtualization technology that is completely consistent with the production environment. It includes hardware configuration (CPU, memory, disk, etc.), software configuration (database version, operating system version, etc.), data volume (the same as the data volume in the production environment or scaled proportionally), index configuration, parameter settings, etc.
[0085] When setting up a simulation database environment, it is necessary to back up and restore data from the production environment to the simulation environment to ensure data consistency. For cases involving privacy-sensitive data, data masking is required during backup and recovery. This can be done by using data masking tools to replace, encrypt, or mask sensitive fields, ensuring that the data in the simulation environment does not contain any real privacy information.
[0086] When executing candidate optimization schemes, each scheme is run in the simulation environment according to a preset execution order and concurrency level. The execution results (such as the returned dataset, whether execution was successful, etc.) and performance metrics (such as execution time, logical read counts, physical read counts, CPU usage time, memory usage, etc.) are recorded. Performance metrics are collected using the database's built-in performance monitoring tools or third-party monitoring tools to ensure data accuracy and completeness.
[0087] All optimization schemes are executed and tested in the simulation library, and the test results are obtained, including SQL execution results, execution time, execution plan, etc.
[0088] Step S140: Based on the execution performance and the statement execution result, select a target optimization scheme that is semantically equivalent to the database statement to be optimized from the candidate optimization scheme set. The execution performance of the target optimization scheme is greater than or equal to a preset performance threshold and the statement execution result is a valid result.
[0089] In this embodiment, semantic equivalence means that the execution result of the statement obtained after the candidate optimization scheme is executed is completely consistent with the execution result of the statement in the database to be optimized, including the number of records returned and the field values of each record.
[0090] Execution performance greater than or equal to the preset performance threshold means that the execution performance indicators (such as execution time, number of logical reads, etc.) of the candidate optimization scheme are better than or equal to the preset threshold, such as execution time not exceeding 500 milliseconds, number of logical reads not exceeding 1000 times, etc.
[0091] The screening process first determines the validity of the execution results of candidate optimization schemes, excluding schemes that fail or produce empty results. Then, it checks the semantic equivalence of valid schemes, retaining those that are semantically equivalent to the statement to be optimized. Finally, it evaluates the execution performance of semantically equivalent schemes, selecting those whose execution performance reaches a preset threshold as the target optimization scheme. A self-consistent algorithm then determines the optimal scheme from multiple candidate schemes.
[0092] Step S141: Based on the execution result of the statement, remove the abnormal optimization schemes that failed to execute from the candidate optimization scheme set to obtain a set of effective candidate optimization schemes.
[0093] In this embodiment, the validity judgment of the statement execution result includes checking whether errors occur during the execution process (such as syntax errors, permission errors, table non-existence, etc.) and whether the result returned after execution meets expectations (such as the returned result is not empty, the data format is correct, etc.).
[0094] Candidate optimization schemes that fail to execute, such as those that throw exceptions or return error codes during execution, are removed from the candidate optimization scheme set. After removal, the remaining candidate optimization schemes form the valid candidate optimization scheme set.
[0095] For example, let's define the database statement to be optimized as q_0, and the SQL that needs to be tested in the simulation library for the N optimization schemes given by the statement optimization analysis model as Q={q_1, q_2, ..., q_N}, and the execution function Exec(q, D)=r∈R∪{ }, where D represents the database status, r represents the query results, and R represents the set of all valid execution results. For all invalid results, the set of valid SQL rewrites is Q_valid={q_i∈Q|Exec(q_i,D)≠}
[0096] Step S142: Map the statement execution results of the effective candidate optimization schemes included in the effective candidate optimization scheme set and the statement execution results of the database statement to be optimized to the standardized result space to obtain the standardized results of the effective candidate optimization schemes and the standardized results of the database statement to be optimized.
[0097] In this embodiment, the standardized result space is a unified result representation format used to eliminate differences in format, order, and other aspects of the execution results of different statements. The mapping process includes operations such as sorting the execution results (according to a preset field order and sorting method), deduplication (removing duplicate records), and field standardization (unifying field names and data types).
[0098] For example, the execution results of the database statement to be optimized and the execution results of valid candidate optimization schemes may differ in the order of records. This order difference can be eliminated by sorting the results in ascending order according to the primary key field. For numeric fields, two decimal places are uniformly retained to ensure consistent data precision. After mapping, standardized results are obtained, facilitating subsequent consistency comparisons. The corresponding standardized result set is R_norm={s_i∈φ(Exec(q_i,D))|q_i∈Q_valid}, where φ is the standardization function of the SQL execution results. All SQL execution results belong to the standardized result space S. If r_1 and r_2 are semantically equivalent, then φ(r_1)=φ(r_2).
[0099] Step S143: Based on the standardization results of the effective candidate optimization schemes, perform consistency grouping on the effective candidate optimization schemes included in the set of effective candidate optimization schemes to obtain semantic grouping results, wherein the effective candidate optimization schemes included in the same semantic grouping results have the same standardization results.
[0100] In this embodiment, consistency grouping involves grouping valid candidate optimization schemes with the same standardization results into one group. The grouping process is achieved by comparing the standardization results of each valid candidate optimization scheme, and grouping schemes with completely identical standardization results into the same group.
[0101] For example, if three valid candidate optimization schemes all have standardized results for datasets containing 10 records, and each record has the same field value, then these three schemes are grouped into the same semantic grouping result. The semantic grouping result can be represented by a unique group identifier, and each group identifier corresponds to a group of valid candidate optimization schemes with the same standardized results. For any standardized result s∈S, its corresponding consistency group is C(s)={q_i∈Q_valid|φ(Exec(q_i,D))=s}.
[0102] Step S144: Based on the standardization results of the database statement to be optimized and the standardization results corresponding to each semantic grouping result, determine the candidate semantic grouping results that are semantically equivalent to the database statement to be optimized.
[0103] In this embodiment, the standardized result of the database statement to be optimized is used as a benchmark result, and the standardized results corresponding to each semantic grouping result are compared with this benchmark result. If the standardized result of a certain semantic grouping result is exactly the same as the standardized result of the database statement to be optimized, then the semantic grouping result is determined as a candidate semantic grouping result that is semantically equivalent to the database statement to be optimized.
[0104] For example, if the normalized result of the database statement to be optimized is dataset A, and the normalized result of a certain semantic grouping is also dataset A, then the semantic grouping result is a candidate semantic grouping result.
[0105] Step S145: Calculate the consistency score of the candidate semantic grouping results, and select target semantic grouping results from the candidate semantic grouping results whose consistency scores are greater than or equal to a preset score threshold based on the consistency scores.
[0106] In this embodiment, the consistency score is an indicator that measures the semantic equivalence between the candidate semantic grouping results and the database statements to be optimized. Calculating the consistency score can consider multiple factors, such as the matching degree of record count, the matching degree of field values, and the consistency of data types.
[0107] For example, the maximum consistency score can be κ=max(s∈S)(|C(s)| / |Q_valid|), where | | represents the number of elements in the set. The execution result set of the scheme group with the highest consistency score is s*=argmax(s∈S)(|C(s)| / |Q_valid|). The consistency scores of the candidate semantic grouping results are compared with a preset score threshold. Candidate semantic grouping results with scores greater than or equal to the threshold are selected as the target semantic grouping results.
[0108] Step S146: Determine the target optimization scheme based on each effective candidate optimization scheme in the target semantic grouping result, the statement execution result of each effective candidate optimization scheme, the execution performance, and the preset judgment rules. The preset judgment rules include the preset performance threshold and security guarantee rules. The security guarantee rules include at least one of the following: system stability rules, data consistency rules, statement legality rules, and resource consumption rationality rules.
[0109] In this embodiment, the target semantic grouping results contain multiple valid candidate optimization schemes, and the optimal scheme needs to be selected as the target optimization scheme according to the preset judgment rules.
[0110] The preset performance thresholds include execution time threshold, logical read count threshold, and CPU utilization threshold. Only solutions with execution performance metrics that are better than or equal to these thresholds can be selected.
[0111] The system stability rule in the security assurance rules requires that the optimization plan will not cause the database system to crash, deadlock or other unstable situations during execution; the data consistency rule requires that no data will be lost or corrupted before and after the optimization plan is executed; the statement legality rule requires that the statements in the optimization plan comply with the database syntax and permission requirements; and the resource consumption rationality rule requires that the execution of the optimization plan will not excessively consume CPU, memory, disk I / O and other resources.
[0112] Taking these factors into account, the effective candidate optimization schemes in the target semantic grouping results are scored, and the scheme with the highest score is determined as the target optimization scheme. For example, the execution time function of each scheme in the simulation library is T(q, D)∈R, and the SQL rewrite result corresponding to the determined optimal scheme is q*=argmin(q∈C(s*))T(q, D).
[0113] Step S147: Confirm the final optimization scheme: If the execution result of the determined optimal scheme in the above simulation database environment meets the preset standard or reaches the maximum number of iterations, it is confirmed as the final optimization scheme.
[0114] In this embodiment, the maximum number of iterations can be configured according to actual business needs. When the maximum number of iterations is reached, if the determined optimal solution still does not meet the preset standard, the solution will still be selected as the final solution for this optimization, and an optimization effect description document will be generated, prompting the user that the solution has not fully met the preset standard and suggesting further optimization in conjunction with manual review.
[0115] The preset standards mainly include performance improvement standards and security assurance standards. Performance improvement standards (partial compliance is allowed) include reduced SQL execution time, reduced logical read volume, decreased CPU time, and peak memory usage less than the original query. Security assurance standards (all must be met) include no new lock waits or deadlock events triggered after optimization, completely consistent result sets before and after optimization (number of rows, field values, sort order, etc.), no syntax errors or permission issues, no major adjustments to the table structure required by the changes, and index storage overhead within permissible limits.
[0116] Step S148: Feedback and re-optimization mechanism: If the execution results of the determined optimal solution do not meet the preset standard, collect the optimization data and send it back to step S120 to trigger a new round of optimization iteration.
[0117] In this embodiment, the optimization data includes the original information of slow SQL queries, the candidate optimization schemes generated this time, the optimization scheme execution logs, the execution results, and the performance bottleneck analysis report. After the above optimization data is sent back to step S120, the system adjusts the analysis dimensions based on this data, focusing on iteratively generating optimization schemes for the performance bottlenecks corresponding to the unmet evaluation indicators, while referring to the heuristic rule set and the reinforcement rules related to this type of bottleneck in the expert calibration knowledge base.
[0118] Step S150: The target optimization scheme is invoked to perform structured reflection on the optimization process data using a large language model to obtain reflection results, and the reflection results are updated to the expert calibration knowledge base to update and optimize the optimization process.
[0119] In this embodiment, the large language model is a pre-trained natural language processing model capable of understanding and generating natural language text. The optimization process data includes the original information of the database statements to be optimized, a set of candidate optimization schemes, test results of each candidate scheme, and a comprehensive score of the target optimization scheme.
[0120] The large language model is used to analyze and reflect on this data, identifying successful experiences and shortcomings in the optimization process, summarizing optimization patterns and rules, and generating structured reflection results. These structured reflection results include problems encountered during the optimization process, solutions, optimization effect analysis, and experience summaries.
[0121] The reflection results are updated to the expert calibration knowledge base, enabling the knowledge base to continuously accumulate new optimization experience, thereby improving the accuracy and efficiency of subsequent optimization processes. After each optimization, the optimization scheme is comprehensively scored. If the score reaches a preset score, a large language model is invoked to perform structured reflection on the entire optimization process, and the reflection results are updated to the expert calibration knowledge base.
[0122] Step S151: Obtain the optimization process data corresponding to the target optimization scheme. The optimization process data includes the original information of the database statement to be optimized, the set of candidate optimization schemes, the test results of the candidate optimization schemes, and the comprehensive score result of the target optimization scheme. The comprehensive score result of the target optimization scheme includes the rejection factor status identifier and the performance improvement rate calculation result.
[0123] In this embodiment, the original information of the database statement to be optimized includes the statement text, execution frequency, and original execution performance indicators; the candidate optimization scheme set is all candidate optimization schemes generated in step S127; the test results of the candidate optimization schemes include the execution results and execution performance indicators of each scheme; the comprehensive score result of the target optimization scheme is a comprehensive evaluation of the target optimization scheme, wherein the rejection factor status indicator is used to indicate whether there are factors that cause the scheme to be rejected (such as execution failure, inconsistent results, etc.), and the performance improvement rate calculation result is the percentage improvement in execution performance of the target optimization scheme compared to the database statement to be optimized. When obtaining this data, it needs to be extracted from storage locations such as database optimization logs and test reports, and then organized and formatted for processing by the large language model.
[0124] Step S152: Call the large language model to perform hierarchical parsing processing on the optimization process data, extract the core performance bottleneck features of the database statement to be optimized and the corresponding solution descriptions. The core performance bottleneck features include execution time bottleneck, logical read bottleneck, CPU usage bottleneck, and memory usage bottleneck.
[0125] In this embodiment, hierarchical parsing processing refers to the large language model performing a layer-by-layer in-depth analysis of the optimization process data. First, it understands the structure and content of the data as a whole, and then extracts key information.
[0126] The extraction of core performance bottleneck characteristics is achieved by analyzing the original execution performance indicators of the database statement to be optimized and the test results of candidate optimization schemes. For example, if the execution time of the statement to be optimized is long, and a certain candidate optimization scheme significantly reduces the execution time by creating an index, then the execution time bottleneck can be identified as one of the core performance bottlenecks.
[0127] The solution description is a textual description of the methods and strategies used to address these bottlenecks, such as "By creating indexes for frequently filtered fields, the number of logical reads was reduced, thereby lowering execution time." The large language model uses natural language understanding technology to extract this information from the optimization process data and output it in a structured form.
[0128] Step S153: Based on the core performance bottleneck characteristics, perform a comparative analysis on the execution performance, statement execution results, and comprehensive score results of the target optimization scheme for each candidate optimization scheme in the candidate optimization scheme set, and generate a scheme comparison feature set, which includes performance improvement difference, score difference, and rejection factor status difference.
[0129] In this embodiment, the performance comparison analysis compares the candidate optimization schemes in terms of execution performance, statement execution results, and overall score. The performance improvement difference is the difference in performance improvement rate between different candidate optimization schemes. For example, if the performance improvement rate of scheme A is 50% and the performance improvement rate of scheme B is 30%, then the performance improvement difference between the two is 20%.
[0130] The score difference refers to the difference between the overall score results of each scheme. The veto factor status difference is a comparison of the veto factor status indicators of different schemes, such as which schemes have veto factors and which do not. Through these comparative analyses, a scheme comparison feature set containing multiple dimensions of comparative indicators is generated to facilitate subsequent analysis of the advantages and disadvantages of different schemes.
[0131] Step S1531: Extract the comprehensive score of the target optimization scheme for each candidate optimization scheme in the candidate optimization scheme set, and separate the rejection factor status identifier and performance improvement rate calculation result for each candidate optimization scheme.
[0132] In this embodiment, the comprehensive score result of the target optimization scheme is structured data containing multiple fields, including the rejection factor status identifier and the performance improvement rate calculation result. Through data parsing technology, the values of these two fields are extracted from the comprehensive score result and stored in different variables. For example, for each candidate optimization scheme, its rejection factor status identifier is stored in one list, and the performance improvement rate calculation result is stored in another list for subsequent comparison and analysis.
[0133] Step S1532: Compare the performance improvement rate calculation result with the original performance parameters of the database statement to be optimized, and calculate the performance improvement difference of each candidate optimization scheme.
[0134] In this embodiment, the original performance parameters of the database statement to be optimized include the original execution time and the original number of logical reads. The performance improvement rate calculation result of each candidate optimization scheme is compared with the original performance parameters. For example, if the original execution time is 1000 milliseconds, the execution time of scheme A is 500 milliseconds, with a performance improvement rate of 50%; and the execution time of scheme B is 600 milliseconds, with a performance improvement rate of 40%. Then the performance improvement difference between scheme A and scheme B is 10%. The performance improvement difference can be calculated using absolute difference, relative difference, etc., depending on the business requirements.
[0135] Step S1533: Compare the comprehensive score result of the target optimization scheme with the preset score threshold, and calculate the score difference of each candidate optimization scheme.
[0136] In this embodiment, the preset score threshold is the standard score for judging whether a candidate optimization scheme is qualified. The comprehensive score result of each candidate optimization scheme is compared with the preset score threshold, and the difference between the two is calculated, i.e., the score difference degree. For example, if the preset score threshold is 80 points, the comprehensive score of scheme A is 90 points, and the score difference degree is 10 points; the comprehensive score of scheme B is 75 points, and the score difference degree is -5 points. The score difference degree can reflect whether the comprehensive performance of the scheme meets expectations.
[0137] Step S1534: Compare the rejection factor status identifiers of different candidate optimization schemes, statistically analyze the rejection factor triggering frequency and type distribution, and generate rejection factor status differences.
[0138] In this embodiment, the veto factor status identifier has two states: 0 indicates that the veto factor has not been triggered, and 1 indicates that the veto factor has been triggered. The veto factor triggering frequency is obtained by statistically analyzing the proportion of the number of schemes that trigger the veto factor in the candidate optimization scheme set to the total number of schemes.
[0139] Simultaneously, the types of triggered veto factors are categorized and statistically analyzed. For example, the number of schemes triggering veto factors due to execution failure, the number of schemes triggering veto factors due to inconsistent results, etc., are counted to obtain the type distribution. The veto factor status difference is a comprehensive description of the veto factor status of different schemes, including information such as triggering frequency and type distribution.
[0140] Step S1535: Perform feature fusion on the performance improvement difference, the scoring difference, and the rejection factor state difference to generate the scheme comparison feature set containing multi-dimensional comparison indicators.
[0141] In this embodiment, feature fusion combines multiple comparative indicators, such as performance improvement difference, scoring difference, and rejection factor status difference, into a unified feature set. The fusion process can be performed by concatenating the indicators in a certain order to form a multi-dimensional vector.
[0142] For example, the feature set for comparing solutions can be represented as [performance improvement difference, score difference, veto factor trigger frequency, veto factor type distribution vector], where the veto factor type distribution vector is a vector representing the number of times different types of veto factors are triggered. Feature fusion integrates multiple disparate indicators, facilitating subsequent analysis and processing.
[0143] Step S154: Verify the applicability of the heuristic optimization rule set in the optimization process, extract the application scenario features of applicable rules, the conflict reasons description of inapplicable rules, and the corresponding veto factor triggering conditions. The veto factor triggering conditions include lock wait event triggering conditions, result set inconsistency triggering conditions, syntax permission error triggering conditions, table structure adjustment triggering conditions, and index storage overhead exceeding the limit triggering conditions.
[0144] In this embodiment, the applicability of the heuristic optimization rule set is verified by analyzing the application effect of each rule in the optimization process. For applicable rules, the scenario characteristics of their successful application are extracted, such as "the rule of creating an index is applicable when the number of table records is greater than 10,000 and the query contains the filtering conditions of this table". For inapplicable rules, the reasons for their conflict with the current optimization scenario are analyzed, such as "the rule of creating a hash index is not applicable because the current database storage engine does not support hash indexes".
[0145] The veto factor trigger condition is the specific condition that causes a rule to be inapplicable or a solution to be rejected. For example, the lock wait event trigger condition is "when the lock wait time exceeds a preset threshold during the execution of the optimization solution, the veto factor is triggered." By verifying and extracting this information, the set of heuristic optimization rules can be optimized and improved.
[0146] Step S155: Identify the key adjustment points in each step of the optimization process. The key adjustment points include state space mapping parameter adjustment, action space matching strategy adjustment, consistency grouping threshold adjustment, optimization scheme scoring weight adjustment, and rejection factor judgment rule adjustment.
[0147] In this embodiment, key adjustment points refer to parameters or strategies that have a significant impact on the optimization results during the optimization process.
[0148] Adjustments to state space mapping parameters include adjustments to parameters such as feature selection and dimensionality normalization methods; adjustments to action space matching strategies include adjustments to strategies such as similarity calculation methods and matching thresholds; adjustments to consistency grouping thresholds refer to adjusting the thresholds used to determine whether standardized results are the same; adjustments to the scoring weights of optimization schemes refer to adjusting the weights of each indicator in the comprehensive score; and adjustments to the veto factor judgment rules refer to adjusting the triggering conditions and judgment criteria for veto factors.
[0149] Identifying these key adjustment points can provide direction for improvements in subsequent optimization processes.
[0150] Step S156: Analyze the core reasons why the target optimization scheme meets the preset performance threshold, security guarantee rules, target optimization scheme scoring requirements and triggering veto factors, and generate a reason feature vector. The reason feature vector includes security guarantee compliance features, performance improvement compliance features, and scoring compliance features.
[0151] In this embodiment, the core cause analysis explores the underlying reasons for the success or failure of the target optimization solution. If the target optimization solution meets the preset performance threshold, the specific reasons for its performance improvement are analyzed, such as whether it was achieved through creating indexes or optimizing the statement structure. If it meets the security assurance rules, the analysis examines how it avoids problems such as system instability and data inconsistency. If it meets the target optimization solution's scoring requirements, the contribution of each scoring indicator is analyzed. If a veto factor is triggered, the specific reasons for the veto are analyzed. The cause feature vector represents these reasons in the form of features. For example, the security assurance compliance feature can be represented by 1 for compliance and 0 for non-compliance; the performance improvement compliance feature can be represented by the specific numerical value of the performance improvement rate; and the scoring compliance feature can be represented by the difference between the comprehensive score and the preset score threshold.
[0152] Step S157: The core performance bottleneck characteristics, the solution approach description, the solution comparison feature set, the applicable rule application scenario characteristics, the conflict cause description, the key adjustment points, the cause feature vector, and the comprehensive score result of the target optimization scheme are correlated and integrated to obtain a structured reflection result.
[0153] In this embodiment, the association integration is to organize the various information extracted in the previous steps according to certain logical relationships to form a complete structured reflection result.
[0154] For example, core performance bottleneck characteristics can be associated with corresponding solution descriptions, solution comparison feature sets can be associated with applicable rule application scenario characteristics, and key adjustment points can be associated with cause feature vectors, etc.
[0155] Structured reflection specifically includes analyzing the core performance bottlenecks and solutions for slow SQL queries during the optimization process, comparing the merits of candidate optimization solutions, verifying the applicability of the heuristic rule set, identifying key adjustment points during the optimization iteration process, and identifying the core reasons for achieving the evaluation metrics. The results of structured reflection can be presented in a hierarchical format, such as dividing them into sections for problem description, solutions, optimization results, and experience summary, with each section containing relevant detailed information.
[0156] Step S158: Update the structured reflection results to the expert calibration knowledge base to update and optimize the optimization process.
[0157] In this embodiment, the process of updating the expert calibration knowledge base includes converting and storing the structured reflection results according to the storage format of the knowledge base.
[0158] First, the structured reflection results are categorized, for example, by optimization scenario, statement type, etc.; then, the categorized results are stored in the corresponding locations in the knowledge base; finally, an index is created for subsequent querying and use.
[0159] The updated expert calibration knowledge base includes new optimization experiences and rules. In subsequent optimization processes, the statement optimization analysis model can utilize this new knowledge for more accurate analysis and optimization, thereby continuously improving optimization results. When updating the structured reflection results to the expert calibration knowledge base, a knowledge classification storage method is adopted, categorizing the reflection results according to the type of database statement to be optimized, database type, and business scenario type.
[0160] Step S1581: Call the knowledge classification module of the expert calibration knowledge base to classify the structured reflection results according to the classification labels. The classification labels are related to the statement type, database type, and business scenario type of the database statement to be optimized.
[0161] In this embodiment, the knowledge classification module is a component of the expert calibration knowledge base, responsible for classifying new knowledge. Classification tags include statement type (e.g., SELECT, INSERT, UPDATE, DELETE), database type (e.g., MySQL, Oracle, SQL Server), and business scenario type (e.g., e-commerce order query, financial transaction record query, logistics information query, etc.).
[0162] Based on these attributes of the database statement to be optimized, appropriate category labels are assigned to the structured reflection results. For example, if the statement to be optimized is an e-commerce order query in a MySQL database, the category label would be "SELECT, MySQL, e-commerce order query". This categorization process makes the knowledge in the knowledge base more organized, facilitating subsequent retrieval and application.
[0163] Step S1582: Based on the classification label, retrieve the corresponding knowledge storage path in the expert calibration knowledge base. If there are historical knowledge entries with the same classification label, perform conflict detection on the structured reflection results and historical knowledge entries. The conflict detection includes conflict detection of core performance bottleneck features, conflict detection of optimization scheme scoring weights, and conflict detection of veto factor triggering conditions.
[0164] In this embodiment, the knowledge storage path is pre-defined based on category tags, with each category tag corresponding to a unique storage path. The retrieval process finds the corresponding storage path based on the category tag and checks whether there are historical knowledge entries with the same category tag under that path.
[0165] If conflicts exist, conflict detection is required. Core performance bottleneck feature conflict detection compares the consistency between the structured reflection results and the core performance bottleneck features in historical knowledge entries; for example, do both consider execution time to be the primary bottleneck? Optimization scheme scoring weight conflict detection compares whether the weights of each indicator in the comprehensive score are the same for both. Veto factor trigger condition conflict detection compares whether the veto factor trigger conditions are consistent for both. Through conflict detection, contradictions between new and existing knowledge can be discovered.
[0166] Step S1583: If there is no conflict, append the structured reflection result to the corresponding knowledge storage path and associate it with the veto factor status identifier and performance improvement rate calculation result in the comprehensive scoring result of the target optimization scheme.
[0167] In this embodiment, if the conflict detection result shows no conflict, the structured reflection result is directly appended to the corresponding knowledge storage path. Simultaneously, the veto factor status indicator and performance improvement rate calculation result in the comprehensive scoring result of the target optimization scheme are associated with the structured reflection result, so that the corresponding optimization effect and the existence of veto factors can be understood when using this knowledge later.
[0168] Association operations can be achieved by adding corresponding fields to the structured reflection results. For example, add fields such as "veto factor status identifier" and "performance improvement rate" to the reflection results and fill in the corresponding values.
[0169] Step S1584: If a conflict exists, the conflict content is fused, the valid features of both sides of the conflict are retained, and a conflict solution description is generated. The conflict solution description includes the conflict type, conflict feature comparison, fusion rules, and fused features.
[0170] In this embodiment, conflict fusion processing is a method used when there is a conflict between the structured reflection results and historical knowledge entries. First, the conflict type is determined, such as core performance bottleneck feature conflict, scoring weight conflict, etc.; then, the features of the conflicting parties are compared and their respective feature values are listed; next, the conflicting features are fused according to preset fusion rules (such as using new knowledge as the standard, using the majority of knowledge as the standard, combining the features of both parties, etc.); finally, the fused features are generated, and the fusion process and results are described to form a conflict resolution description.
[0171] For example, if the structured reflection results identify execution time as the core bottleneck, while historical knowledge entries identify logical reads as the core bottleneck, the fusion rule could be to simultaneously identify both as core bottlenecks and optimize for both bottlenecks in the solution.
[0172] Step S1585: Perform integrity verification on the updated expert calibration knowledge base to ensure the logical consistency between the newly added knowledge entries and the existing knowledge system. The integrity verification includes classification label consistency verification, feature association integrity verification, and rule applicability verification.
[0173] In this embodiment, integrity verification ensures that the updated knowledge base is logically consistent and complete. Category label consistency verification checks whether the category labels of newly added knowledge entries are consistent with the existing classification system in the knowledge base; feature association integrity verification checks whether there are associations between the various features in the newly added knowledge entries, and whether these associations are complete; rule applicability verification checks whether the optimization rules in the newly added knowledge entries are applicable to the scenarios corresponding to their category labels.
[0174] Integrity checks can identify and correct logical errors and incompleteness that may occur during knowledge base updates.
[0175] Step S1586: Generate a knowledge update log, which includes the updated category label, storage path, conflict resolution result, update timestamp, and associated optimization process data identifier. The knowledge update log is used for knowledge tracing in subsequent optimization processes and dynamic adjustment of the heuristic optimization rule set.
[0176] In this embodiment, the knowledge update log records the knowledge base update process, facilitating subsequent tracing of the knowledge's source and update history. The category label records the category of the updated knowledge; the storage path records the knowledge's location in the knowledge base; the conflict resolution result records whether a conflict exists and how it was resolved; the update timestamp records the time of the knowledge update; and the associated optimization process data identifier records the identifier of the optimization process data related to this knowledge, allowing for viewing of the original data when needed.
[0177] Knowledge update logs can be stored in the form of text files or database tables to ensure that they are searchable and traceable.
[0178] Step S160: Check whether the target optimization scheme meets the security assurance standards. The security assurance standards include no new lock wait events, complete consistency of the result set before and after optimization, no syntax errors or permission issues, no major adjustments to the table structure, and index storage overhead within the allowable range.
[0179] In this embodiment, the security assurance standard is a series of standards that ensure that the target optimization scheme will not have a negative impact on the database system and data during implementation.
[0180] No new lock wait events means that no new lock wait events will be added during the execution of the target optimization plan, or the lock wait time is within the preset range; the result set before and after optimization is completely consistent, meaning that the execution result of the target optimization plan is exactly the same as the execution result of the database statement to be optimized; no syntax errors or permission issues mean that the statement of the target optimization plan conforms to the database syntax specification and the executing user has the corresponding permissions; no major adjustments to the table structure mean that the target optimization plan will not make major changes to the table structure, such as adding / deleting fields or modifying field types; index storage overhead is within the allowable range, meaning that the storage space required to create the index does not exceed the preset storage threshold.
[0181] The inspection process is achieved by executing the target optimization plan and monitoring relevant metrics. For example, it checks lock wait events through database lock monitoring tools, checks result set consistency by comparing execution results, checks syntax and permissions through a syntax parser, and estimates storage overhead by analyzing index creation statements.
[0182] Step S161: If any security assurance standard is not met, the veto factor status identifier is set to the first state, the performance improvement rate calculation result is set to the base value, and the comprehensive score result of the target optimization scheme is set to the base score.
[0183] In this embodiment, if the target optimization scheme fails to meet any of the security assurance criteria—for example, a new lock wait event occurs, the result set is inconsistent, or there is a syntax error—the rejection factor status flag is set to the first state (e.g., 1 indicates that the rejection factor is triggered). At this time, the performance improvement rate calculation result no longer considers the actual performance improvement but is set to a base value (e.g., 0), indicating that the scheme is not accepted due to security issues. The comprehensive score result of the target optimization scheme is also set to a base score (e.g., 0 points), indicating that the scheme does not meet the requirements.
[0184] For example, a veto factor V can be introduced during the comprehensive evaluation of the target optimization scheme. V=1 when all security assurance criteria are met, otherwise V=0. Here, P_lock represents no new lock waiting, P_data represents result set consistency, P_syntax represents no syntax and permission errors, P_schema represents no table structure adjustment, and P_index represents controllable index storage overhead.
[0185] Step S162: If all security assurance standards are met, the veto factor status flag is set to the second state, and the performance improvement rate is calculated based on the execution performance. The performance improvement rate is obtained by summing the products of the execution time improvement rate, logical read improvement rate, CPU time improvement rate, and peak memory usage improvement rate with their respective weights.
[0186] In this embodiment, if the target optimization scheme meets all security assurance standards, the veto factor status flag is set to the second state (e.g., 0 indicates that the veto factor has not been triggered).
[0187] For example, the performance improvement rate is calculated by comprehensively considering the improvement of multiple execution performance indicators. The execution time improvement rate R_T = max(0, (T_o - T_N) / T_o), where T_o is the original execution time and T_N is the optimized execution time; the logical read improvement rate R_L = max(0, (L_o - L_N) / L_o), where L_o is the original number of logical reads and L_N is the optimized number of logical reads; the CPU time improvement rate R_C = max(0, (C_o - C_N) / C_o), where C_o is the original CPU time and C_N is the optimized CPU time; and the peak memory usage improvement rate R_M = max(0, (M_o - M_N) / M_o), where M_o is the original peak memory usage and M_N is the optimized peak memory usage. Let the weights of each indicator be W_T, W_L, W_C, and W_M, satisfying W_T + W_L + W_C + W_M = 1. Performance improvement P = W_T R_T+W_L R_L+W_C R_C+W_M R_M.
[0188] Step S163: Multiply the state coefficient corresponding to the veto factor state identifier by the performance improvement rate to obtain the comprehensive score result of the target optimization scheme.
[0189] In this embodiment, the state coefficient corresponding to the veto factor state identifier is a value set according to the state. For example, the state coefficient corresponding to the second state (no veto factor triggered) is 1, and the state coefficient corresponding to the first state (veto factor triggered) is 0.
[0190] For example, the overall score of the target optimization scheme is equal to the product of the state coefficient and the performance improvement rate. The final score of the optimization scheme is the product of the rejection factor V and the performance improvement score P: S(ω)=V(ω) Σ(i∈{T,L,C,M})ω_i max(0, (x_i0-x_iN) / x_i0) When the veto factor is not triggered, the overall score is equal to the performance improvement rate; when the veto factor is triggered, the overall score is 0.
[0191] Step S164: Compare the comprehensive score result of the target optimization scheme with the preset score threshold to determine whether the structured reflection trigger condition has been met.
[0192] In this embodiment, a preset score threshold is used as the standard for determining whether structured reflection is needed; for example, the preset score threshold is 50 points. If the overall score of the target optimization scheme is greater than or equal to the preset score threshold, it is considered that the structured reflection trigger condition has been met, and structured reflection needs to be performed by calling the large language model; otherwise, reflection is not required. In this way, it can be ensured that only schemes with better optimization effects are used to update the expert calibration knowledge base, thereby improving the quality of the knowledge base.
[0193] The method provided in this application obtains metadata information matching the database statement to be optimized and its execution scenario, and generates a set of candidate optimization schemes by progressively searching and analyzing the statement optimization analysis model based on a set of heuristic optimization rules and an expert calibration knowledge base. Then, it verifies the performance of the candidate schemes in a simulated database environment, selects the target optimization schemes that are semantically equivalent and meet the performance standards, and finally performs structured reflection on the optimization process through a large language model and updates the expert knowledge base to construct an end-to-end closed-loop optimization system covering "analysis-verification-reflection-iteration". Compared with existing technologies that rely solely on empirical rules or simple algorithms for single optimization and lack a continuous tracking mechanism, this solution avoids the security risks of direct testing in the production environment through simulation environment verification, achieves dynamic accumulation of optimization knowledge through a structured reflection mechanism, and improves the adaptability of the optimization strategy to business scenarios by combining metadata information and heuristic rules. This significantly improves the accuracy, stability, and generalization ability of database statement optimization, effectively reduces the risk of performance regression, and forms an autonomously evolving optimization knowledge system.
[0194] Figure 2 This is a schematic diagram illustrating the structure of an artificial intelligence-based database statement optimization system 100 provided in an embodiment of this application. Figure 2 As shown, the processor 120 can be used in the AI-based database statement optimization system 100 and to perform the functions in this invention.
[0195] The AI-based database statement optimization system 100 can be a general-purpose server or a special-purpose server; both can be used to implement the AI-based database statement optimization method of this invention. Although only one server is shown in this invention, for convenience, the functions described in this invention can be implemented in a distributed manner on multiple similar platforms to balance the load.
[0196] For example, the AI-based database statement optimization system 100 may include a network port 110 connected to a network, one or more processors 120 for executing program instructions, a communication bus 130, and various forms of storage media 140, such as a disk, ROM, or RAM, or any combination thereof. Exemplarily, the AI-based database statement optimization system 100 may also include program instructions stored in ROM, RAM, or other types of non-transitory storage media, or any combination thereof. The method of the present invention can be implemented according to these program instructions. The AI-based database statement optimization system 100 also includes an input / output (I / O) interface 150 between the computer and other input / output devices.
[0197] For ease of explanation, only one processor is described in the AI-based database statement optimization system 100. However, it should be noted that the AI-based database statement optimization system 100 of this invention may also include multiple processors, and therefore the steps executed by one processor described in this invention may also be executed jointly or individually by multiple processors. For example, if the processor of the AI-based database statement optimization system 100 executes steps A and B, it should be understood that steps A and B may also be executed jointly by two different processors or individually by one processor. For example, the first processor executes step A, the second processor executes step B, or the first processor and the second processor jointly execute steps A and B.
[0198] The device embodiments described above are merely illustrative. The modules described as separate components may or may not be physically separate, and the components shown as modules may or may not be physical modules; that is, they may be located in one place or distributed across multiple network modules. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs. Those skilled in the art can understand and implement this without any inventive effort.
[0199] Finally, it should be noted that the above-disclosed embodiments are merely preferred embodiments of the present invention and are only used to illustrate the technical solutions of the present invention, not to limit them. Although the present invention has been described in detail with reference to the foregoing embodiments, those skilled in the art should understand that modifications can still be made to the technical solutions described in the foregoing embodiments, or equivalent substitutions can be made to some of the technical features. Such modifications or substitutions do not cause the essence of the corresponding technical solutions to deviate from the spirit and scope of the technical solutions of the embodiments of the present invention.
Claims
1. A database statement optimization method based on artificial intelligence, characterized in that, include: Obtain the database statement to be optimized and the metadata information matching the execution scenario of the database statement to be optimized. The metadata information includes table structure information, index configuration information, runtime environment parameters, and database performance indicators. Based on the heuristic optimization rule set and expert calibration knowledge base, the statement optimization analysis model is invoked to perform progressive retrieval analysis on the database statement to be optimized and the metadata information, generating a set of candidate optimization schemes; The candidate optimization schemes included in the candidate optimization scheme set are executed in a simulation database environment with parameters consistent with the production environment to obtain the test results of the candidate optimization schemes. The test results include statement execution results and execution performance. Based on the execution performance and the statement execution result, a target optimization scheme that is semantically equivalent to the database statement to be optimized is selected from the candidate optimization scheme set. The execution performance of the target optimization scheme is greater than or equal to a preset performance threshold and the statement execution result is a valid result. The target optimization scheme is invoked to perform structured reflection on the optimization process data using a large language model to obtain reflection results, and the reflection results are updated to the expert calibration knowledge base to update and optimize the optimization process.
2. The database statement optimization method based on artificial intelligence according to claim 1, characterized in that, The step of selecting a target optimization scheme that is semantically equivalent to the database statement to be optimized from the candidate optimization scheme set based on the execution performance and the statement execution result includes: Based on the execution result of the statement, the abnormal optimization schemes that failed to execute are removed from the candidate optimization scheme set to obtain the effective candidate optimization scheme set; The execution results of the statements of the effective candidate optimization schemes included in the set of effective candidate optimization schemes and the execution results of the statements of the database statements to be optimized are mapped to the standardized result space to obtain the standardized results of the effective candidate optimization schemes and the standardized results of the statements of the database statements to be optimized. Based on the standardized results of the effective candidate optimization schemes, the effective candidate optimization schemes included in the set of effective candidate optimization schemes are grouped in a consistent manner to obtain semantic grouping results, wherein the effective candidate optimization schemes included in the same semantic grouping results have the same standardized results; Based on the standardized results of the database statement to be optimized and the standardized results corresponding to each semantic grouping result, candidate semantic grouping results that are semantically equivalent to the database statement to be optimized are determined. Calculate the consistency score of the candidate semantic grouping results, and select target semantic grouping results with a consistency score greater than or equal to a preset score threshold from the candidate semantic grouping results based on the consistency score; Based on the effective candidate optimization schemes in the target semantic grouping results, the statement execution results and execution performance of each effective candidate optimization scheme, and the preset judgment rules, the target optimization scheme is determined. The preset judgment rules include the preset performance threshold and security assurance rules. The security assurance rules include at least one of the following: system stability rules, data consistency rules, statement legality rules, and resource consumption rationality rules.
3. The database statement optimization method based on artificial intelligence according to claim 1, characterized in that, The process, based on a heuristic optimization rule set and an expert-calibrated knowledge base, invokes a statement optimization analysis model to perform progressive retrieval and analysis on the database statements to be optimized and the metadata information, generating a set of candidate optimization schemes, including: The initialization operation is completed by calling the reinforcement learning scheduling module built into the statement optimization analysis model, predefining the state space covering the statement feature dimension and metadata dimension, and the action space covering the adjustment operation of the heuristic optimization rule set and the historical optimization operation of the expert calibration knowledge base; The database statement to be optimized, the table structure information, the index configuration information, the runtime environment parameters, and the database performance indicators included in the metadata are mapped to the state space to generate a unique corresponding current state node; Based on the current state node, the corresponding heuristic optimization rule adjustment operation is matched in the action space. The matched adjustment operation is called to perform fragmented adjustment on the database statement to be optimized, and an initial set of adjusted statement fragments is generated. The features of the initially adjusted set of sentence fragments are mapped to the state space, and iterative state nodes are obtained by completing the state iterative update. Based on the iterative state node, the corresponding expert calibration knowledge base historical optimization operation is matched in the action space. The matched historical optimization operation is called to perform a secondary adjustment on the initially adjusted set of sentence fragments to generate an optimized set of sentence fragments. The optimized set of statement fragments is concatenated according to the original logical order of the database statements to be optimized, generating multiple sets of optimized statements to be verified. The multiple sets of statements to be verified and optimized are subjected to syntax compliance verification, semantic consistency verification, and deduplication processing to generate the candidate optimization scheme set.
4. The database statement optimization method based on artificial intelligence according to claim 3, characterized in that, The step of mapping the database statement to be optimized, the table structure information, index configuration information, runtime environment parameters, and database performance metrics included in the metadata information to the state space to generate a unique corresponding current state node includes: Extract the structural and semantic features of the database statement to be optimized, as well as the attribute features corresponding to the table structure information, index configuration information, runtime environment parameters, and database performance indicators included in the metadata information; The structural features, semantic features, and attribute features are matched one by one with the predefined dimensions of the state space to determine the state dimension mapping relationship corresponding to each feature; According to the standardized dimensional requirements of the state space, all features are dimensionally aligned to generate a set of dimensional features that conform to the state space dimensional specification. The dimensional feature set is input into the feature encoding module built into the state space to complete the feature encoding operation and generate a state encoding vector with a unified dimension. Verify the dimensionality fit between the state encoding vector and the state space; if any dimension is missing, supplement the default feature encoding of the corresponding dimension. The verified state encoding vector is marked as the current state node and associated with the identifier of the database statement to be optimized.
5. The database statement optimization method based on artificial intelligence according to claim 3, characterized in that, The adjustment operation based on the heuristic optimization rule matched in the action space by the current state node, calls the matched adjustment operation to perform fragmented adjustment on the database statement to be optimized, generating an initial set of adjusted statement fragments, including: Analyze all the dimensional features contained in the current state node and extract the action trigger condition identifiers corresponding to each dimensional feature; Traverse the action entries stored in the action space that correspond to the set of heuristic optimization rules, and match the triggering conditions corresponding to the action entries with the action triggering condition identifiers to obtain an initial set of matched actions; The applicability of each action item in the initial matching action set is verified, and action items that conflict with the metadata-related dimension features of the current state node are removed to obtain a valid matching action set. According to the preset action priority sorting rules of the action space, the action items in the effective matching action set are sorted to obtain the action execution sequence; The corresponding adjustment operations are executed sequentially according to the action execution sequence to adjust the corresponding fragments of the database statement to be optimized, generating multiple adjusted statement fragments; The adjusted statement fragments are subjected to grammatical format validation, and statement fragments that do not meet the grammatical requirements are removed to obtain the initial set of adjusted statement fragments.
6. The database statement optimization method based on artificial intelligence according to claim 1, characterized in that, The process of calling a large language model to perform structured reflection on the optimization process data of the target optimization scheme yields the following reflection results: Obtain the optimization process data corresponding to the target optimization scheme. The optimization process data includes the original information of the database statement to be optimized, the set of candidate optimization schemes, the test results of the candidate optimization schemes, and the comprehensive score result of the target optimization scheme. The comprehensive score result of the target optimization scheme includes the rejection factor status identifier and the performance improvement rate calculation result. The large language model is invoked to perform hierarchical parsing processing on the optimization process data, extracting the core performance bottleneck features of the database statement to be optimized and the corresponding solution descriptions. The core performance bottleneck features include execution time bottleneck, logical read bottleneck, CPU usage bottleneck, and memory usage bottleneck. Based on the core performance bottleneck characteristics, the execution performance, statement execution results, and comprehensive score results of the target optimization scheme in the candidate optimization scheme set are compared and analyzed to generate a scheme comparison feature set, which includes performance improvement difference, score difference, and rejection factor status difference. Verify the applicability of the heuristic optimization rule set in the optimization process, extract the application scenario characteristics of applicable rules, the conflict reasons description of inapplicable rules, and the corresponding veto factor triggering conditions. The veto factor triggering conditions include lock wait event triggering conditions, result set inconsistency triggering conditions, syntax permission error triggering conditions, table structure adjustment triggering conditions, and index storage overhead exceeding the limit triggering conditions. Identify the key adjustment points in each step of the optimization process. These key adjustment points include adjustments to state space mapping parameters, action space matching strategies, consistency grouping thresholds, optimization scheme scoring weights, and rejection factor determination rules. Analyze the core reasons why the target optimization scheme meets the preset performance threshold, security assurance rules, target optimization scheme scoring requirements and triggering veto factors, and generate a cause feature vector. The cause feature vector includes security assurance compliance features, performance improvement compliance features, and scoring compliance features. The core performance bottleneck characteristics, the solution approach description, the solution comparison feature set, the applicable rule application scenario characteristics, the conflict cause description, the key adjustment points, the cause feature vector, and the comprehensive score result of the target optimization scheme are correlated and integrated to obtain a structured reflection result.
7. The database statement optimization method based on artificial intelligence according to claim 6, characterized in that, The method further includes: Check whether the target optimization scheme meets the security assurance standards, which include no new lock wait events, complete consistency of the result set before and after optimization, no syntax errors or permission issues, no major adjustments to the table structure, and index storage overhead within the allowable range. If any security assurance standard is not met, the veto factor status identifier is set to the first state, the performance improvement rate calculation result is set to the base value, and the comprehensive score result of the target optimization scheme is set to the base score. If all security assurance standards are met, the veto factor status flag is set to the second state, and the performance improvement rate is calculated based on the execution performance. The performance improvement rate is obtained by summing the products of the execution time improvement rate, logical read improvement rate, CPU time improvement rate, and peak memory usage improvement rate with their respective weights. Multiply the state coefficient corresponding to the veto factor state identifier by the performance improvement rate to obtain the comprehensive score result of the target optimization scheme; The comprehensive score of the target optimization scheme is compared with the preset score threshold to determine whether the structured reflection trigger condition has been met.
8. The database statement optimization method based on artificial intelligence according to claim 6, characterized in that, Based on the core performance bottleneck characteristics, a comparative analysis is performed on the execution performance, statement execution results, and comprehensive score results of the target optimization scheme in the candidate optimization scheme set to generate a scheme comparison feature set, including: Extract the comprehensive score of the target optimization scheme for each candidate optimization scheme in the candidate optimization scheme set, and separate the rejection factor status identifier and performance improvement rate calculation result for each candidate optimization scheme; The performance improvement rate calculation result is compared with the original performance parameters of the database statement to be optimized, and the performance improvement difference of each candidate optimization scheme is calculated. The comprehensive score of the target optimization scheme is compared with a preset score threshold, and the score difference of each candidate optimization scheme is calculated. Compare the status indicators of rejection factors for different candidate optimization schemes, statistically analyze the triggering frequency and type distribution of rejection factors, and generate differences in rejection factor status. The performance improvement difference, the scoring difference, and the rejection factor status difference are fused to generate the scheme comparison feature set containing multi-dimensional comparison indicators.
9. The database statement optimization method based on artificial intelligence according to claim 6, characterized in that, The step of updating the structured reflection results to the expert calibration knowledge base to update and optimize the optimization process includes: The knowledge classification module of the expert calibration knowledge base is invoked to classify the structured reflection results according to the classification labels. The classification labels are related to the statement type, database type, and business scenario type of the database statement to be optimized. Based on the classification labels, the corresponding knowledge storage path in the expert calibration knowledge base is retrieved. If there are historical knowledge entries with the same classification labels, conflict detection is performed between the structured reflection results and the historical knowledge entries. The conflict detection includes conflict detection of core performance bottleneck features, conflict detection of optimization scheme scoring weights, and conflict detection of veto factor triggering conditions. If there is no conflict, the structured reflection results are appended to the corresponding knowledge storage path and associated with the veto factor status flag and performance improvement rate calculation results in the comprehensive scoring results of the target optimization scheme; If a conflict exists, the conflicting content is merged, retaining the valid features of both sides of the conflict and generating a conflict solution description. The conflict solution description includes the conflict type, conflict feature comparison, fusion rules, and fused features. The updated expert calibration knowledge base is subjected to integrity verification to ensure that the new knowledge entries are logically consistent with the existing knowledge system. The integrity verification includes classification label consistency verification, feature association integrity verification, and rule applicability verification. Generate a knowledge update log, which includes the updated category label, storage path, conflict resolution result, update timestamp, and associated optimization process data identifier. The knowledge update log is used for knowledge tracing in subsequent optimization processes and dynamic adjustment of the heuristic optimization rule set.
10. A database statement optimization system based on artificial intelligence, characterized in that, The method includes a processor and a computer-readable storage medium storing machine-executable instructions, which, when executed by a computer, implement the artificial intelligence-based database statement optimization method according to any one of claims 1-9.