A SQL statement optimization method and device based on dictionary
By generating a dictionary of interpretation and using it to optimize SQL statements, the problem of low accuracy in SQL statement generation in the prior art is solved, and higher semantic understanding and SQL statement accuracy are achieved.
Patent Information
- Application Number
- CN202510246031.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2025-03-04
- Publication Date
- 2025-05-06
- Estimated Expiration
- 2045-03-04
AI Technical Summary
Existing SQL statement generation techniques are difficult to capture semantic details and business attributes in user problems, resulting in low accuracy of generated SQL statements.
By obtaining the initial association relationship between the subscene text data and the scene database, extracting keywords and update association relationships, matching keywords and specified data to filter target data, fusing text data and association relationships to generate a dictionary, and optimizing SQL statements using the dictionary.
Improve the model's understanding in specific scenarios, improve the accuracy of SQL statements, and the generated SQL statements are more accurate.
Smart Images

Figure CN119739738B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of artificial intelligence technology, and in particular to a dictionary-based SQL statement optimization method and device. Background Art
[0002] SQL, as the core language for data query and processing, is widely used in database management systems. Users can interact with databases by writing SQL statements to query and process data. With the increasing demand for intelligence, technologies that convert natural language into SQL statements have emerged. Text2sql is a technology that converts natural language requests into SQL statements. It can design prompt words for user text and valid database information, and input the prompt words into a large language model to obtain the corresponding SQL statement.
[0003] However, although the current SQL statement generation technology introduces database information when generating SQL statements, due to the limited understanding ability of the model, the understanding of database information is superficial and lacks in-depth understanding of specific scenarios. It is difficult to capture the semantic details and business attributes in user questions, which leads to low accuracy of generated SQL statements. Summary of the invention
[0004] In view of the above problems, the purpose of the present invention is to provide a dictionary-based SQL statement optimization method and device, which can generate an interpretation dictionary in a specific scenario to improve the understanding of the model in the specific scenario, so as to improve the accuracy of the SQL statement.
[0005] In order to solve the above technical problems, the present invention provides the following technical solutions:
[0006] In one aspect, the present invention provides a dictionary-based SQL statement method, comprising:
[0007] Acquire an initial association relationship between each sub-scene text data and a scene database, wherein the sub-scene text data is obtained by segmenting the scene text data in the specified scene;
[0008] For each sub-scenario text data, extract keywords from the sub-scenario text data, and update the initial association relationship to a first association relationship using the keywords;
[0009] Matching the keyword with designated data to filter out target data from the designated data and update the target data into the first association relationship to obtain a second association relationship, wherein the designated data is data extracted from the scene database;
[0010] The scene text data and the second association relationship are integrated to generate interpretation data corresponding to the target data, thereby obtaining an interpretation dictionary;
[0011] The SQL statement to be optimized is optimized by using the interpretation dictionary to generate a target SQL statement, wherein the SQL statement to be optimized is generated based on the query data in the specified scenario.
[0012] On the other hand, the present invention also provides a dictionary-based SQL statement optimization device, which is used to implement any of the above methods, and the device includes:
[0013] An acquisition module, used to acquire an initial association relationship between each sub-scene text data and a scene database, wherein the sub-scene text data is obtained by segmenting the scene text data in the specified scene;
[0014] A first association module is used to extract keywords from each sub-scene text data, and update the initial association relationship to a first association relationship with the keyword;
[0015] A second association module is used to match the keyword with designated data to filter out target data from the designated data and update the target data to the first association relationship to obtain a second association relationship, wherein the designated data is data extracted from the scene database;
[0016] A paraphrase module, used for fusing the scene text data with the second association relationship, generating paraphrase data corresponding to the target data, and obtaining a paraphrase dictionary;
[0017] The optimization module is used to optimize the SQL statement to be optimized by using the interpretation dictionary to generate a target SQL statement, wherein the SQL statement to be optimized is generated based on the query data in the specified scenario.
[0018] On the other hand, the present invention further provides an electronic device, comprising a processor and a memory, wherein the memory stores a plurality of instructions; the processor loads instructions from the memory to execute the steps in any one of the dictionary-based SQL statement optimization methods provided by the present invention.
[0019] On the other hand, the present invention further provides a computer-readable storage medium storing a plurality of instructions suitable for loading by a processor to execute the steps in any one of the dictionary-based SQL statement optimization methods provided by the present invention.
[0020] On the other hand, the present invention further provides a computer program product, including a computer program / instruction, which implements the steps of any dictionary-based SQL statement optimization method provided by the present invention when executed by a processor.
[0021] The beneficial effects brought about by the technical solution provided by the present invention include at least:
[0022] The embodiment of the present invention can obtain the initial association relationship between the sub-scenario text data and the scene database, update the initial association relationship to a first association relationship using keywords extracted from the sub-scenario text data, match the keywords and designated data to determine the target data, and use the target data to update the first association relationship to a second association relationship, fuse the scene text data and the second association relationship, generate interpretation data corresponding to the target data, obtain a scene-related interpretation dictionary, and finally use the interpretation dictionary to improve the semantic understanding of the model in a specific scene, optimize the SQL statement to be optimized, and generate a more accurate target SQL statement. BRIEF DESCRIPTION OF THE DRAWINGS
[0023] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, the drawings required for use in the description of the embodiments will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative work.
[0024] Figure 1 It is a schematic diagram of an application scenario of a dictionary-based SQL statement optimization method provided by an embodiment of the present invention;
[0025] Figure 2 It is a flowchart of a dictionary-based SQL statement optimization method provided by an embodiment of the present invention;
[0026] Figure 3 is a schematic diagram of updating a first association relationship to a second association relationship provided by an embodiment of the present invention;
[0027] Figure 4 is a schematic diagram of generating a definition dictionary provided by an embodiment of the present invention;
[0028] Figure 5 It is a structural schematic diagram of a dictionary-based SQL statement optimization device provided by an embodiment of the present invention;
[0029] Figure 6 It is a schematic diagram of the structure of an electronic device provided by an embodiment of the present invention. DETAILED DESCRIPTION
[0030] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by those skilled in the art without creative work are within the scope of protection of the present invention.
[0031] The present invention provides a dictionary-based SQL statement optimization method, which can construct an interpretation dictionary for a specific scenario to enhance the understanding ability of a model in a specific scenario, so as to optimize the SQL statement into a more accurate target SQL statement.
[0032] It should be noted that in the specific implementation of the present invention, data related to user information, etc., needs to obtain user permission or consent, and the collection, use and processing of relevant data need to comply with relevant laws, regulations and standards of relevant countries and regions.
[0033] See also Figure 1 , shows a schematic diagram of an application scenario of a dictionary-based SQL statement optimization method. The application scenario may include a terminal 101 and a server 102, where data can be exchanged between the terminal 101 and the server 102 via a network, and a corresponding application program may be installed on the terminal 101. The terminal 101 may be a mobile phone, a tablet computer, a smart Bluetooth device, a computer, a large screen device, a robot, etc.; the server 102 may be a single server or a server cluster consisting of multiple servers.
[0034] The user can send query data or SQL statements to be optimized in a specified scenario to the server 102 through the terminal 101. It should be noted that if the server 102 receives query data, it needs to use general technology to convert the query data into SQL statements as the SQL statements to be optimized.
[0035] The server 102 can obtain the initial association relationship between each sub-scene text data and the scene database, where the sub-scene text data is obtained by segmenting the scene text data in the specified scene; for each sub-scene text data, keywords can be extracted from the sub-scene text data, and the initial association relationship can be updated to a first association relationship with the keywords; the keywords are matched with the specified data to filter out the target data from the specified data and update it to the first association relationship to obtain a second association relationship, where the specified data is the data extracted from the scene database; the scene text data and the second association relationship are integrated to generate interpretation data corresponding to the target data to obtain an interpretation dictionary; the interpretation dictionary is used to optimize the SQL statement to be optimized to generate a target SQL statement.
[0036] The server 102 may return the generated target SQL statement to the terminal 101 , or may directly use the target SQL statement to execute a query and return the queried data to the terminal 101 . The specific configuration may be based on actual needs and is not specifically limited herein.
[0037] In this embodiment, a dictionary-based SQL statement optimization method is provided, such as Figure 2As shown, the specific process of the multi-dictionary-based SQL statement optimization method can be as follows:
[0038] S110: Obtain an initial association relationship between each sub-scenario text data and the scenario database.
[0039] A scenario database refers to a database used to store scenario text data in a specified scenario. The specified scenario can be set according to actual needs. For example, if SQL statements in the medical field need to be optimized, the specified scenario is the medical scenario. For another example, if SQL statements in the education field need to be optimized, the specified scenario is the education field.
[0040] In a specified scenario, there are usually a lot of data related to the specified scenario that need to be stored. For example, in the medical field, the patient's diagnosis and treatment data can be stored to form a scenario database for subsequent use; for example, in the education field, each student's test score can be stored to form a scenario database for subsequent use. Text document data can be obtained from the scenario database, such as documents in word, pdf, txt, markdown, etc., which are uniformly converted into text using general technology, and after data cleaning processing, the scenario text data can be obtained. It should be noted that in order to avoid affecting the subsequent effects, in the data cleaning process, there is no need to process the English capitalization and spelling rules in the data.
[0041] The scene text data needs to be annotated and associated with the scene database, for example, scene text data 1: database1, scene text data 2: database2, database3, and the association relationship between the scene text data and the scene database can be saved.
[0042] The sub-scene text data is obtained by slicing the scene text data. The slicing method can be set according to data materials, etc. In the embodiment of the present invention, slicing can be performed according to periods to obtain multiple sub-scene text data. Since there is an association relationship between the scene text data and the scene database, there is also an association relationship between the sub-scene text data and the scene database, which is the initial association relationship.
[0043] The data storage format of the initial association relationship may be selected according to actual needs. In the embodiment of the present invention, the initial association relationship may be recorded as raw_pieces_labeled, as follows: [
[0045] {
[0046] uuid:uuid,
[0047] text:raw_piece_i,
[0048] label: [database1, database2]
[0049] },
[0050] {
[0051] uuid:uuid2,
[0052] text:raw_piece_j,
[0053] label: [database3]
[0054] }, ... ]
[0056] S120 . For each sub-scenario text data, extract keywords from the sub-scenario text data, and update the initial association relationship to a first association relationship using the keywords.
[0057] For each sub-scene text data, keywords can be extracted from the sub-scene text data, and the initial association relationship can be updated to the first association relationship using the extracted keywords. As an implementation method, the first keyword can be extracted from the sub-scene text data according to a specified nomenclature; the first keyword is updated to the initial association relationship to obtain a first intermediate relationship; the sub-scene text data is segmented to obtain a second keyword; the second keyword is updated to the first intermediate relationship to obtain a first association relationship.
[0058] The specified nomenclature can be set according to actual needs. In an embodiment of the present invention, the specified nomenclature may include camel case nomenclature, Pascal nomenclature, underscore nomenclature, etc. Among them, the camel case nomenclature requires that the first word starts with a lowercase letter, and starting from the second word, the first letter of each word is capitalized, for example, userNickname, myLastName. The Pascal nomenclature requires that the first letter of the first word and the first letters of all subsequent words are capitalized, for example, ClassRoom, UserNickname. The underscore nomenclature is a naming method that separates words with an underscore "_", and all letters are lowercase, such as user_nickname. The word or word group that meets the specified nomenclature in the sub-scene text data is extracted as the first keyword.
[0059] Then the extracted first keyword can be updated to the initial association relationship to obtain the first intermediate relationship. If the first keyword is recorded as keywords: [kw1, kw2, ...], the first intermediate relationship is: [
[0061] {
[0062] uuid:uuid,
[0063] text:raw_piece_i,
[0064] label: [database1, database2] ,
[0065] keywords: [kw1, kw2, ...]
[0066] },
[0067] {
[0068] uuid:uuid2,
[0069] text:raw_piece_j,
[0070] label: [database3] ,
[0071] keywords: [kw2,kw3,kw4,...]
[0072] }, ... ]
[0074] The sub-scene text data is segmented to extract the second keyword. Optionally, the general tokenize technology can be used to segment the sub-scene text data, and then open source toolkits such as nltk and hanlp are used to extract Chinese nouns from the segmented data. The extracted Chinese nouns are then normalized, cleaned, and processed according to actual needs.
[0075] In an embodiment of the present invention, the extracted Chinese nouns can be translated into English, and the translated English words are cleaned, normalized, and deduplicated, and then processed using camel case naming to obtain the second keyword. It should be noted that if the Chinese nouns have obvious domain characteristics, a hidden Markov model (HMM) can be used to model the domain translation model to ensure the accuracy of the translation.
[0076] Then the extracted second keyword can be updated to an intermediate relationship. If the second keyword is recorded as cn2en_keywords: [ckw1, ckw2, ...], the first association relationship is: [
[0078] {
[0079] uuid:uuid,
[0080] text:raw_piece_i,
[0081] label: [database1, database2] ,
[0082] keywords: [kw1, kw2, ...],
[0083] cn2en_keywords: [ckw1, ckw2, ...]
[0084] },
[0085] {
[0086] uuid:uuid2,
[0087] text:raw_piece_j,
[0088] label: [database3] ,
[0089] keywords: [kw2,kw3,kw4,...],
[0090] cn2en_keywords: [ckw2, ckw3, ...]
[0091] }, ... ]
[0093] In this step, the first keyword is extracted using the specified naming method, and the sub-scene text data is segmented to extract the second keyword. The keywords in the sub-scene text data can be extracted from multiple angles, which is more comprehensive and accurate, and can ensure the subsequent effective optimization of SQL statements.
[0094] S130: Match the keyword with the designated data to filter out target data from the designated data and update the target data into the first association relationship to obtain a second association relationship.
[0095] Based on the above content, it can be known that the keyword may include a first keyword extracted by a specified nomenclature and a second keyword obtained by word segmentation. When matching the keyword and the specified data, the first keyword and the second keyword need to be matched respectively.
[0096] The specified data is data captured from the aforementioned scenario database. The specified data has a specific format and requirements and can be set according to actual needs. In the embodiment of the present invention, the specified data may include a database name, a table name, and a field name. The first keyword and the second keyword are matched with the specified data respectively to obtain corresponding matching results, and then the matching results are used to update the specified data to the first association relationship to obtain the second association relationship.
[0097] As an implementation mode, when obtaining the second association relationship, the scene database may be traversed to extract multiple specified data, wherein the specified data include a database name, a table name, and a field name; for each of the specified data, the field name in the specified data is matched with each first keyword to obtain a matching string; using the matching string and the field name, the first target data is determined from the multiple specified data, and the first target data is updated to the first association relationship to obtain a second intermediate relationship; for each of the specified data, the field name in the specified data is matched with the second keyword to obtain a matching similarity; using the matching similarity, the second target data is determined from the multiple specified data, and the second target data is updated to the second intermediate relationship to obtain a second association relationship.
[0098] See also Figure 3 , showing a schematic diagram of updating the first association relationship to the second association relationship. Traverse all scenario databases, and organize multiple specified data in the format of "database name|table name|field name", that is, each specified data includes a data name, a table name, and a field name. For each specified data, the field name in the specified data can be subjected to the longest common substring matching process with the first keyword extracted above to obtain a first matching result. Among them, the longest common substring processing refers to the longest continuous string found in the field name and the first keyword, and the string is the matching string. Among them, the specific method of the longest common substring matching process can refer to the existing algorithm, which will not be repeated here.
[0099] By using the matching string and the field name used in the matching, the first target data can be determined from the multiple specified data, and the first target data can be updated to the first association relationship to obtain the second intermediate relationship. For example, the length ratio between the length of the matching string and the length of the field name can be calculated; the specified data corresponding to the field name whose length ratio is not less than the length threshold is determined as the first target data; the first target data is added to the first association relationship to obtain the second intermediate relationship.
[0100] The matching string is obtained by matching the longest common substring of the first keyword and the field name. The length of the matching string can be obtained first, and the length of the field name used for the match can be obtained. The length of the matching string is divided by the length of the field name to obtain a length ratio. The length threshold is a value not greater than 1, which can be set according to actual needs. In an embodiment of the present invention, the length threshold can be set to 0.8. Compare the length ratio with the length threshold. If the length ratio is not less than the length threshold, the designated data corresponding to the field name can be determined as the first designated data. Specifically, the field name corresponding to the matching string whose length ratio is not less than the length threshold can be determined as the first field name, and then the designated data corresponding to the first field name can be determined as the first target data.
[0101] Then, the first target data may be added to the first association relationship, and the second intermediate relationship corresponding to the sub-scene data may be obtained.
[0102] If the first specified data is defined as db_keywords_part1, the second intermediate relationship is: [
[0104] {
[0105] uuid:uuid,
[0106] text:raw_piece_i,
[0107] label: [database1, database2] ,
[0108] keywords: [kw1, kw2, ...],
[0109] cn2en_keywords: [ckw1, ckw2, ...],
[0110] db_keywords_part1: [dbkw1, dbkw, ...]
[0111] },
[0112] {
[0113] uuid:uuid2,
[0114] text:raw_piece_j,
[0115] label: [database3] ,
[0116] keywords: [kw2,kw3,kw4,...],
[0117] cn2en_keywords: [ckw2, ckw3, ...],
[0118] db_keywords_part1: [dbkw2, dbkw3,...]
[0119] }, ... ]
[0121] For each designated data, the field name in the designated data and the second keyword can also be matched to each other by similarity, that is, the similarity between each field name and the second keyword is calculated to obtain the matching similarity. As an implementation method, the field name and the second keyword can be encoded to obtain the corresponding field vector and the second keyword vector, and then the cosine similarity between the field vector and the second keyword vector can be calculated to obtain the matching similarity.
[0122] After obtaining the matching similarity, the matching similarity can be used to filter out the second target data from the designated data, and the second target data can be updated to the second intermediate relationship to obtain the second association relationship. Specifically, if the matching similarity is not less than the similarity threshold, the field name corresponding to the matching similarity is recorded as the designated field name; the designated data corresponding to the designated field name is obtained as the second target data; the second target data is added to the second intermediate relationship to obtain the second association relationship.
[0123] The similarity threshold can be set in advance according to actual needs. In the embodiment of the present invention, the similarity threshold is set to 0.9. Compare the matching similarity with the similarity threshold. If the matching similarity is not less than the similarity threshold, the field name corresponding to the matching similarity can be recorded as the specified field name. Then obtain the specified data corresponding to the specified field name as the second target data, and then add the second target data to the second intermediate relationship to obtain the second association relationship. If the second target data is defined as db_keywords_part2, the second association relationship is: [
[0125] {
[0126] uuid:uuid,
[0127] text:raw_piece_i,
[0128] label: [database1, database2],
[0129] keywords: [kw1, kw2, ...],
[0130] cn2en_keywords: [ckw1, ckw2, ...],
[0131] db_keywords_part1: [dbkw1, dbkw2, ...],
[0132] db_keywords_part2: [dbkw2, dbkw3, ...]
[0133] },
[0134] {
[0135] uuid:uuid2,
[0136] text:raw_piece_j,
[0137] label: [database3],
[0138] keywords: [kw2,kw3,kw4,...],
[0139] cn2en_keywords: [ckw2, ckw3, ...],
[0140] db_keywords_part1: [dbkw2, dbkw3,...],
[0141] db_keywords_part2: [dbkw4, dbkw5,...]
[0142] }, ... ]
[0144] S140: Fusing the scene text data with the second association relationship to generate interpretation data corresponding to the target data, and obtaining an interpretation dictionary.
[0145] According to the foregoing content, the second association relationship includes the scene database corresponding to the sub-scene text data, the first keyword, the second keyword, the first target data, and the second target data. The interpretation data of the target data is the data that explains the target data in combination with the scene, so that in the subsequent optimization of the SQL statement, the meaning of the field can be accurately understood to ensure the accuracy of the SQL statement. The interpretation dictionary can contain the association relationship between the target data and the interpretation data.
[0146] As an implementation mode, when obtaining the interpretation dictionary, the scene text data may be used to perform context expansion on each of the sub-scene text data to obtain sub-extended text data corresponding to the sub-scene text data; the first target data and the second target data in the second association relationship may be merged into third target data, and a mapping relationship between the third target data and the sub-extended text data may be established to obtain a third association relationship; for each third target data, the interpretation data corresponding to the third target data may be generated using the database information corresponding to the third target data and the sub-extended text data to obtain the interpretation dictionary.
[0147] See also Figure 4 , showing a schematic diagram of generating a definition dictionary. The sub-scene text data is obtained by segmenting the scene text data, and the context data of the sub-scene text data can be obtained in the scene text data to expand the sub-scene text data and obtain the sub-extended text data of the sub-scene text data. Specifically, when the scene text data is segmented, it has been sliced into multiple sub-scene text data. According to the position of each sub-scene text data in the scene text data, a sub-scene text sequence can be arranged. In the sub-scene text sequence, the front and back n fragments of the sub-scene text data and the sub-scene text data can be used as sub-extended text data. Among them, n can be adjusted according to business experience or actual needs. In the embodiment of the present invention, n is 4. If the sub-extended text data is defined as context_data, the sub-extended text data can be saved in the following format: [
[0149] {
[0150] uuid:uuid,
[0151] text:raw_piece_i,
[0152] context: previous n+raw_piece_i+next n
[0153] } ]
[0155] There are first target data and second target data in the second association relationship. The first target data and the second target data are both in the format of database name, table name, and field name. The first target data and the second target data can be merged, and the merged data can be deduplicated to obtain the third target data.
[0156] Sub-extension text data can be added to the second association relationship. On this basis, the second association relationship and the third target data are merged to obtain the mapping relationship between the third target data and the sub-extension text data. For the same sub-scenario text data, its corresponding sub-extension text data and the third target data can be obtained. The third target data is used as the key and the sub-extension text data is used as the value to obtain the third association relationship. The reference format of the third association relationship is as follows:
[0157] {
[0158] Database name | Table name | Field name: [context1, context2], ...
[0159] }
[0160] According to the third association relationship, the sub-extended text data corresponding to the third target data can be obtained, and the interpretation data of the third target data can be generated by using the sub-extended text data corresponding to the third target data and the database information.
[0161] The third target data includes the database name, table name, and field name, and the database information of the third target data can be obtained. The database information refers to information related to the database, which may include the organization and structure of the database, i.e., schema information, primary key and foreign key information, and data randomly extracted by row in the table, etc. Based on the third association relationship, the corresponding sub-extended text data can also be obtained, and the interpretation data of the third target data can be generated by using the sub-extended text data and the database information.
[0162] As an implementation method, the interpretation data may be generated by using prompt words and a large language model. Specifically, an interpretation prompt word template may be obtained, wherein the interpretation prompt word template includes a designated slot, a database slot, and an extension slot, and the interpretation prompt word template is in a pseudo-function form; the third target data is filled into the designated slot, the database information is filled into the database slot, and the sub-extension text data is filled into the extension slot to obtain the prompt word to be used; the prompt word to be used is input into the large language model to obtain the interpretation data of the third target data generated by the large language model, and obtain the interpretation dictionary.
[0163] A definition prompt word template may be pre-set, and the definition prompt word template may include a designated slot, a database slot and an extension slot, wherein the designated slot may be used to fill in the third target data, the database slot may be used to fill in the database information, and the extension slot may be used to fill in the sub-extension text data.
[0164] The interpretation prompt word template provided by the embodiment of the present invention is as follows:
[0165] “### Role: You are a Python pseudocode interpreter. Key Points You do not need to actually call the Python interpreter to execute the following code. Give the corresponding answer based on the ideas of the following code writing.
[0166] def summary_column_meaning(key_info:str) -> str:
[0167] """The function of this function is to explain key_info based on the given context information, and explain its role, function, and meaning.
[0168] key_info:str is the field name to be interpreted, in the format of database name | table name | field name | field type | field remarks (leave it blank if none).
[0169] return:str is the answer to the task. Output only the answer, which must be a sentence and within 150 words.
[0170] """
[0171] # ---- SCHEMA INFO ----
[0172] ## database: \n{database name}
[0173] ## table1: \n{table name}
[0174] ## columns of table1:\n{column1, column2, ...}
[0175] ## values of columns:\n{ table randomly samples 3 values by row}
[0176] ## primary keys: \n{primary key information}
[0177] ## foreign keys: \n{foreign key information}
[0178] # ---- context of key_info ----
[0179] {context1 \n context2}\n\n
[0180] column_meaning = summary_column_meaning (key_info = database name | table name | field name)
[0181] column_meaning: "
[0182] Fill the third target data into the specified slot, that is, key_info=database name | table name | field name; fill the database information into the database slot, that is, SCHEMA INFO; fill the sub-extension text data into the extension slot, that is, {context1\n context2}, and the prompt word to be used can be obtained.
[0183] The above-mentioned interpretation prompt word template is in the form of a pseudo-function. Compared with the prompt words in the form of regular text, the prompt words to be used obtained based on the pseudo-function form can reduce the illusion of the large language model and enhance its understanding ability. It should be noted that the prompt words given later in the embodiment of the present invention are all in the form of pseudo-functions. By inputting the prompt words to be used into the large language model, the interpretation data corresponding to the third target data can be output, and the interpretation data can include the function and meaning of the third target data.
[0184] The large language model may adopt a common LLM model, which may be adjusted according to actual needs. After obtaining the interpretation data output by the large language model, the third target data may be associated with the interpretation data to obtain an interpretation dictionary.
[0185] S150: Utilize the definition dictionary to optimize the SQL statement to be optimized to generate a target SQL statement.
[0186] The SQL statement to be optimized is obtained after converting the query data in the specified scenario, and the query data is the original query statement input by the user. The query data can be converted into an SQL statement through the existing text2sql method. Since the SQL statement generated by the existing method has a series of problems such as inaccuracy, the SQL statement can be used as the SQL statement to be optimized, and a more accurate target SQL statement can be generated after optimization.
[0187] As an implementation method, when converting query data into an SQL statement to be optimized, the query data may be cleaned to obtain cleaned query data; database information corresponding to a target database may be obtained; the cleaned query data and the database information corresponding to the target database may be merged with a specified prompt word template to obtain a specified prompt word; and the specified prompt word may be input into a large language model to obtain the SQL statement to be optimized.
[0188] Among them, the cleaning process can be to use the semantic parsing ability of the large language model to eliminate descriptions and modal particles with low information content in the query data, and further refine the query data to make the information expressed in the query data clearer and more accurate.
[0189] Then, the database information of the target database, namely, the schema information, is obtained, which may include a table name set, a field name set, a field type set, primary key and foreign key information, and sample value data of a random sampling of table data samples. The database information and the cleaned query data are filled into the specified prompt word template to obtain the specified prompt word.
[0190] In the embodiment of the present invention, the specified prompt word template may be:
[0191] “### Role: You are a Python pseudocode interpreter that also has the ability to output SQL executable statements. Key Points You do not need to actually call the Python interpreter to execute the following code. Give the corresponding answer based on the ideas of the following code writing.
[0192] def get_preliminary_SQL(schema_info:str, question:str) -> str:
[0193] """The function of this function is to understand the meaning of the user question question based on the input parameters provided, and combined with the given schema_info, please translate the user quesiton into an execution statement that conforms to the SQLite syntax.
[0194] schema_info:str is the database schema information associated with question, including table name, column name and its corresponding value type and value sampling, primary key and foreign key information.
[0195] question:str is the question entered by the user.
[0196] SQL:str is the answer to this task, that is, the SQLite executable statement that meets the requirements of the question.
[0197] Only the executable SQL of the user's question question needs to be output, and no other content needs to be output.
[0198] """
[0199] # Please translate question into an executable statement that conforms to SQL syntax based on the schema_info information.
[0200] # Table name
[0201] T_i
[0202] # Column[Column Type, (Value1,Value2,Value3)] in Table T_i
[0203] F_1[FT_1,(Value_f1_1, Value_f1_2, Value_f1_3)],F_2[FT_2,( Value_f2_1,Value_f2_2, Value_f2_3)],...
[0204] #primary keys
[0205] T_i.primary_k1,(T_i.primary_k1, T_i.parmary_k2)
[0206] # foreign keys
[0207] T_i.primary = T_j.primary_k1
[0208] # User question
[0209] question: {Q_"normalized"}
[0210] SQL: "
[0211] By inputting the specified prompt words into the large language model, the SQL statement to be optimized can be obtained. Of course, the SQL statement to be optimized can also be provided directly by the user.
[0212] As an implementation mode, when optimizing the SQL statement to be optimized to obtain the target SQL statement, the column name can be extracted from the SQL statement to be optimized, and the database name and table name corresponding to the column name can be extracted from the target database as the designated data to be queried, and the target database is the database corresponding to the SQL statement to be optimized; the interpretation data corresponding to the designated data to be queried is determined from the interpretation dictionary as the interpretation data to be used; the column name in the SQL statement to be optimized is optimized in combination with the interpretation data to be used, the query data and the database information of the target database to generate an optimized SQL statement; if the optimized SQL statement passes the verification process, the optimized SQL statement is used as the target SQL statement corresponding to the query data.
[0213] The SQL statement to be optimized can be generated using query data and the target database, and the SQL statement to be optimized can contain relevant column names in the target database. The column names in the SQL statement to be optimized can be extracted using existing technologies to obtain a column name set. At the same time, in the target database, the database name and table name corresponding to the column name can be extracted and sorted according to the format of the third target data as the designated data to be queried.
[0214] In the definition dictionary, definition data corresponding to the designated data to be queried are extracted as definition data to be used, wherein the definition data to be used may include the designated data to be queried and definition data corresponding to the designated data to be queried.
[0215] The column names contained in the SQL statement to be optimized are usually related to the query data. For column names that cannot be directly associated with the query data, these column names may be errors. The optimization process is mainly to conduct a secondary understanding of the column names that cannot be directly associated with the query data to correct potential errors.
[0216] Optionally, when performing the optimization process, it can be: using a first prompt word to guide the optimization model to analyze and understand each column name in the SQL statement to be optimized according to the database information of the target database; using a second prompt word to guide the optimization model to use the query data and the interpretation data to be used to enhance the analysis and understanding process; using a third prompt word to guide the optimization model to generate an optimized SQL statement corresponding to the SQL statement to be optimized based on the processing results of the analysis and understanding process, the query data and the interpretation data to be used.
[0217] The optimization process is mainly performed by the optimization model, which refers to a large language model, which can be an existing model or a model obtained by training with specific data according to actual needs.
[0218] The first prompt word, the second prompt word and the third prompt word are obtained by filling corresponding data on the basis of the pre-set first prompt word template, the second prompt word template and the third prompt word template. The first prompt word template, the second prompt word template and the third prompt word template can all be pre-set, and corresponding filling slots are set.
[0219] The first prompt word is a system-level prompt word, the second prompt word is an assistant-level prompt word, and the third prompt word is a user-level prompt word. These prompt words can all be input into the optimization model to generate optimized SQL statements.
[0220] Among them, the first prompt word template is as follows:
[0221] # SCHEMA INFO
[0222] ## database: \n{database name}
[0223] ## table1: \n{table name}
[0224] ## columns of table1:\n{column1, column2, ...}
[0225] ## values of columns:\n{ table randomly samples 3 values by row}
[0226] ## primary keys: \n{primary key information}
[0227] ## foreign keys: \n{foreign key information}
[0228] # preliminary_sql: {preliminary_sql}
[0229] In conjunction with SCHEMA INFO, please carefully understand the column names in preliminary_sql. Some column names may be incorrect. We will fix them based on user issues.
[0230] The first prompt word is obtained by filling the database information of the target database and the SQL statement to be optimized into the first prompt word template. The first prompt word is a system-level prompt word, which is used to constrain and control the basic behavior of the model. The first prompt word is input into the optimization model, which can guide the optimization model to analyze and understand each column name in the SQL statement to be optimized according to the database information in the first prompt word.
[0231] The second prompt word template is as follows:
[0232] "In conjunction with user issue: "{Q_normalized}" the explanations of the following column names may be helpful to you when repairing.
[0233] preliminary_columns_key1: column_meaning1
[0234] preliminary_columns_key2: column_meaning2
[0235] ... "
[0236] The second prompt word can be obtained by filling the interpretation data to be used into the second prompt word template. The second prompt word is an assistant-level prompt word, which can further refine the behavior in a specific scene or task based on the system-level prompt word. The second prompt word is input into the optimization model to guide the optimization model to refer to the interpretation data to be used to enhance the analysis, understanding and processing process, so as to improve the accuracy of its analysis and reasoning.
[0237] The third prompt word template is as follows:
[0238] "{preliminary_sql}
[0239] There may be a problem with the column names of the above SQL statements, as the correct column names may not be directly related by my question.
[0240] Based on my question: "{Q_normalized}", as well as the explanation of schema info and column names, please give me an executable SQL statement again. Please give the SQL result directly without outputting additional content.
[0241] SQL: "
[0242] The SQL statement to be optimized and the query data can be filled into the third prompt word template to obtain the third prompt word. The third prompt word is a user-level prompt word. The third prompt word is input into the optimization model to guide the optimization model to generate and output the optimized SQL statement based on the processing results of the aforementioned analysis and understanding processing, the query data and the interpretation data to be used.
[0243] In order to improve the accuracy of the target SQL statement, the optimized SQL statement can be verified. If the optimized SQL statement passes the verification process, the optimized SQL statement can be directly used as the target SQL statement corresponding to the query data; if the optimized SQL statement fails the verification process, the optimized SQL statement needs to be corrected again until the specified conditions are met.
[0244] Optionally, when performing the verification process, the maximum number of dialogue rounds, the timeout waiting parameter, the agent and the specified tool can be predefined. In the embodiment of the present invention, the agent can be set to a first agent and a second agent, the maximum number of dialogue rounds can be set to 3, and the timeout waiting parameter can be set to 60 seconds.
[0245] If the current dialogue round number is 1, the first agent can call the specified tool to automatically execute the optimized SQL statement and give the corresponding execution result, which can include search results or error information. The error information can include execution error or execution timeout. If the specified tool does not output error information, the optimized SQL statement can be directly used as the target SQL statement.
[0246] If the specified tool outputs an error message, and the current number of dialogue rounds is less than the maximum number of dialogue rounds, the error message can be used to correct the optimized SQL statement through the second agent. When correcting the optimized SQL statement, the second agent can use the first prompt word, the second prompt word, and the third prompt word mentioned above to process, and finally use the fourth prompt word to output the final corrected SQL statement. The fourth prompt word can also be pre-set with a fourth prompt word template. The error message or timeout is filled into the fourth prompt word template to obtain the fourth prompt word, and the model then outputs the corrected SQL statement based on the fourth prompt word.
[0247] Among them, the fourth prompt word template is as follows:
[0248] "Result error: {error_msg} / Result timeout
[0249] Please understand the past historical information carefully and correct your answer.
[0250] New SQL: ”
[0251] The corrected SQL statement is then used as the optimized SQL statement, and the verification and subsequent steps are returned until the target SQL statement is output or the current number of conversation rounds reaches the maximum number of conversation rounds. If the target SQL statement is obtained, the target SQL statement can be directly output. If the current number of conversation rounds reaches the maximum number of conversation rounds, a fallback statement can be set based on business experience, such as: "Your question is not detailed enough. Can you change your mind and ask me again?"
[0252] The dictionary-based SQL statement optimization solution provided by the embodiment of the present invention can be applied in various scenarios. For example, taking the medical and health care scenario as an example, a dictionary related to the field can be generated to optimize the relevant SQL statements in the scenario and obtain more accurate target SQL statements.
[0253] As can be seen from the above, the embodiment of the present invention can obtain the initial association relationship between the sub-scene text data and the scene database, perform keyword extraction of multiple dimensions on the sub-scene text data, use the keywords to obtain the first association relationship, match the extracted keywords with the field names of the database, update the data with a high match between the field and the keyword to the first association relationship, obtain the second association relationship, and then fuse the scene text data with the second association relationship to generate an interpretation dictionary, and use the interpretation dictionary to enhance the model's ability to understand the database fields in the specified scene, so as to correct potential errors in the SQL statement to be optimized and obtain a more accurate target SQL statement.
[0254] In order to better implement the above method, the embodiment of the present invention also provides a dictionary-based SQL statement optimization device, which can be integrated in an electronic device, which can be a terminal, a server, etc. Among them, the terminal can be a mobile phone, a tablet computer, a smart Bluetooth device, a laptop, a personal computer, etc.; the server can be a single server or a server cluster composed of multiple servers.
[0255] For example, in this embodiment, the method of the embodiment of the present invention is described in detail by taking the SQL statement optimization device based on the dictionary as an example in which it is specifically integrated in the server.
[0256] For example, Figure 5 As shown, the dictionary-based SQL statement optimization device 200 may include an acquisition module 210, a first association module 220, a second association module 230, a interpretation module 240, and an optimization module 250, as follows:
[0257] An acquisition module 210 is used to acquire an initial association relationship between each sub-scene text data and a scene database, wherein the sub-scene text data is obtained by segmenting the scene text data in a specified scene;
[0258] A first association module 220 is used to extract keywords from each sub-scene text data, and update the initial association relationship to a first association relationship with the keyword;
[0259] The second association module 230 is used to match the keyword with the specified data to filter out the target data from the specified data and update it to the first association relationship to obtain a second association relationship, wherein the specified data is data extracted from the scene database;
[0260] A paraphrase module 240, configured to fuse the scene text data with the second association relationship, generate paraphrase data corresponding to the target data, and obtain a paraphrase dictionary;
[0261] The optimization module 250 is used to optimize the SQL statement to be optimized by using the interpretation dictionary to generate a target SQL statement, wherein the SQL statement to be optimized is generated based on the query data in the specified scenario.
[0262] In some embodiments, the first association module 220 is specifically used to:
[0263] Extracting a first keyword from the sub-scenario text data according to a specified nomenclature;
[0264] Updating the first keyword into the initial association relationship to obtain a first intermediate relationship;
[0265] Performing word segmentation processing on the sub-scenario text data to obtain a second keyword;
[0266] Update the second keyword to the first intermediate relationship to obtain a first association relationship
[0267] In some embodiments, the keyword includes a first keyword extracted by a specified nomenclature and a second keyword extracted by word segmentation, the target data includes first target data and second target data, and the second association module 230 is specifically used to:
[0268] Traversing the scenario database to extract a plurality of specified data, the specified data including a database name, a table name, and a field name;
[0269] For each of the specified data, the field name in the specified data is matched with each first keyword for the longest common substring to obtain a matching string;
[0270] Determine first target data from the plurality of specified data using the matching string and the field name, and update the first target data into the first association relationship to obtain a second intermediate relationship;
[0271] For each of the designated data, a field name in the designated data is matched with the second keyword to obtain a matching similarity;
[0272] The second target data is determined from the plurality of designated data using the matching similarity, and the second target data is updated into the second intermediate relationship to obtain a second association relationship.
[0273] In some embodiments, the second association module 230 is specifically used to:
[0274] Calculate the length ratio between the length of the matching string and the length of the field name;
[0275] Determine the designated data corresponding to the field name whose length ratio is not less than the length threshold as the first target data;
[0276] The first target data is added to the first association relationship to obtain a second intermediate relationship.
[0277] In some embodiments, the second association module 230 is specifically used to:
[0278] If the matching similarity is not less than the similarity threshold, the field name corresponding to the matching similarity is recorded as the designated field name;
[0279] Obtaining the specified data corresponding to the specified field name as the second target data;
[0280] The second target data is added to the second intermediate relationship to obtain a second association relationship.
[0281] In some embodiments, the interpretation module 240 is specifically used to:
[0282] Performing context expansion on each of the sub-scenario text data using the scene text data to obtain sub-expanded text data corresponding to the sub-scenario text data;
[0283] The first target data and the second target data in the second association relationship are combined into third target data, and a mapping relationship between the third target data and the sub-extended text data is established to obtain a third association relationship;
[0284] For each of the third target data, the database information of the third target data and the sub-expanded text data are used to generate the interpretation data corresponding to the third target data, and obtain the interpretation dictionary.
[0285] In some embodiments, the interpretation module 240 is specifically used to:
[0286] Obtaining a definition prompt word template, wherein the definition prompt word template includes a specified slot, a database slot, and an extension slot, and the definition prompt word template is in a pseudo-function form;
[0287] Fill the third target data into the designated slot, fill the database information into the database slot, and fill the sub-extension text data into the extension slot to obtain the prompt word to be used;
[0288] The prompt words to be used are input into the large language model to obtain the interpretation data of the third target data generated by the large language model, and obtain the interpretation dictionary.
[0289] In some embodiments, the optimization module 250 is specifically configured to:
[0290] Extracting a column name from the SQL statement to be optimized, and extracting a database name and a table name corresponding to the column name from a target database as designated data to be queried, wherein the target database is a database corresponding to the SQL statement to be optimized;
[0291] Determine the interpretation data corresponding to the designated data to be queried from the interpretation dictionary as the interpretation data to be used;
[0292] Optimizing the column names in the SQL statement to be optimized by combining the interpretation data to be used, the query data and the database information of the target database to generate an optimized SQL statement;
[0293] If the optimized SQL statement passes the verification process, the optimized SQL statement is used as the target SQL statement corresponding to the query data.
[0294] In some embodiments, the optimization module 250 is specifically configured to:
[0295] Using the first prompt word, guiding the optimization model to analyze, understand and process each column name in the SQL statement to be optimized according to database information of the target database;
[0296] Using the second prompt word, guiding the optimization model to use the query data and the interpretation data to be used to enhance the analysis and understanding process;
[0297] The third prompt word is used to guide the optimization model to generate an optimized SQL statement corresponding to the SQL statement to be optimized based on the processing result of the analysis and understanding processing, the query data, and the interpretation data to be used.
[0298] In specific implementation, the above modules can be implemented as independent entities, or can be arbitrarily combined and implemented as the same or several entities. The specific implementation of the above modules can be found in the previous method embodiments, which will not be repeated here.
[0299] From the above, it can be seen that the dictionary-based SQL statement optimization device of this embodiment can obtain the initial association relationship between the sub-scene text data and the scene database, use the keywords extracted from the sub-scene text data to update the initial association relationship to the first association relationship, match the keywords and the specified data to determine the target data, and use the target data to update the first association relationship to the second association relationship, fuse the scene text data and the second association relationship, generate interpretation data corresponding to the target data, obtain the scene-related interpretation dictionary, and finally use the interpretation dictionary to improve the semantic understanding of the model in a specific scene, optimize the SQL statement to be optimized, and generate a more accurate target SQL statement.
[0300] The embodiment of the present invention further provides an electronic device, which may be a terminal, a server, or the like. The terminal may be a mobile phone, a tablet computer, a smart Bluetooth device, a notebook computer, a personal computer, or the like; the server may be a single server or a server cluster composed of multiple servers, or the like.
[0301] In some embodiments, the dictionary-based SQL statement optimization device may also be integrated into multiple electronic devices. For example, the dictionary-based SQL statement optimization device may be integrated into multiple servers, and the dictionary-based SQL statement optimization method of the present invention may be implemented by multiple servers.
[0302] In this embodiment, the electronic device of this embodiment is a server as an example for detailed description, for example, Figure 6 As shown, it shows a schematic diagram of the structure of an electronic device involved in an embodiment of the present invention, specifically:
[0303] The electronic device may include components such as a processor 310 with one or more processing cores, a memory 320 with one or more computer-readable storage media, a power supply 330, an input module 340, and a communication module 350. Those skilled in the art will appreciate that Figure 6 The electronic device structure shown in the figure does not constitute a limitation on the electronic device, and may include more or fewer components than shown in the figure, or combine certain components, or arrange the components differently.
[0304] The processor 310 is the control center of the electronic device. It uses various interfaces and lines to connect various parts of the entire electronic device. It executes various functions of the electronic device and processes data by running or executing software programs and / or modules stored in the memory 320, and calling data stored in the memory 320. In some embodiments, the processor 310 may include one or more processing cores; in some embodiments, the processor 310 may integrate an application processor and a modem processor, wherein the application processor mainly processes the operating system, user interface, and application programs, and the modem processor mainly processes wireless communications. It is understandable that the above-mentioned modem processor may not be integrated into the processor 310.
[0305] The memory 320 can be used to store software programs and modules. The processor 310 executes various functional applications and data processing by running the software programs and modules stored in the memory 320. The memory 320 may mainly include a program storage area and a data storage area, wherein the program storage area may store an operating system, an application required for at least one function (such as a sound playback function, an image playback function, etc.), etc.; the data storage area may store data created according to the use of the electronic device, etc. In addition, the memory 320 may include a high-speed random access memory, and may also include a non-volatile memory, such as at least one disk storage device, a flash memory device, or other volatile solid-state storage devices. Accordingly, the memory 320 may also include a memory controller to provide the processor 310 with access to the memory 320.
[0306] The electronic device also includes a power supply 330 for supplying power to various components. In some embodiments, the power supply 330 can be logically connected to the processor 310 through a power management system, so as to manage charging, discharging, and power consumption through the power management system. The power supply 330 can also include any components such as one or more DC or AC power supplies, recharging systems, power failure detection circuits, power converters or inverters, and power status indicators.
[0307] The electronic device may further include an input module 340, which may be used to receive input digital or character information and generate keyboard, mouse, joystick, optical or trackball signal inputs related to user settings and function controls.
[0308] The electronic device may further include a communication module 350. In some embodiments, the communication module 350 may include a wireless module. The electronic device may perform short-range wireless transmission through the wireless module of the communication module 350, thereby providing the user with wireless broadband Internet access. For example, the communication module 350 may be used to help the user send and receive emails, browse web pages, and access streaming media.
[0309] Although not shown, the electronic device may further include a display unit, etc., which will not be described in detail herein. Specifically in this embodiment, the processor 310 in the electronic device will load the executable files corresponding to the processes of one or more application programs into the memory 320 according to the following instructions, and the processor 310 will run the application programs stored in the memory 320, thereby implementing the steps in the methods of the embodiments of the present invention.
[0310] The specific implementation of the above operations can be found in the previous embodiments, which will not be described in detail here.
[0311] From the above, it can be seen that the electronic device provided by the embodiment of the present invention can obtain the initial association relationship between the sub-scene text data and the scene database, use the keywords extracted from the sub-scene text data to update the initial association relationship to the first association relationship, match the keywords and the specified data to determine the target data, and use the target data to update the first association relationship to the second association relationship, fuse the scene text data and the second association relationship, generate interpretation data corresponding to the target data, obtain the scene-related interpretation dictionary, and finally use the interpretation dictionary to improve the semantic understanding of the model in a specific scene, optimize the SQL statement to be optimized, and generate a more accurate target SQL statement.
[0312] A person of ordinary skill in the art will appreciate that all or part of the steps in the various methods of the above embodiments may be completed by instructions, or by controlling related hardware through instructions. The instructions may be stored in a computer-readable storage medium and loaded and executed by a processor.
[0313] To this end, an embodiment of the present invention provides a computer-readable storage medium, in which a plurality of instructions are stored. The instructions can be loaded by a processor to execute the steps in any one of the dictionary-based SQL statement optimization methods provided in the embodiments of the present invention.
[0314] The storage medium may include: a read-only memory (ROM), a random access memory (RAM), a magnetic disk or an optical disk, etc.
[0315] According to one aspect of the present invention, a computer program product or computer program is provided, the computer program product or computer program including a computer program / instruction, the computer program / instruction being stored in a computer-readable storage medium. A processor of an electronic device reads the computer program / instruction from the computer-readable storage medium, and the processor executes the computer program / instruction, so that the electronic device executes the method provided in various optional implementations of the above-mentioned embodiments in terms of generating a paraphrase dictionary or optimizing SQL statements based on a paraphrase dictionary.
[0316] Since the instructions stored in the storage medium can execute the steps in any dictionary-based SQL statement optimization method provided in the embodiments of the present invention, the beneficial effects that can be achieved by any dictionary-based SQL statement optimization method provided in the embodiments of the present invention can be achieved. Please refer to the previous embodiments for details and will not be repeated here.
[0317] The above is a detailed introduction to a dictionary-based SQL statement optimization method and device provided in an embodiment of the present invention. Specific examples are used in this article to illustrate the principles and implementation methods of the present invention. The description of the above embodiments is only used to help understand the method of the present invention and its core idea. At the same time, for those skilled in the art, according to the idea of the present invention, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as limiting the present invention.
Claims
1. A dictionary-based SQL statement optimization method, characterized in that: The method comprises: Acquire an initial association relationship between each sub-scene text data and a scene database, wherein the sub-scene text data is obtained by segmenting the scene text data in the specified scene; For each sub-scenario text data, extract keywords from the sub-scenario text data, and update the initial association relationship to a first association relationship using the keywords; Matching the keyword with designated data to filter out target data from the designated data and update the target data into the first association relationship to obtain a second association relationship, wherein the designated data is data extracted from the scene database; The scene text data and the second association relationship are integrated to generate interpretation data corresponding to the target data, thereby obtaining an interpretation dictionary; Optimizing the SQL statement to be optimized by using the interpretation dictionary to generate a target SQL statement, wherein the SQL statement to be optimized is generated based on the query data in the specified scenario; The keyword includes a first keyword extracted by a specified nomenclature and a second keyword extracted by word segmentation, the target data includes a first target data and a second target data, and the keyword is matched with the specified data to filter out the target data from the specified data and update it to the first association relationship to obtain the second association relationship, including: Traversing the scene database to extract multiple specified data, the specified data including database name, table name and field name; for each of the specified data, performing longest common substring matching processing on the field name in the specified data and each first keyword to obtain a matching string; using the matching string and field name, determining first target data from the multiple specified data, and updating the first target data to the first association relationship to obtain a second intermediate relationship; for each of the specified data, performing similarity matching processing on the field name in the specified data and the second keyword to obtain matching similarity; using the matching similarity to determine second target data from the multiple specified data, and updating the second target data to the second intermediate relationship to obtain a second association relationship; The SQL statement to be optimized is optimized by using the interpretation dictionary to generate a target SQL statement, including: extracting column names from the SQL statement to be optimized, and extracting database names and table names corresponding to the column names from a target database as designated data to be queried, the target database being a database corresponding to the SQL statement to be optimized; determining interpretation data corresponding to the designated data to be queried from the interpretation dictionary as interpretation data to be used; optimizing the column names in the SQL statement to be optimized in combination with the interpretation data to be used, query data and database information of the target database to generate an optimized SQL statement; if the optimized SQL statement passes the verification process, using the optimized SQL statement as the target SQL statement corresponding to the query data.
2. The method according to claim 1, characterized in that The step of extracting a keyword from the sub-scenario text data and updating the initial association relationship to a first association relationship using the keyword includes: Extracting a first keyword from the sub-scenario text data according to a specified nomenclature; Updating the first keyword into the initial association relationship to obtain a first intermediate relationship; Performing word segmentation processing on the sub-scenario text data to obtain a second keyword; The second keyword is updated into the first intermediate relationship to obtain a first association relationship.
3. The method according to claim 1, characterized in that The method of using the matching string and the field name to determine the first target data from the plurality of specified data, and updating the first target data to the first association relationship to obtain the second intermediate relationship includes: Calculate the length ratio between the length of the matching string and the length of the field name; Determine the designated data corresponding to the field name whose length ratio is not less than the length threshold as the first target data; The first target data is added to the first association relationship to obtain a second intermediate relationship.
4. The method according to claim 1, characterized in that: The determining second target data from the plurality of designated data by using the matching similarity, and updating the second target data into the second intermediate relationship to obtain a second association relationship includes: If the matching similarity is not less than the similarity threshold, the field name corresponding to the matching similarity is recorded as the designated field name; Obtaining the specified data corresponding to the specified field name as the second target data; The second target data is added to the second intermediate association relationship to obtain a second association relationship.
5. The method according to claim 1, characterized in that The fusing the scene text data with the second association relationship to generate interpretation data corresponding to the target data to obtain an interpretation dictionary includes: Performing context expansion on each of the sub-scenario text data using the scene text data to obtain sub-expanded text data corresponding to the sub-scenario text data; The first target data and the second target data in the second association relationship are combined into third target data, and a mapping relationship between the third target data and the sub-extended text data is established to obtain a third association relationship; For each of the third target data, the database information of the third target data and the sub-expanded text data are used to generate the interpretation data corresponding to the third target data, and obtain the interpretation dictionary.
6. The method according to claim 5, characterized in that The step of using the database information of the third target data and the sub-expanded text data to generate interpretation data corresponding to the third target data and obtain an interpretation dictionary includes: Obtaining a definition prompt word template, wherein the definition prompt word template includes a specified slot, a database slot, and an extension slot, and the definition prompt word template is in a pseudo-function form; Fill the third target data into the designated slot, fill the database information into the database slot, and fill the sub-extension text data into the extension slot to obtain the prompt word to be used; The prompt words to be used are input into the large language model to obtain the interpretation data of the third target data generated by the large language model, and obtain the interpretation dictionary.
7. The method according to claim 1, characterized in that The optimizing the column names in the SQL statement to be optimized by combining the to-be-used interpretation data, the query data and the database information of the target database to generate an optimized SQL statement includes: Using the first prompt word, guiding the optimization model to analyze, understand and process each column name in the SQL statement to be optimized according to database information of the target database; Using the second prompt word, guiding the optimization model to use the query data and the interpretation data to be used to enhance the analysis and understanding process; The third prompt word is used to guide the optimization model to generate an optimized SQL statement corresponding to the SQL statement to be optimized based on the processing result of the analysis and understanding processing, the query data, and the interpretation data to be used.
8. A dictionary-based SQL statement optimization device, the device being used to implement the method according to any one of claims 1 to 7, characterized in that: The device comprises: An acquisition module, used to acquire an initial association relationship between each sub-scene text data and a scene database, wherein the sub-scene text data is obtained by segmenting the scene text data in the specified scene; A first association module is used to extract keywords from each sub-scene text data, and update the initial association relationship to a first association relationship with the keyword; A second association module is used to match the keyword with designated data to filter out target data from the designated data and update the target data to the first association relationship to obtain a second association relationship, wherein the designated data is data extracted from the scene database; A paraphrase module, used for fusing the scene text data with the second association relationship, generating paraphrase data corresponding to the target data, and obtaining a paraphrase dictionary; An optimization module, configured to optimize the SQL statement to be optimized by using the interpretation dictionary to generate a target SQL statement, wherein the SQL statement to be optimized is generated based on the query data in the specified scenario; The keyword includes a first keyword extracted by a specified nomenclature and a second keyword extracted by word segmentation, the target data includes a first target data and a second target data, and the keyword is matched with the specified data to filter out the target data from the specified data and update it to the first association relationship to obtain the second association relationship, including: Traversing the scene database to extract multiple specified data, the specified data including database name, table name and field name; for each of the specified data, performing longest common substring matching processing on the field name in the specified data and each first keyword to obtain a matching string; using the matching string and field name, determining first target data from the multiple specified data, and updating the first target data to the first association relationship to obtain a second intermediate relationship; for each of the specified data, performing similarity matching processing on the field name in the specified data and the second keyword to obtain matching similarity; using the matching similarity to determine second target data from the multiple specified data, and updating the second target data to the second intermediate relationship to obtain a second association relationship; The SQL statement to be optimized is optimized by using the interpretation dictionary to generate a target SQL statement, including: extracting column names from the SQL statement to be optimized, and extracting database names and table names corresponding to the column names from a target database as designated data to be queried, the target database being a database corresponding to the SQL statement to be optimized; determining interpretation data corresponding to the designated data to be queried from the interpretation dictionary as interpretation data to be used; optimizing the column names in the SQL statement to be optimized in combination with the interpretation data to be used, query data and database information of the target database to generate an optimized SQL statement; if the optimized SQL statement passes the verification process, using the optimized SQL statement as the target SQL statement corresponding to the query data.
Citation Information
Patent Citations
Cross-data-source query dynamic optimization method and system
CN116775696A
Database querying system and method
US20020107840A1