SQL statement optimization method, electronic equipment, storage medium and product
By building an initial graph model and combining graph neural networks and large language models to analyze database element information, the problem of poor SQL optimization effect in existing technologies is solved, and more accurate and practical SQL statement optimization is achieved.
Patent Information
- Application Number
- CN202510786057.7
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-12
- Publication Date
- 2025-09-23
AI Technical Summary
In existing technologies, SQL optimization relies on large language models that cannot effectively perceive the table structure relationships, index configurations, and data distribution characteristics of the database, resulting in a lack of targetedness and practicality in the optimization, making it difficult to achieve the expected results in actual application environments.
By parsing the original SQL statement, building an initial graph model and adding database element information to it, the target graph model is analyzed using graph neural networks and large language models to generate optimized SQL statements.
The accuracy and practicality of SQL optimization have been significantly improved, making the optimization suggestions better adapt to the actual database environment and generating more targeted and practical target SQL statements.
Smart Images

Figure CN120688487A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of SQL optimization technology, and in particular to a SQL statement optimization method, electronic device, storage medium, and product. Background Art
[0002] In the field of database SQL optimization, optimization primarily relies on large language models to directly process natural language queries or SQL statements. However, because these models cannot effectively perceive key contextual information such as database table structures, index configurations, and data distribution characteristics, optimization lacks specificity and practicality, making it difficult to achieve the expected results in real-world applications. Consequently, current SQL optimization techniques suffer from poor SQL statement optimization results.
[0003] The above content is only used to assist in understanding the technical solution of this application and does not constitute an admission that the above content is prior art. Summary of the Invention
[0004] The main purpose of this application is to provide a SQL statement optimization method, system, electronic device, storage medium and computer program product, aiming to solve the technical problem of poor SQL statement optimization effect.
[0005] To achieve the above objectives, the present application proposes a SQL statement optimization method, which includes:
[0006] Parsing the original SQL statement to obtain key elements in the original SQL statement and the association relationships between the key elements, and constructing an initial graph model based on the key elements and the association relationships;
[0007] Add the element information in the database corresponding to the original SQL statement to the initial graph model to obtain a target graph model;
[0008] The target graph model is analyzed based on a preset SQL statement optimization model to optimize the original SQL statement to obtain a target SQL statement.
[0009] In one embodiment, the steps of parsing the original SQL statement, obtaining key elements in the original SQL statement and associations between the key elements, and constructing an initial graph model based on the key elements and the associations include:
[0010] Decomposing the original SQL statement into grammatical units, and constructing an abstract syntax tree based on the grammatical structure relationship between the grammatical units;
[0011] Extracting key elements in the original SQL statement and the association relationships between the key elements from the abstract syntax tree;
[0012] The key elements are mapped into graph nodes, and connection edges of the graph nodes are constructed according to the association relationships to obtain an initial graph model.
[0013] In one embodiment, the preset SQL statement optimization model includes a graph neural network and a large language model. The step of analyzing the target graph model based on the preset SQL statement optimization model to optimize the original SQL statement to obtain the target SQL statement includes:
[0014] Extracting non-text features in the target graph model based on the graph neural network;
[0015] Extracting text features from the original SQL statement and the element information based on the large language model;
[0016] splicing the non-text features and the text features to obtain a fused feature vector;
[0017] The fused feature vector is input into the large language model to generate SQL statement modification content, and the original SQL statement is adjusted based on the SQL statement modification content to obtain an optimized target SQL statement.
[0018] In one embodiment, the non-text features include node logic features and node relationship features, and the step of extracting the non-text features in the target graph model based on a preset graph neural network includes:
[0019] Extracting attribute data of graph nodes in the target graph model based on a preset graph neural network, embedding the attribute data, and obtaining logical features of the nodes;
[0020] Obtaining adjacent nodes that have edges connected to the graph node, aggregating the feature vectors of the adjacent nodes to generate adjacent node features, and concatenating the adjacent node features with the feature vector of the graph node to obtain the node relationship feature;
[0021] The node logical features and the node relationship features are combined to obtain non-text features in the target graph model.
[0022] In one embodiment, the step of extracting text features from the original SQL statement and the element information based on the large language model includes:
[0023] Converting grammatical units in the original SQL statement into a first word vector sequence based on the large language model;
[0024] Extracting metadata text corresponding to the element information, performing word segmentation on the metadata text, and converting it into a second word vector sequence;
[0025] Concatenate the first word vector sequence and the second word vector sequence into an input sequence, and determine the context dependency of the word vectors in the input sequence through semantic association analysis;
[0026] Feature fusion is performed on the input sequence based on the context dependency to obtain text features in the original SQL statement and the element information.
[0027] In one embodiment, the step of combining the non-text features and the text features to obtain a fused feature vector includes:
[0028] Mapping the non-text features and the text features to the same dimensional space to obtain non-text features and text features of the same dimension;
[0029] A vector concatenation operation is performed on the non-text features and text features of the same dimension to generate a fused feature vector.
[0030] In one embodiment, the SQL statement optimization method further includes:
[0031] A training data set is received, and for any training sample in the training data set, an original SQL statement sample in the training sample is converted into a graph model sample;
[0032] Inputting the original SQL statement sample, the graph model sample, and the element information sample in the database corresponding to the original SQL statement sample into a preset initial model to obtain an optimized SQL statement sample, wherein the graph neural network branch in the preset initial model is used to process the graph model sample, and the large language model branch in the preset initial model is used to process the original SQL statement sample and the element information sample in the database corresponding to the original SQL statement sample;
[0033] Calculating the difference loss between the optimized SQL statement sample and the sample optimization result of the original SQL statement in the training sample, and updating the model parameters of the graph neural network branch and the large language model branch based on the difference loss;
[0034] After the preset initial model meets the preset training conditions, a preset SQL statement optimization model is obtained.
[0035] In addition, to achieve the above objectives, the present application also proposes a SQL statement optimization system, which includes:
[0036] An initial graph model generation module is used to parse the original SQL statement, obtain key elements in the original SQL statement and the association relationships between the key elements, and construct an initial graph model based on the key elements and the association relationships;
[0037] A target graph model generation module is used to add element information in the database corresponding to the original SQL statement to the initial graph model to obtain a target graph model;
[0038] The SQL statement optimization module is used to analyze the target graph model based on a preset SQL statement optimization model to optimize the original SQL statement to obtain a target SQL statement.
[0039] In addition, to achieve the above-mentioned purpose, the present application also proposes an electronic device, which includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the SQL statement optimization method as described above.
[0040] In addition, to achieve the above objectives, the present application also proposes a storage medium, which is a computer-readable storage medium and stores a computer program. When the computer program is executed by a processor, the steps of the SQL statement optimization method described above are implemented.
[0041] In addition, to achieve the above-mentioned purpose, the present application also provides a computer program product, which includes a computer program. When the computer program is executed by a processor, the steps of the SQL statement optimization method described above are implemented.
[0042] The present application provides a SQL statement optimization method, which includes: parsing the original SQL statement to obtain key elements in the original SQL statement and the association relationships between the key elements, and constructing an initial graph model based on the key elements and the association relationships; adding element information in the database corresponding to the original SQL statement to the initial graph model to obtain a target graph model; analyzing the target graph model based on a preset SQL statement optimization model to optimize the original SQL statement to obtain a target SQL statement.
[0043] This application parses the original SQL statement into a target graph model, which includes the key elements in the original SQL statement and the relationship between the key elements, analyzes the target graph model based on a preset SQL statement optimization model, optimizes the original SQL statement, and obtains the target SQL statement. Compared with related solutions, which cannot effectively perceive key contextual information such as the database's table structure relationships, index configuration, and data distribution characteristics, the optimized SQL statements lack pertinence and practicality. This application extracts key elements from the original SQL statements and the relationships between key elements, parses the original SQL statements into an initial graph model, and can clearly present the structure and relationship of the internal elements of the SQL statements, laying a solid foundation for subsequent optimization work. By adding the element information in the database corresponding to the original SQL statements to the initial graph model, it is possible to fully understand the database environment in which the SQL statements are executed, providing more complete and accurate information for subsequent optimization, so that the generated optimization suggestions can better adapt to the actual database environment. At the same time, based on the preset SQL statement optimization model, the target graph model is analyzed, and the rich information in the target graph model is fully utilized to perform more accurate and in-depth analysis, thereby generating more accurate target SQL statements that are more suitable for actual application scenarios. The generated optimization suggestions are more suitable for actual application scenarios, have stronger pertinence and practicality, and significantly improve the accuracy and practicality of SQL optimization. BRIEF DESCRIPTION OF THE DRAWINGS
[0044] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present application and, together with the description, serve to explain the principles of the present application.
[0045] In order to more clearly illustrate the embodiments of the present application or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the description of the prior art. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without any creative work.
[0046] Figure 1 A flowchart of the first embodiment of the SQL statement optimization method of this application is provided;
[0047] Figure 2 A flowchart of the second embodiment of the SQL statement optimization method of this application is provided;
[0048] Figure 3 This is a data collection flow chart of the SQL statement optimization method provided in Example 2 of this application;
[0049] Figure 4 This is a diagram of the model training architecture of the SQL statement optimization method provided in Example 2 of this application;
[0050] Figure 5 This is a model training flow chart of the SQL statement optimization method provided in Example 2 of this application;
[0051] Figure 6 This is a schematic diagram of the module structure of the SQL statement optimization system according to an embodiment of the present application;
[0052] Figure 7 This is a schematic diagram of the device structure of the hardware operating environment involved in the SQL statement optimization method in the embodiment of the present application.
[0053] The purpose, features and advantages of this application will be further explained in conjunction with the embodiments and with reference to the accompanying drawings. DETAILED DESCRIPTION
[0054] It should be understood that the first embodiment described herein is merely used to explain the technical solution of the present application and is not intended to limit the present application.
[0055] In order to better understand the technical solution of the present application, a detailed description will be given below in conjunction with the accompanying drawings and specific implementation methods.
[0056] The main solutions of the first embodiment of this application are:
[0057] In the first embodiment, for ease of description, the following description is made with the SQL statement optimization system as the execution subject.
[0058] In the field of database SQL optimization technology, large language models are mainly relied upon to directly process natural language queries or SQL statements to optimize SQL statements. However, since the models cannot effectively perceive key contextual information such as the database's table structure relationships, index configuration, and data distribution characteristics, the optimization lacks specificity and practicality, making it difficult to achieve the expected results in actual application environments.
[0059] The present application provides a solution that parses the original SQL statement into an initial graph model by extracting the key elements in the original SQL statement and the associations between the key elements. This can clearly present the structure and associations of the internal elements of the SQL statement, laying a solid foundation for subsequent optimization work. By adding the element information in the database corresponding to the original SQL statement to the initial graph model, it is possible to fully understand the database environment in which the SQL statement is executed, providing more complete and accurate information for subsequent optimization, so that the generated optimization suggestions can better adapt to the actual database environment. At the same time, based on the preset SQL statement optimization model, the target graph model is analyzed, and the rich information in the target graph model is fully utilized to perform more accurate and in-depth analysis, thereby generating a more accurate target SQL statement that fits the actual application scenario, making the generated optimization suggestions more in line with the actual application scenario, with stronger pertinence and practicality, and significantly improving the accuracy and practicality of SQL optimization.
[0060] It should be noted that the execution entity of the first embodiment may be a computing service device with data processing, network communication, and program execution capabilities, such as a tablet computer, personal computer, mobile phone, or other electronic device, or a system, application, or program capable of implementing the aforementioned functions. The following describes the first embodiment and the following embodiments using the SQL statement optimization system as an example.
[0061] Based on this, the embodiment of the present application provides a method for optimizing SQL statements. Figure 1 , Figure 1 This is a flow chart of the first embodiment of the SQL statement optimization method of this application.
[0062] In this embodiment, the SQL statement optimization method includes steps S01 to S03:
[0063] Step S01: parse the original SQL statement to obtain key elements in the original SQL statement and the relationships between the key elements, and build an initial graph model based on the key elements and the relationships;
[0064] It should be noted that SQL (Structured Query Language) is a standard programming language for managing and operating relational databases. It is widely used to create, modify, query and manage data in databases. The original SQL statement refers to the unoptimized SQL query statement entered by the user. It contains the database operations that the user wants to perform. The system receives the original SQL statement submitted by the user, deeply analyzes the original SQL statement, identifies and extracts the key elements in the SQL statement. The key elements refer to the basic units that constitute the query or operation in the SQL statement, including but not limited to table names, column names, predicates, join conditions, aggregate functions, etc. The system abstracts these key elements into graph nodes and constructs relationship edges based on the association relationships between elements, thereby forming a complete initial graph model. Association relationships refer to the semantic or structural connections between key elements, such as join conditions, attribution relationships, etc. They define the interactions and dependencies between elements. The initial graph model is a graph structure model that contains all key elements in the original SQL statement and the association relationships between them.
[0065] It can be understood that step S01 parses the original SQL statement and converts it into an initial graph model. The model not only contains the key elements in the SQL statement, but also clarifies the relationship between these elements. It can intuitively display the semantic information and structural characteristics of the SQL statement, and provide comprehensive and accurate context information for subsequent optimization.
[0066] Step S02, adding the element information in the database corresponding to the original SQL statement to the initial graph model to obtain the target graph model;
[0067] It should be noted that the element information in the database corresponding to the original SQL statement is obtained from the database management system. Element information refers to various key information in the database corresponding to the original SQL statement. This information can include table structure information (such as column data type, whether it is a primary key, whether it allows nulls, etc.), index configuration information (such as index type, index column, index uniqueness, etc.), and data distribution characteristics (such as the number of unique values in a column, data statistics, etc.). This element information is added to the initial graph model to enrich the attributes of the nodes and edges of the initial graph model. For example, attributes such as the table size and number of rows are added to the table node, and attributes such as the data type and number of unique values are added to the column node to obtain the target graph model.
[0068] It can be understood that step S02 enables the model to fully understand the database environment in which the SQL statement is executed by adding the element information in the database corresponding to the original SQL statement to the initial graph model, providing more complete and accurate information for subsequent optimization models, thereby generating optimization suggestions that are more in line with actual conditions, and improving the accuracy and practicality of the optimization suggestions.
[0069] Step S03: Analyze the target graph model based on the preset SQL statement optimization model to optimize the original SQL statement to obtain the target SQL statement.
[0070] It should be noted that after obtaining the target graph model, the target graph model is input into the preset SQL statement optimization model. The preset SQL statement optimization model learns a large number of SQL statements and corresponding optimization cases, masters the rules and methods of SQL optimization, analyzes and infers the input target graph model, and generates the optimized target SQL statement.
[0071] It can be understood that step S03 is based on the preset SQL statement optimization model, fully utilizing the rich information in the target graph model to perform more accurate and effective optimization, and can generate more targeted and practical target SQL statements, thereby improving the execution efficiency and performance of SQL statements, so that the optimized SQL statements can achieve better expected results in the actual application environment, solving the problem of poor SQL statement optimization effect.
[0072] In a feasible implementation, in step S01, the original SQL statement is parsed to obtain key elements in the original SQL statement and the association relationships between the key elements. The steps of constructing an initial graph model based on the key elements and the association relationships include steps A01 to A03:
[0073] Step A01: Decompose the original SQL statement into grammatical units and construct an abstract syntax tree based on the grammatical structure relationship between the grammatical units;
[0074] It should be noted that the system performs lexical analysis and syntax analysis on the original SQL statement. Lexical analysis is the first step in SQL parsing. Its core task is to decompose the character sequence in the original SQL statement into a series of meaningful lexical units (also called word symbols, tokens). Lexical units are the smallest and meaningful character sequences in the source program, which can include keywords (such as SELECT, FROM, WHERE in SQL instructions, etc.), identifiers (such as table names, column names, etc.), operators (such as =, >, <, etc.), separators (such as commas, brackets, etc.) and constants (such as numbers, strings, etc.). The system uses a lexical analyzer to scan the original SQL statement character by character, and identifies the character sequence in the original SQL statement and converts the character sequence into corresponding lexical units according to predefined lexical rules. The predefined lexical rules are the basis for the lexical analyzer to identify and parse the character sequence in the SQL statement. They define the standard of character sequences that can constitute valid lexical units. For example, they may include keyword rules, identifier rules, operator rules, etc. The keyword rules define reserved words in the SQL language, such as SELECT, FROM, WHERE, INSERT, UPDATE, DELETE, JOIN, ON, AND, OR, NOT, etc.; the identifier rules define the rules for user-defined names such as table names, column names, view names, index names, etc.; the operator rules define the operators used in SQL statements, such as equal to (=), not equal to (<> or !=), greater than (>), less than (<), greater than or equal to (>=), less than or equal to (<=), LIKE, IN, BETWEEN, IS NULL, IS NOT NULL, etc.
[0075] In addition, it should be noted that after lexical analysis, the parser will take over these lexical units and combine them into grammatical units with grammatical meaning according to the grammatical rules of SQL. A grammatical unit is a unit composed of lexical units with specific grammatical meaning. It can be an expression, a clause, or an entire SQL statement. The goal of grammatical analysis is to construct a representation that can accurately reflect the grammatical structure of the original SQL statement.
[0076] In addition, it should be noted that the steps of parsing the original SQL statement can be implemented by customizing SQLGlot. SQLGlot is a dependency-free SQL parser, converter, optimizer, and execution engine written in Python. It supports user customization to achieve different functions.
[0077] In addition, it should be noted that the Abstract Syntax Tree (AST) is a tree data structure used to represent the grammatical structure of program source code. Each node in the tree represents a grammatical structure in the program, such as an expression, statement, function definition, etc. In the process of constructing the abstract syntax tree, the system obtains the grammatical structure relationship between grammatical units. The grammatical structure relationship refers to the logical relationship between each grammatical unit in the SQL statement, including the superior-subordinate relationship, the inclusion relationship, etc. According to the grammatical structure relationship between grammatical units, the grammatical units are organized into a tree structure, in which the root node represents the entire SQL statement, the internal nodes represent grammatical units (such as SELECT clause, FROM clause, etc.), and the leaf nodes represent specific lexical units (such as table name, column name, etc.).
[0078] In addition, it should be noted that after constructing the abstract syntax tree, the unreferenced subtrees in the abstract syntax tree are detected and deleted, where the unreferenced subtrees are the computed column subtrees that are not referenced by the SELECT clause or the FILTER clause. Isomorphic pattern recognition is performed on the continuously nested logical operator branches in the abstract syntax tree to determine the same logical structure in the abstract syntax tree. When the number of repetitions of the same logical structure exceeds the preset repetition threshold, the continuously nested logical operator branches are merged into a single super node.
[0079] Step A02: extracting key elements in the original SQL statement and the association relationships between the key elements from the abstract syntax tree;
[0080] It should be noted that key elements usually include table names, column names, predicates (such as conditional expressions), join conditions (such as the ON clause in the JOI N operation), aggregate functions (such as SUM, AVG, etc.), grouping conditions (such as the GROUP BY clause), sorting conditions (such as the ORDER BY clause), etc. These elements exist as specific node types in the AST. The system uses the Depth-First Search (DFS) or Breadth-First Search (BFS) algorithm to traverse the AST to access each node in the abstract syntax tree. During the traversal process, key elements are identified based on the node type of the node. For example, a node type of "TableReference" represents a table name, a node type of "ColumnReference" represents a column name, and a node type of "Bi naryPred icate" represents a predicate condition, etc. After identifying the nodes where the key elements are located, it is necessary to extract specific element values from these nodes. For example, if the key element is a table name or column name, directly extract the text value from the corresponding node; if the key element is a predicate condition, further parse the node's child nodes to extract the complete conditional expression, etc.
[0081] In addition, it should be noted that in AST, the association relationship between key elements is usually reflected through the parent-child relationship or sibling relationship of the node, and the association relationship includes parent-child relationship and sibling relationship. Among them, the parent-child relationship refers to a node directly containing another node as its child node. In the AST of the SQL statement, this relationship represents the inclusion or ownership relationship between elements. For example, in a SELECT statement, the SELECT node may have one or more child nodes, each child node represents a selected column, and the parent-child relationship between the SELECT node and its child nodes (column name nodes) indicates that these columns belong to the SELECT statement; in a FROM clause, the FROM node may have one or more child nodes, each child node represents a table (TableReference node), and the parent-child relationship indicates that these tables are the data source of the SELECT statement. After determining the key element, by traversing the AST, checking the child nodes of the key element, and determining the inclusion or ownership relationship between these child nodes and the key element based on the parent-child relationship.
[0082] A sibling relationship refers to the relationship between nodes with the same parent node. In the AST of a SQL statement, a sibling relationship indicates a parallel or collaborative relationship between elements. In a JOIN operation, there may be two TableReference nodes as sibling nodes, which share a parent node (the node representing the JOIN operation). The sibling relationship indicates that the two tables are connected through the JOIN operation. In a WHERE clause, there may be multiple BinaryPredict nodes as sibling nodes, which together constitute the query conditions. The sibling relationship indicates that these conditions are parallel and need to be met simultaneously. After determining the key element, by traversing the AST, find the sibling nodes that share the same parent node with the key element. Based on the sibling relationship and the operation represented by the parent node (such as JOIN, WHERE, etc.), the parallel or collaborative relationship between these sibling nodes and the key element is determined.
[0083] Step A03: Map the key elements to graph nodes, and construct connection edges of the graph nodes according to the association relationships to obtain an initial graph model.
[0084] It should be noted that the system maps the extracted key elements into graph nodes, each graph node represents a key element, and constructs the connecting edges of the graph nodes based on the association relationship between the key elements to form a graph structure, where the graph nodes are the nodes representing the key elements in the initial graph model, and the connecting edges are the edges representing the association relationship between the key elements in the initial graph model.
[0085] In this implementation, through lexical analysis and syntax parsing, the system can accurately understand the composition and structure of SQL statements, laying the foundation for building an abstract syntax tree and extracting key elements in subsequent steps. The construction of AST enables the system to clearly understand the structure and semantic relationships of SQL statements, providing strong support for the subsequent extraction of key elements and construction of the initial graph model. By extracting key elements and association relationships, the necessary data foundation is provided for the subsequent construction of the initial graph model, enabling the system to more accurately capture the semantic and structural information of SQL statements. Through the construction of the initial graph model, the system can comprehensively capture the semantic information and structural features of SQL statements, providing comprehensive and accurate context information for subsequent optimization. The optimization suggestions based on the initial graph model are more in line with actual needs, have stronger pertinence and practicality, thereby significantly improving the accuracy and practicality of SQL optimization.
[0086] Based on the first embodiment of the present application, in the second embodiment of the present application, the same or similar contents as those in the above embodiment 1 can be referred to the above introduction and will not be described in detail later. Figure 2 In step S03, the preset SQL statement optimization model includes a graph neural network and a large language model. The target graph model is analyzed based on the preset SQL statement optimization model to optimize the original SQL statement. The step of obtaining the target SQL statement includes steps S11 to S14:
[0087] Step S11, extracting non-text features in the target graph model based on the graph neural network;
[0088] It should be noted that Graph Neural Networks (GNN) is a deep learning model used to process graph-structured data. It extracts the topological relationship between structured input data by learning the potential representation vectors of nodes and their connection relationships in the graph. GNN iteratively aggregates and updates the features of nodes in the graph, capturing the node logical features and node relationship features between nodes, thereby extracting non-text features in the target graph model. Non-text features refer to the structured information represented by nodes (such as tables and columns) and edges (such as connection relationships) in the graph model. This information cannot be directly obtained from the SQL text, but is crucial for understanding the semantics and structure of SQL statements.
[0089] Large language models (LLMs) are deep learning-based natural language processing models, typically with billions or even hundreds of billions of parameters. By learning from large amounts of text data (such as books, web pages, and news articles), these models can generate natural language text and perform well in a variety of natural language processing tasks. For example, they can be used for text generation, machine translation, question-answering systems, and text classification. Target SQL statements are optimized SQL queries that improve query efficiency and performance by adjusting query structure, leveraging indexes, and reducing data scans while maintaining the original query intent.
[0090] Step S12: extracting text features from the original SQL statement and element information based on the large language model;
[0091] It should be noted that large language models (LLMs) are natural language processing models based on deep learning, which usually have billions or even hundreds of billions of parameters. These models can generate natural language text by learning large amounts of text data (such as books, web pages, news articles, etc.) and perform well in a variety of natural language processing tasks. For example, they can be used for tasks such as text generation, machine translation, question-answering systems, and text classification. LLMs use their deep neural network structure to extract text features from original SQL statements and element information. Text features refer to semantic information extracted from SQL statements and element information in text form, including keywords, phrases, syntactic structures, etc. These features are crucial for understanding the intent and context of SQL statements.
[0092] Step S13, concatenating non-text features and text features to obtain a fused feature vector;
[0093] It should be noted that the system performs vector concatenation on the non-text features extracted in step S11 and the text features extracted in step S12 to form a fused feature vector. The fused feature vector is a multi-dimensional vector, in which each dimension represents a feature of the SQL statement.
[0094] In step S14, the fused feature vector is input into the large language model to generate modified SQL statement content, and the original SQL statement is adjusted based on the modified SQL statement content to obtain an optimized target SQL statement.
[0095] It should be noted that the system inputs the fused feature vector generated in step S13 into the decoding layer of the large language model. The decoding layer decodes the fused feature vector according to the preset SQL grammar rules and the knowledge learned by the model to generate SQL statement modification content. The SQL statement modification content refers to a series of specific optimization suggestions and operations for the original SQL statement generated by the large language model based on the fused feature vector. It can be specific optimization suggestions such as adding indexes, adjusting query conditions, and optimizing connection operations. Based on the generated SQL statement modification content, the original SQL statement is adjusted, such as adding index hints and modifying query conditions in the original SQL statement, and finally the optimized target SQL statement is obtained.
[0096] In addition, it should be noted that when generating the target SQL statement, it is also necessary to consider whether the SQL statement complies with the preset SQL syntax rules. The preset SQL syntax rules are rules used to guide the generation of SQL statements. They can include keyword usage rules, table name and column name specifications, data type matching rules, etc. Among them, keyword usage rules define the keywords that must be used in SQL statements, such as SELECT, FROM, WHERE, JOIN, GROUP BY, HAVING, ORDER BY, and specify their correct usage location and order; table name and column name specifications specify the naming rules for table and column names, such as not using reserved words, not starting with numbers, and length restrictions; data type matching rules define the compatibility rules between different data types, such as integer types cannot be directly compared or operated on with string types. The decoding layer uses these rules and the knowledge learned by the model to convert the fused feature vector into a statement that complies with SQL syntax specifications.
[0097] In this embodiment, through the preset graph neural network, the system can effectively extract non-text features from the target graph model. These features capture the deep semantics and structural information of the SQL statement, providing rich contextual information for subsequent optimization. Through the preset large language model, the system can extract text features from the original SQL statement and the preset table information. These features are combined with the non-text features extracted in step S11 to form a comprehensive understanding of the SQL statement. By splicing the extracted non-text features and text features, the system obtains a fused feature vector. The vector contains both text and non-text information of the SQL statement, providing a more comprehensive and accurate feature representation for subsequent optimization. By inputting the fused feature vector into the large language model and combining it with the preset SQL grammar rules, the system can generate SQL statement modification content. The generated SQL statement modification content can comprehensively consider structural information and semantic information, solving the problem of low quality optimization suggestions. The original SQL statement is adjusted based on the modification content, so that the optimized target SQL statement can better adapt to the database environment, improve the execution efficiency of the SQL statement, and solve the problem of poor SQL statement optimization effect in current SQL optimization technology.
[0098] In a feasible implementation, in step S11, the non-text features include node logic features and node relationship features, and the step of extracting the non-text features in the target graph model based on the preset graph neural network includes steps B01 to B03:
[0099] Step B01: extracting attribute data of graph nodes in the target graph model based on a preset graph neural network, embedding the attribute data, and obtaining node logical features;
[0100] It should be noted that the preset graph neural network first traverses each graph node in the target graph model and extracts its attribute data. Attribute data refers to the various attributes and information of the graph node, including the node type (such as table node, column node, etc.), the specific information of the node (such as table name, column name, data type, etc.) and other metadata related to the node (such as associated nodes, relationships with associated nodes, etc.). The system uses embedding technology to convert these attribute data into low-dimensional vector representations, namely node logical features. Node logical features are obtained through embedded representation, which are feature vectors reflecting the logical attributes and semantic relationships of graph nodes. Embedded representation is used to convert high-dimensional, sparse attribute data into low-dimensional, dense vector representations for subsequent processing and analysis.
[0101] Step B02: Obtain each adjacent node that has a connection edge with the graph node, aggregate the feature vectors of each adjacent node to generate adjacent node features, and concatenate the adjacent node features with the feature vectors of the graph node to obtain the node relationship features;
[0102] It should be noted that the system traverses each graph node in the target graph model and finds all adjacent nodes with connected edges to it. The system aggregates the feature vectors of these adjacent nodes to generate adjacent node features, where the feature vector is a vector representing the attributes of a graph node or an adjacent node, obtained through embedding representation. The adjacent node feature is obtained by aggregating the feature vectors of the adjacent nodes, and is a feature vector reflecting the attributes and relationships of the adjacent nodes. The system concatenates the adjacent node features with the feature vectors of the original graph nodes to obtain node relationship features. The node relationship features are obtained by concatenating the adjacent node features and the feature vectors of the original graph nodes, and are feature vectors reflecting the relationship between graph nodes.
[0103] Step B03: Merge the node logic features and the node relationship features to obtain the non-text features in the target graph model.
[0104] It should be noted that the node logical features obtained in step B01 and the node relationship features obtained in step B02 are vectored together, or the node logical features and the node relationship features are fused using methods such as weighted summation and attention mechanism to form non-text features in the target graph model. These non-text features include the logical attributes and relationship features of the nodes in the SQL statement.
[0105] In this embodiment, the system extracts attribute data of graph nodes through a preset graph neural network and performs embedded representation. This allows the system to capture the intrinsic logical features of graph nodes. These logical features provide an important basis for subsequent understanding of the semantics and structure of SQL statements, helping to generate more accurate optimization suggestions. By obtaining each adjacent node with a connected edge to the graph node and aggregating their feature vectors, the system can capture the relationship features between graph nodes. These relationship features reflect the complex associations between key elements in the SQL statement. After concatenating the adjacent node features with the feature vectors of the graph nodes, the resulting node relationship features provide more comprehensive contextual information for subsequent optimization, helping to generate optimization suggestions that better meet actual needs. By merging node logical features and node relationship features, the system can obtain non-text features in the target graph model. Non-text features integrate the intrinsic logical attributes of key elements in the SQL statement and the relationship features between them, providing a more comprehensive and accurate feature representation for subsequent optimization. Based on these non-text features, the system can more deeply understand the semantics and structure of the SQL statement, thereby generating more accurate and practical optimization suggestions, significantly improving the accuracy and practicality of SQL optimization.
[0106] In a feasible implementation, in step S12, the step of extracting text features from the original SQL statement and element information based on the large language model includes steps B11 to B14:
[0107] Step B11: converting grammatical units in the original SQL statement into a first word vector sequence based on the large language model;
[0108] It should be noted that the system uses a large language model to parse the grammatical units in the original SQL statement, converts these grammatical units into corresponding word vectors, and forms a first word vector sequence. The first word vector sequence is a sequence formed after converting the grammatical units in the original SQL statement into word vectors, which is used to represent the semantic information of the SQL statement. The word vector is the representation of words in the vector space, which can capture the semantic relationship between words.
[0109] Step B12: extracting metadata text corresponding to the element information, performing word segmentation on the metadata text, and converting it into a second word vector sequence;
[0110] It should be noted that the system extracts the corresponding metadata text from the element information. The metadata text is the text that describes the table information, such as the table name, column name, data type, etc. These metadata texts are segmented and split into smaller semantic units. The split semantic units are converted into corresponding word vectors to generate a second word vector sequence. The second word vector sequence is a sequence formed after the metadata text is segmented and converted into word vectors, which is used to represent the semantic information of the table metadata.
[0111] Step B13: concatenate the first word vector sequence and the second word vector sequence into an input sequence, and determine the context dependency of the word vectors in the input sequence through semantic association analysis;
[0112] It should be noted that the system concatenates the first word vector sequence and the second word vector sequence into a complete input sequence, and uses the semantic association analysis capability of the large language model to analyze the contextual dependencies between word vectors in the input sequence. The contextual dependencies reflect the relative position and semantic association of word vectors in the sequence, which helps the system to more accurately understand the semantic connection between SQL statements and table metadata.
[0113] Step B14: perform feature fusion on the input sequence based on the context dependency to obtain text features in the original SQL statement and element information.
[0114] It should be noted that the system performs feature fusion on the word vectors in the input sequence based on the context dependency determined in step B13. Feature fusion is the process of combining multiple feature vectors into a comprehensive feature vector, taking into account the semantic connection and contextual relationship between features, so that the fused feature vector can more comprehensively represent the text features in the original SQL statement and element information. Text features are feature vectors extracted from the original SQL statement and element information that reflect their semantic information.
[0115] In this embodiment, by generating a first word vector sequence, the system can represent the syntax and semantic information of the SQL statement in a numerical manner, providing a basis for subsequent semantic association analysis and feature fusion, helping the system to more accurately understand the intention and context of the SQL statement, thereby generating more accurate optimization suggestions. The generation of the second word vector sequence enables the system to represent the semantic features of the element information in a numerical manner and fuse it with the word vector sequence of the SQL statement, helping the system to more comprehensively understand the context of the SQL statement, thereby generating optimization suggestions that are more in line with actual needs. The determination of contextual dependencies enables the system to more accurately understand the semantic connection between the SQL statement and table information, thereby generating more reasonable optimization suggestions, helping to improve the accuracy and practicality of SQL optimization, and solving the problem of poor optimization effect in current SQL optimization technology. The generation of text features enables the system to more comprehensively understand the semantic features of SQL statements and table information, providing richer feature support for subsequent optimization, helping the system to generate more accurate and practical optimization suggestions, and significantly improving the effect of SQL optimization.
[0116] In a feasible implementation, in step S13, the step of combining non-text features and text features to obtain a fused feature vector includes steps B21 to B22:
[0117] Step B21, mapping the non-text features and the text features to the same dimensional space to obtain non-text features and text features of the same dimension;
[0118] It should be noted that the system performs dimensionality normalization on non-text features and text features. Dimensionality normalization refers to the process of mapping feature vectors of different dimensions to the same dimensional space through linear or nonlinear transformation. Dimensional space refers to the mathematical space where the feature vector is located, and its dimension determines the length and complexity of the feature vector.
[0119] Step B22: performing vector concatenation operation on non-text features and text features of the same dimension to generate a fused feature vector.
[0120] It should be noted that vector concatenation is the process of connecting two or more vectors of the same dimension in a certain order to form a new, higher-dimensional vector. The system performs vector concatenation on non-text features and text features of the same dimension. Through this vector concatenation, the non-text features and text features are combined into a new feature vector, called a fused feature vector.
[0121] In addition, it should be noted that before performing vector splicing operations on non-text features and text features of the same dimension, the system generates dynamic weight coefficients for the non-text features and text features respectively, and performs weighted fusion operations on the non-text features and text features according to the dynamic weight coefficients to obtain weighted fusion results. The weighted fusion results are used as residual terms superimposed on the fused feature vector, which can retain the potential correlation information between non-text features and text features.
[0122] In addition, the steps of the system generating dynamic weight coefficients for non-text features and text features respectively include: the system presets a weight matrix that matches the feature dimension, wherein the number of rows in the weight matrix is equal to the feature dimension corresponding to the text feature vector, and the number of columns in the weight matrix is equal to the feature dimension corresponding to the non-text feature vector, transposing the text feature vector into a row vector, and transposing the non-text feature vector into a column vector, calculating the correlation coefficient of the two types of features through matrix multiplication, performing absolute value sum calculation on the correlation coefficient matrix, normalizing the elements in the weight matrix to probability distribution values, and selecting the matrix element corresponding to the maximum value in the probability distribution value as the dynamic weight coefficient.
[0123] In this implementation, through dimensional normalization, the system addresses the problem that traditional SQL optimization techniques often overlook the dimensional differences between non-text features and text features, leading to information loss or redundancy during feature fusion. By using vector concatenation, the system organically combines non-text and text features to form a more comprehensive feature representation. This allows the system to more comprehensively capture the structural and semantic features of SQL statements, providing richer feature support for subsequent SQL optimization. This helps the system generate more accurate and practical optimization recommendations, significantly improving the effectiveness of SQL optimization.
[0124] In a feasible implementation, the SQL statement optimization method further includes steps B31 to B34:
[0125] Step B31: receiving a training data set, and for any training sample in the training data set, converting the original SQL statement sample in the training sample into a graph model sample;
[0126] It should be noted that the system receives a training data set containing original SQL statement samples and sample optimization results of the original SQL statements. These samples are used to train the model so that it can learn how to generate optimized SQL statements from original SQL statements. Among them, the original SQL statement samples are set with unoptimized SQL query statements as the input of the model, and the sample optimization results of the original SQL statements are query statements after optimizing the original SQL statements, which serve as the expected output of the model.
[0127] In addition, it should be noted that the training dataset can be implemented through large language model prompt engineering. Large language model prompt engineering refers to a method of guiding the model to generate more accurate and expected output by designing and optimizing the prompt words (prompt) input to the large language model. The overall processing flow is as follows: Figure 3 As shown, Figure 3 A data collection flow chart is provided, which includes four stages: graph structure construction, feature engineering, data verification, and valid data collection. After collecting the original SQL statements, the SQL information as well as index and table information are extracted. In the graph structure construction stage, an abstract syntax tree is generated based on the extracted information, which includes graph nodes (such as Table, Column, etc.) and edge relationships (such as JOIN, WHERE, etc.), and metadata is injected into it. The metadata can be index information, primary key tags, data types, and table data in the table to obtain a graph structure; in the feature engineering stage, text feature encoding and non-text feature encoding are performed on the graph structure. Specifically, text feature encoding is achieved by processing text features and extracting text features from the graph structure. Non-text feature encoding is achieved by traversing the nodes in the graph structure, encoding node type features, extracting metadata features, and processing edge features to finally obtain output features; data verification is performed on these output features, valid data is collected, and data cleaning is performed to obtain a cleaned data set.
[0128] Step B32: Input the original SQL statement sample, the graph model sample, and the element information sample in the database corresponding to the original SQL statement sample into the preset initial model to obtain the optimized SQL statement sample, wherein the graph neural network branch in the preset initial model is used to process the graph model sample, and the large language model branch in the preset initial model is used to process the original SQL statement sample and the element information sample in the database corresponding to the original SQL statement sample;
[0129] It should be noted that the graph model sample is the representation after the original SQL statement sample is converted into a graph structure. The system parses the SQL statement into a graph model containing key elements and association relationships to obtain a graph model sample. The system uses these graph model samples, original SQL statement samples, and element information samples in the database corresponding to the original SQL statement samples to train the preset initial model. The element information samples in the database corresponding to the original SQL statement samples are metadata information related to the database corresponding to the original SQL statement, such as table structure, index configuration, etc., which are used for model training. The preset initial model is a model containing a graph neural network branch and a large language model branch, which can be used to process graph structure samples and text information. The graph neural network branch is the part of the preset initial model used to process graph structure samples, which can extract non-text features in the graph. The large language model branch is the part of the preset initial model used to process text information, which can extract text features. The optimized SQL statement sample refers to the optimized SQL statement generated by the preset initial model during the training process, which is used to compare with the sample optimization results of the original SQL statement in the training sample.
[0130] Step B33: Calculate the difference loss between the optimized SQL statement sample and the sample optimization results of the original SQL statement in the training sample, and update the model parameters of the graph neural network branch and the large language model branch based on the difference loss;
[0131] It should be noted that the system compares the similarity between the optimized SQL statement samples and the sample optimization results of the original SQL statements in the training samples to calculate the difference loss between the optimized SQL statement samples and the sample optimization results of the original SQL statements in the training samples. The difference loss refers to the degree of difference between the sample optimization results of the optimized SQL statement samples and the original SQL statements in the training samples, which is used to measure the performance of the model. The system updates the model parameters of the graph neural network branch and the large language model branch by backpropagation based on the difference loss to reduce the difference between the sample optimization results of the predicted optimized SQL statement samples and the original SQL statements in the training samples. Model parameters refer to the parameters that need to be learned in the preset initial model, such as weights, biases, etc., which are used to adjust the output of the model.
[0132] Step B34: After the preset initial model reaches the preset training conditions, a preset SQL statement optimization model is obtained.
[0133] It should be noted that the system continues to train the preset initial model until the model meets the preset training conditions (such as reaching the maximum number of iterations, the difference loss is less than the threshold, etc.). When the model meets the preset training conditions, the system believes that the model has learned the ability to generate optimized SQL statements from the original SQL statements, and obtains the preset SQL statement optimization model. The preset SQL statement optimization model is a model obtained after training, which includes a preset graph neural network and a large language model, and can be used to optimize SQL statements.
[0134] For example, to help understand the technical concept or technical principle of this application, please refer to Figure 4 , Figure 4 A model training architecture diagram is provided, and table information and index information are collected from the original SQL data. Table information collection mainly focuses on the content of each table in the database, and index information collection mainly focuses on the relationship between each table in the database and the relationship between elements in the table. After the information collection is completed, a data set is generated, the data in the data set is cleaned and preprocessed, and the processed data is divided for model training. Finally, the trained model is deployed and applied.
[0135] For details, please refer to Figure 5 , Figure 5A model training flowchart is provided. For the graph neural network branch, the information of the node neighbors in the graph is aggregated based on GATConv (Graph Attention Network Convolution), and different weights are assigned to different neighbor nodes to capture local and global features in the graph structure. Layer Norm (Layer Normalization) is performed to normalize the input data of each layer, stabilize the training process and accelerate model convergence. Based on Global_add_pool (global sum pooling layer), the features of all nodes in the graph are added to obtain graph-level feature representation. These features are input into the graph neural network branch, linearly combined through Linear (linear transformation layer), normalized again by Layer Norm, and adjusted by expand (expansion operation). For the large language model branch, text formatting is first performed on the original SQL statement and preset metadata to ensure that they meet the specifications and requirements of subsequent processing. The formatted text is then segmented into discrete tokens using a Tokenizer to facilitate model understanding and processing. The feature representation of the text is then extracted in conjunction with the base model. The base model is a pre-trained large language model used to extract the feature representation of the text. The extracted features are input into the large language model branch, and feature transformation is performed using Layer Norm and Linear. Nonlinearity is then introduced into the features using the GELU (Gaussian Error Linear Unit) activation function. The features processed by the graph neural network branch and the large language model branch are concatenated (concat) to fuse the information of the two different modalities. The Linear transformation layer linearly combines the concatenated features to further adjust the feature representation. The nonlinear expression ability of the model is enhanced by the GELU activation function, and some neurons are randomly discarded (Dropout) to prevent the model from overfitting. The importance of different features in the fusion process is adjusted in combination with the fusion parameters (Fusion Params). Finally, the fused features are layer-normalized based on the Layer Norm to stabilize the feature distribution.
[0136] In addition, it should be noted that after data collection and model building are completed, the model is trained using the AdamW optimizer. AdamW is an improvement on the Adam optimization algorithm. It combines weight decay (L2 regularization) with the adaptive learning rate adjustment mechanism of the Adam optimizer. Weight decay is a regularization technique used to prevent model overfitting. Weight decay is achieved by adding the L2 norm term of the model weight to the loss function during the optimization process. At the same time, a warmup strategy is set to optimize the dynamic adjustment of the learning rate. The warmup strategy refers to gradually increasing the learning rate in the initial stage of training instead of using a larger learning rate for training from the beginning. This can help the model converge better in the early stages of training and avoid training instability or model divergence due to excessive learning rate. During training, the initial learning rate can be set to 2e-5, the loss function uses cross entropy loss, and the training cycle is set to 200.
[0137] In this embodiment, by receiving a training data set containing original SQL statement samples and sample optimization results of the original SQL statements, the system can use this data to train the model and improve the optimization effect. By converting the original SQL statement samples into graph model samples and using the graph neural network branch and the large language model branch in the preset initial model to process this information respectively, the system can more comprehensively understand the semantics and structural characteristics of the SQL statement and improve the optimization effect. By calculating the difference loss and updating the model parameters, the system can continuously adjust and optimize the model to improve the accuracy of SQL optimization. By training the preset initial model until the preset training conditions are met, the system can obtain a fully trained preset SQL statement optimization model and improve the stability and accuracy of SQL optimization.
[0138] It should be noted that the above examples are only used to understand the present application and do not constitute a limitation on the SQL statement optimization method of the present application. More simple transformations based on this technical concept are all within the scope of protection of the present application.
[0139] This application also provides a SQL statement optimization system, please refer to Figure 6 , the SQL statement optimization system includes:
[0140] The initial graph model generation module 10 is used to parse the original SQL statement, obtain the key elements in the original SQL statement and the association relationship between the key elements, and construct the initial graph model based on the key elements and the association relationship;
[0141] The target graph model generation module 20 is used to add the element information in the database corresponding to the original SQL statement to the initial graph model to obtain the target graph model;
[0142] The SQL statement optimization module 30 is used to analyze the target graph model based on a preset SQL statement optimization model to optimize the original SQL statement to obtain a target SQL statement.
[0143] Optionally, the graph model generation module 10 is further configured to:
[0144] Decompose the original SQL statement into grammatical units and construct an abstract syntax tree based on the grammatical structure relationship between the grammatical units;
[0145] Extract key elements in the original SQL statement and the relationship between key elements from the abstract syntax tree;
[0146] The key elements are mapped to graph nodes, and the connection edges of the graph nodes are constructed according to the association relationships to obtain the initial graph model.
[0147] Optionally, the preset SQL statement optimization model includes a graph neural network and a large language model, and the SQL statement optimization module 30 is further used to:
[0148] Extract non-text features from the target graph model based on graph neural network;
[0149] Extract text features from original SQL statements and element information based on a large language model;
[0150] Concatenate non-text features and text features to obtain a fused feature vector;
[0151] The fused feature vector is input into the large language model to generate the modified SQL statement content. The original SQL statement is adjusted based on the modified SQL statement content to obtain the optimized target SQL statement.
[0152] Optionally, the non-text features include node logic features and node relationship features. The SQL statement optimization module 30 is further configured to:
[0153] Extract the attribute data of the graph nodes in the target graph model based on the preset graph neural network, embed the attribute data and obtain the node logical features;
[0154] Obtain each adjacent node that has a connection edge with the graph node, aggregate the feature vectors of each adjacent node to generate the adjacent node feature, and concatenate the adjacent node feature with the feature vector of the graph node to obtain the node relationship feature;
[0155] Merge node logical features and node relationship features to obtain non-text features in the target graph model.
[0156] Optionally, the SQL statement optimization module 30 is further configured to:
[0157] Convert the grammatical units in the original SQL statement into the first word vector sequence based on the large language model;
[0158] Extract the metadata text corresponding to the element information, perform word segmentation on the metadata text, and convert it into a second word vector sequence;
[0159] The first word vector sequence and the second word vector sequence are concatenated into an input sequence, and the context dependency of the word vectors in the input sequence is determined through semantic association analysis.
[0160] The input sequence is feature fused based on context dependencies to obtain the original SQL statement and text features in element information.
[0161] Optionally, the SQL statement optimization module 30 is further configured to:
[0162] Mapping non-text features and text features to the same dimensional space to obtain non-text features and text features of the same dimension;
[0163] Perform vector concatenation operations on non-text features and text features of the same dimension to generate a fused feature vector.
[0164] Optionally, the SQL statement optimization system includes a model training module 40, which is used to:
[0165] Receive a training data set, and for any training sample in the training data set, convert the original SQL statement sample in the training sample into a graph model sample;
[0166] Input the original SQL statement samples, the graph model samples, and the element information samples in the database corresponding to the original SQL statement samples into the preset initial model to obtain the optimized SQL statement samples, wherein the graph neural network branch in the preset initial model is used to process the graph model samples, and the large language model branch in the preset initial model is used to process the original SQL statement samples and the element information samples in the database corresponding to the original SQL statement samples;
[0167] Calculate the difference loss between the optimized SQL statement sample and the sample optimization results of the original SQL statement in the training sample, and update the model parameters of the graph neural network branch and the large language model branch based on the difference loss;
[0168] After the preset initial model reaches the preset training conditions, a preset SQL statement optimization model is obtained.
[0169] The SQL statement optimization system provided in this application utilizes the SQL statement optimization method described in the aforementioned embodiments to address the technical issue of poor SQL statement optimization results. Compared to the prior art, the beneficial effects of the SQL statement optimization system provided in this application are the same as those of the SQL statement optimization method described in the aforementioned embodiments. Other technical features of the SQL statement optimization system are the same as those disclosed in the aforementioned embodiments and are not further elaborated upon here.
[0170] The present application provides an electronic device, comprising: at least one processor; and a memory communicatively connected to the at least one processor; wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the at least one processor so that the at least one processor can execute the SQL statement optimization method in the above-mentioned embodiment 1.
[0171] Reference below Figure 7 , which shows a schematic diagram of the structure of an electronic device suitable for implementing the embodiments of the present application. The electronic devices in the embodiments of the present application may include, but are not limited to, mobile terminals such as mobile phones, laptop computers, PADs (Portable Application Description: tablet computers), etc., and fixed terminals such as digital TVs and desktop computers. Figure 7 The electronic device shown is merely an example and should not limit the functions and scope of use of the embodiments of the present application.
[0172] like Figure 7As shown, the electronic device may include a processing device 1001 (e.g., a central processing unit, a graphics processing unit, etc.), which can perform various appropriate actions and processes based on programs stored in a read-only memory 1002 or programs loaded from a storage device 1003 into a random access memory 1004. The random access memory 1004 also stores various programs and data required for the operation of the electronic device. The processing device 1001, the read-only memory 1002, and the random access memory 1004 are connected to each other via a bus 1005. An input / output interface 1006 is also connected to the bus. Typically, the following systems may be connected to the input / output interface 1006: an input device 1007 including, for example, a touch screen, a touchpad, a keyboard, a mouse, an image sensor, a microphone, an accelerometer, a gyroscope, etc.; an output device 1008 including, for example, a liquid crystal display (LCD), a speaker, a vibrator, etc.; a storage device 1003 including, for example, a magnetic tape or a hard disk; and a communication device 1009. The communication device 1009 may allow the electronic device to communicate with other devices wirelessly or wired to exchange data. Although the figures show electronic devices with various systems, it should be understood that it is not required to implement or have all the systems shown. More or fewer systems may be implemented or have instead.
[0173] In particular, according to the embodiments disclosed in the present application, the processes described above with reference to the flowcharts can be implemented as computer software programs. For example, the embodiments disclosed in the present application include a computer program product comprising a computer program carried on a computer-readable medium, the computer program comprising program code for executing the method shown in the flowchart. In such an embodiment, the computer program can be downloaded and installed from a network via a communication device, or installed from a storage device 1003, or installed from a read-only memory 1002. When the computer program is executed by the processing device 1001, the above-mentioned functions defined in the method of the embodiment disclosed in the present application are performed.
[0174] The electronic device provided in this application utilizes the SQL statement optimization method of the above-described embodiment to resolve the technical problem of poor SQL statement optimization results. Compared to the prior art, the beneficial effects of the electronic device provided in this application are the same as those of the SQL statement optimization method provided in the above-described embodiment. Other technical features of the electronic device are the same as those disclosed in the above-described embodiment and are not further described here.
[0175] It should be understood that the various parts disclosed in this application can be implemented using hardware, software, firmware, or a combination thereof. In the description of the above embodiments, specific features, structures, materials, or characteristics can be combined in any one or more embodiments or examples in a suitable manner.
[0176] The above description is merely a specific embodiment of the present application, but the scope of protection of the present application is not limited thereto. Any changes or substitutions that can be easily conceived by a person skilled in the art within the technical scope disclosed in this application should be included in the scope of protection of this application. Therefore, the scope of protection of this application should be based on the scope of protection of the claims.
[0177] The present application provides a computer-readable storage medium having computer-readable program instructions (ie, computer program) stored thereon, and the computer-readable program instructions are used to execute the SQL statement optimization method in the above embodiment.
[0178] The computer-readable storage medium provided herein may be, for example, a USB flash drive, but is not limited to electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems or devices, or any combination thereof. More specific examples of computer-readable storage media may include, but are not limited to, an electrical connection having one or more wires, a portable computer disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, a portable compact disk read-only memory (CD-ROM), an optical storage device, a magnetic storage device, or any suitable combination thereof. In this embodiment, the computer-readable storage medium may be any tangible medium that contains or stores a program that can be used by or in conjunction with an instruction execution system or device. The program code contained on the computer-readable storage medium may be transmitted using any suitable medium, including but not limited to: wires, optical cables, RF (Radio Frequency), etc., or any suitable combination thereof.
[0179] The computer-readable storage medium may be included in the electronic device, or may exist independently without being incorporated into the electronic device.
[0180] The above-mentioned computer-readable storage medium carries one or more programs. When the above-mentioned one or more programs are executed by an electronic device, the SQL statement optimization device: parses the original SQL statement, obtains the key elements in the original SQL statement and the association relationship between the key elements, and constructs an initial graph model based on the key elements and the association relationship; adds the element information in the database corresponding to the original SQL statement to the initial graph model to obtain a target graph model; analyzes the target graph model based on a preset SQL statement optimization model to optimize the original SQL statement and obtain a target SQL statement.
[0181] Computer program code for performing the operations of the present application may be written in one or more programming languages, or a combination thereof, including object-oriented programming languages such as Java, Smalltalk, C++, and conventional procedural programming languages such as "C" or similar programming languages. The program code may be executed entirely on the user's computer, partially on the user's computer, as a stand-alone software package, partially on the user's computer and partially on a remote computer, or entirely on the remote computer or server. In cases involving a remote computer, the remote computer may be connected to the user's computer through any type of network, including a local area network (LAN) or a wide area network (WAN), or may be connected to an external computer (e.g., through the Internet using an Internet service provider).
[0182] The flow charts and block diagrams in the accompanying drawings illustrate the possible architecture, functions and operations of the systems, methods and computer program products according to various embodiments of the present application. In this regard, each box in the flow chart or block diagram can represent a module, program segment or a part of code, and the module, program segment or a part of code contains one or more executable instructions for realizing the specified logical function. It should also be noted that in some alternative implementations, the functions marked in the box can also occur in a different order than that marked in the accompanying drawings. For example, two boxes represented in succession can actually be executed substantially in parallel, and they can sometimes be executed in the opposite order, depending on the functions involved. It should also be noted that each box in the block diagram and / or flow chart, and the combination of the boxes in the block diagram and / or flow chart can be implemented by a dedicated hardware-based system that performs the specified function or operation, or can be implemented by a combination of dedicated hardware and computer instructions.
[0183] The modules described in the embodiments of the present application may be implemented in software or hardware, wherein the name of a module does not necessarily limit the unit itself.
[0184] The computer-readable storage medium provided in this application stores computer-readable program instructions (i.e., a computer program) for executing the aforementioned SQL statement optimization method, thereby resolving the technical issue of poor SQL statement optimization results. Compared to the prior art, the beneficial effects of the computer-readable storage medium provided in this application are similar to those of the SQL statement optimization method provided in the aforementioned embodiments, and are not further elaborated here.
[0185] The present application also provides a computer program product, including a computer program, which implements the steps of the above-mentioned SQL statement optimization method when executed by a processor.
[0186] The computer program product provided in this application can solve the technical problem of poor SQL statement optimization effect. Compared with the prior art, the beneficial effects of the computer program product provided in this application are the same as the beneficial effects of the SQL statement optimization method provided in the above embodiment, which will not be repeated here.
[0187] The above description is only part of the embodiments of the present application and does not limit the patent scope of the present application. All equivalent structural transformations made by using the contents of the present application specification and drawings under the technical concept of the present application, or direct / indirect application in other related technical fields are included in the patent protection scope of the present application.
Claims
1. A SQL statement optimization method, characterized in that: The SQL statement optimization method includes: Parsing the original SQL statement to obtain key elements in the original SQL statement and the association relationships between the key elements, and constructing an initial graph model based on the key elements and the association relationships; Add the element information in the database corresponding to the original SQL statement to the initial graph model to obtain a target graph model; The target graph model is analyzed based on a preset SQL statement optimization model to optimize the original SQL statement to obtain a target SQL statement.
2. The SQL statement optimization method according to claim 1, wherein: The steps of parsing the original SQL statement, obtaining key elements in the original SQL statement and the association relationships between the key elements, and constructing an initial graph model based on the key elements and the association relationships include: Decomposing the original SQL statement into grammatical units, and constructing an abstract syntax tree based on the grammatical structure relationship between the grammatical units; Extracting key elements in the original SQL statement and the association relationships between the key elements from the abstract syntax tree; The key elements are mapped into graph nodes, and connection edges of the graph nodes are constructed according to the association relationships to obtain an initial graph model.
3. The SQL statement optimization method according to claim 1, wherein: The preset SQL statement optimization model includes a graph neural network and a large language model. The step of analyzing the target graph model based on the preset SQL statement optimization model to optimize the original SQL statement to obtain the target SQL statement includes: Extracting non-text features in the target graph model based on the graph neural network; Extracting text features from the original SQL statement and the element information based on the large language model; splicing the non-text features and the text features to obtain a fused feature vector; The fused feature vector is input into the large language model to generate SQL statement modification content, and the original SQL statement is adjusted based on the SQL statement modification content to obtain an optimized target SQL statement.
4. The SQL statement optimization method according to claim 3, wherein: The non-text features include node logic features and node relationship features. The step of extracting the non-text features in the target graph model based on a preset graph neural network includes: Extracting attribute data of graph nodes in the target graph model based on a preset graph neural network, embedding the attribute data, and obtaining logical features of the nodes; Obtaining adjacent nodes that have edges connected to the graph node, aggregating the feature vectors of the adjacent nodes to generate adjacent node features, and concatenating the adjacent node features with the feature vector of the graph node to obtain the node relationship feature; The node logical features and the node relationship features are combined to obtain non-text features in the target graph model.
5. The SQL statement optimization method according to claim 3, wherein: The step of extracting text features from the original SQL statement and the element information based on the large language model includes: Converting grammatical units in the original SQL statement into a first word vector sequence based on the large language model; Extracting metadata text corresponding to the element information, performing word segmentation on the metadata text, and converting it into a second word vector sequence; Concatenate the first word vector sequence and the second word vector sequence into an input sequence, and determine the context dependency of the word vectors in the input sequence through semantic association analysis; Feature fusion is performed on the input sequence based on the context dependency to obtain text features in the original SQL statement and the element information.
6. The SQL statement optimization method according to claim 3, wherein: The step of combining the non-text features and the text features to obtain a fused feature vector includes: Mapping the non-text features and the text features to the same dimensional space to obtain non-text features and text features of the same dimension; A vector concatenation operation is performed on the non-text features and text features of the same dimension to generate a fused feature vector.
7. The SQL statement optimization method according to claim 1, wherein: The SQL statement optimization method further includes: A training data set is received, and for any training sample in the training data set, an original SQL statement sample in the training sample is converted into a graph model sample; Inputting the original SQL statement sample, the graph model sample, and the element information sample in the database corresponding to the original SQL statement sample into a preset initial model to obtain an optimized SQL statement sample, wherein the graph neural network branch in the preset initial model is used to process the graph model sample, and the large language model branch in the preset initial model is used to process the original SQL statement sample and the element information sample in the database corresponding to the original SQL statement sample; Calculating the difference loss between the optimized SQL statement sample and the sample optimization result of the original SQL statement in the training sample, and updating the model parameters of the graph neural network branch and the large language model branch based on the difference loss; After the preset initial model meets the preset training conditions, a preset SQL statement optimization model is obtained.
8. An electronic device, characterized in that: The device includes: a memory, a processor, and a computer program stored in the memory and executable on the processor, wherein the computer program is configured to implement the steps of the SQL statement optimization method according to any one of claims 1 to 7.
9. A storage medium, characterized in that: The storage medium is a computer-readable storage medium, and a computer program is stored on the storage medium. When the computer program is executed by a processor, the steps of the SQL statement optimization method according to any one of claims 1 to 7 are implemented.
10. A computer program product, characterized in that The computer program product comprises a computer program, and when the computer program is executed by a processor, the steps of the SQL statement optimization method according to any one of claims 1 to 7 are implemented.
Citation Information
Cited By
SQL statement correction method and related device based on metadata graph
CN122412454A