A text2sql semantic caching method based on context and pattern matching

By adopting a semantic caching method based on context and pattern matching, the problems of SQL reuse errors and response latency in the Text2SQL model are solved, achieving efficient SQL statement caching and database interaction, and improving the accuracy and response speed of Text2SQL tasks.

CN121681573BActive Publication Date: 2026-04-10QILU UNIVERSITY OF TECHNOLOGY (SHANDONG ACADEMY OF SCIENCES) +1
View PDF 2 Cites 0 Cited by

Patent Information

Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2026-02-09
Publication Date
2026-04-10

AI Technical Summary

Technical Problem

Existing Text2SQL models suffer from SQL reuse errors and high response latency when dealing with complex semantic parsing and pattern alignment, especially in multi-turn dialogues where they cannot accurately capture the logical dependencies between SQL statements.

Method used

A semantic caching method based on context and pattern matching is adopted. The core semantics of SQL are extracted through a semantic compression module, and context encoding is performed in combination with the TaBERT model. A two-stage retrieval is performed using a two-level attention mechanism and semantic and pattern matching to ensure that SQL can be reused directly when the cache is hit, thereby reducing redundant calculations.

Benefits of technology

It effectively avoids SQL reuse errors caused by missing database schema constraints, shortens response time, reduces resource consumption, and improves the accuracy and efficiency of Text2SQL tasks.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN121681573B_ABST
    Figure CN121681573B_ABST
Patent Text Reader

Abstract

The application provides a Text2SQL semantic caching method based on context and pattern matching, relates to the field of natural language processing and database interaction, and comprises the following steps: inputting a user query into a semantic compression module, extracting semantic significant keywords to obtain a candidate set, extracting and constructing a vocabulary set through a large language model, and obtaining a compressed query through a syntax organizer; inputting historical dialogues into a context encoder, obtaining a global context representation through coding and two-stage attention; performing coarse-grained filtering on the compressed query to form a latest candidate set; performing fine-grained context matching on the global context representation and the latest candidate set to obtain a context similarity; comparing the context similarity with a preset threshold to determine whether the cache hits, and if the cache hits, interacting with a database with the hit SQL statement and returning a query result. The application solves the problems of low SQL reuse accuracy and high response delay.
Need to check novelty before this filing date? Find Prior Art

Description

TECHNICAL FIELD

[0001] The application relates to the field of natural language processing and database interaction, and particularly provides a Text2SQL semantic caching method based on context and pattern matching. BACKGROUND

[0002] At present, Text2SQL models have become the core technology for connecting natural language queries and structured databases due to their advanced natural language understanding and query generation capabilities, and are widely used in practical scenarios such as intelligent customer service and data analysis. However, the reasoning process of the Text2SQL model involves complex steps such as semantic analysis, pattern alignment and SQL candidate generation, and the Text2SQL query is highly bound to the database pattern, so the same natural language query may correspond to completely different SQL statements under different database patterns.

[0003] In the prior art, only the natural language semantic similarity matching is based, the pattern constraint is often ignored, SQL reuse errors are easily caused, and database operation failures are caused; in addition, the current query in the multi-round dialogue has a strong dependence on the logical structure of the historical generated SQL statement, and the existing method often uses simple dialogue splicing to model the context, and has the problems of attention dilution or semantic flattening, and cannot accurately capture the logical dependency relationship between SQL statements.

[0004] Therefore, a special Text2SQL semantic caching method considering pattern matching and context logical dependency is needed to solve the core problems of low SQL reuse accuracy and high response delay in the prior art. SUMMARY

[0005] Therefore, in order to solve the above problems, the application provides a Text2SQL semantic caching method based on context and pattern matching, which is used for retrieving reused cached SQL statements and realizing instant response.

[0006] The method comprises the following steps:

[0007] S1. inputting a user query into a semantic compression module, extracting semantic significant keywords, obtaining a candidate set, extracting and constructing a SQL-centered vocabulary set in the candidate set as a core term through a large language model, and obtaining a compressed query through a syntax organizer;

[0008] S2. inputting a historical dialogue into a context encoder, obtaining a global context representation through a TaBERT model coding and two-level attention;

[0009] S3. performing coarse-grained filtering based on semantic and pattern matching on the compressed query to form a latest candidate set; performing fine-grained context matching on the global context representation and the latest candidate set to obtain a context similarity;

[0010] S4. Compare the context similarity with the preset threshold to determine whether the cache hits, if the cache hits, interact with the database with the hit SQL statement and return the query result; if not, return to step S1 and update the cache.

[0011] Optionally, step S1 comprises:

[0012] S11. Input user query , extract semantic significant keywords using TextRank algorithm , get candidate set , the ;

[0013] S12. Extract core terms related to SQL construction, extract and construct candidate set through large language model SQL-centric vocabulary set , get filtered core terms ;

[0014] S13. Through syntax organizer restore syntactic dependency and standard word order, restructure into logically coherent form, get compressed query .

[0015] Optionally, in step S12, the vocabulary set comprises:

[0016] SQL operation verbs, entities and relationship operators, and the filtered core terms are .

[0017] Optionally, step S13 comprises:

[0018] S131. Analyze the original query through the dependency parsing module provided by the natural language processing toolkit Spacy to extract the grammatical relationship between core terms;

[0019] S132. Adopt rule-based reordering strategy to move key operations to the front, group modifiers with central words, and place filtering conditions at the end;

[0020] S133. Get normalized sequence that is more consistent with SQL structure, output compressed query .

[0021] Optionally, step S2 comprises:

[0022] S21. Input historical dialogue to TaBERT model for encoding to get token-level embedding ;

[0023] S22. Process by two-level attention mechanism based on intra-attention and inter-attention , get global context representation .

[0024] Optionally, step S21 comprises:

[0025] S211. Input historical dialog , wherein represents the historical user natural language query, is the SQL execution statement corresponding to the query;

[0026] S212. Build an input sequence containing SQL-aware syntax tags:

[0027] ;

[0028] wherein, is the tagged , is the tagged ;

[0029] S213. Differentiate structural elements by inserting syntax tags, including SQL keywords, columns and tables;

[0030] S214. Contextual encoding of the sequence by TaBERT model, get token-level embedding .

[0031] Optionally, step S22 comprises:

[0032] S221. Input the encoded segment of the round , apply multi-head self-attention layer to the query , key , value , calculate the attention score; derive the intra-round semantic representation of the round through the [CLS] token ;

[0033] S222. Concatenate the intra-round representations of all rounds to model the dependence of the current query on the historical SQL logic in the multi-round dialog , wherein, is the round of the dialog;

[0034] S223. Input into the Transformer encoder, through the [CLS] token outputs global context representation .

[0035] Optionally, step S3 comprises:

[0036] S31. Obtain semantic similarity by cosine similarity, formula is:

[0037] ;

[0038] wherein, represents the word embedding generated by the ALBERT encoder, represents the entry corresponding natural language query;

[0039] S32. Calculate the pattern matching degree score by the Jaccard similarity of the two pattern element sets:

[0040] ;

[0041] wherein, is the pattern element set extracted from the compressed query by pattern linking of DIN-SQL; is the pattern element set extracted from the corresponding SQL statement of the cached query by static SQL parsing;

[0042] S33. Calculate the weighted average value of the entry :

[0043] ;

[0044] wherein, the hyperparameter , the entry satisfying forms the latest candidate set , wherein, .

[0045] S34. From the latest candidate set , use to perform fine-grained context matching;

[0046] S35. Calculate the context similarity of each entry :

[0047] ;

[0048] wherein, is the global context vector representation of the entry .

[0049] Optionally, in step S4:

[0050] The preset threshold is If , a cache hit occurs, the hit SQL statement is interacted with the database, and a query result is returned.

[0051] If , a cache miss occurs, and step S1 is returned and the cache is updated.

[0052] From the above technical solution, the present application has the following advantages:

[0053] By focusing on the core semantics of SQL through the semantic compression module, eliminating redundancy, combining SQL-aware syntax markers and two-level attention context encoding, capturing the logical dependency of historical SQL and current queries in multi-round dialogue, and then performing two-stage retrieval of coarse-grained filtering through semantic and pattern matching and fine-grained context matching of global representation, SQL reuse errors caused by missing database schema constraints are effectively avoided, and the strict requirement of precision for the Text2SQL task is met; coarse-grained filtering can quickly narrow the cache retrieval range, fine-grained matching ensures matching accuracy, and when the cache hits, SQL is directly reused to skip the complete reasoning process, which shortens the response time and reduces resource consumption caused by repeated calculations. BRIEF DESCRIPTION OF DRAWINGS

[0054] In order to more clearly illustrate the technical solutions of the embodiments of the present application, the drawings needed in the embodiments will be briefly introduced below. Obviously, the drawings in the following description are only some embodiments of the present application, and other drawings can be obtained by those skilled in the art without creative labor.

[0055] Figure 1 A flowchart of a Text2SQL semantic caching method based on context and pattern matching provided by the embodiments of the present application;

[0056] Figure 2 A collaboration flowchart of each module of a Text2SQL semantic caching method based on context and pattern matching provided by the embodiments of the present application. DETAILED DESCRIPTION

[0057] In the specific steps of the Text2SQL semantic caching method based on context and pattern matching which will be described in detail below, various embodiments of the present application will be described more fully. The present application can have various embodiments, and adjustments and changes can be made therein. However, it should be understood that there is no intention to limit various embodiments of the present application to the specific embodiments disclosed herein, but the present application should be understood to cover all adjustments, equivalents and / or alternatives falling within the spirit and scope of various embodiments of the present application.

[0058] It should be understood that the word "comprise" or "comprising", when used in this specification, signifies the presence of the stated features, integers, steps, operations, elements, or components but does not preclude the presence or addition of one or more other features, integers, steps, operations, elements, components, or groups thereof. The terms "comprise", "comprising", "have", "having", "include", "including" and "contain", "containing" are also meant not to be limiting and do not exclude other elements or steps.

[0059] The expressions "one embodiment", "some embodiments", "an embodiment", "embodiments", and the like as used throughout this disclosure mean "one or more embodiments", unless expressly specified otherwise.

[0060] In order to make the inventive objectives, characteristics and advantages of the present application more obvious and easy to understand, the technical solutions protected by the present application will be described clearly and completely below by using specific embodiments and drawings. Obviously, the following described embodiments are only some of the embodiments of the present application, but not all the embodiments. Based on the embodiments in the present application, all other embodiments obtained by those skilled in the art without creative labor fall within the scope of protection of the present application.

[0061] Please refer to Figure 1 As shown in the figure, it is a flowchart of a Text2SQL semantic caching method based on context and pattern matching, and the method comprises the following steps:

[0062] S1. Input the user query into the semantic compression module, extract the semantic significant keywords, obtain the candidate set, extract and construct the SQL-centered vocabulary set in the candidate set as the core term through the large language model, and obtain the compressed query through the syntax organizer;

[0063] S2. Input the historical dialogue into the context encoder, obtain the global context representation through the TaBERT model coding and two-level attention;

[0064] S3. Perform coarse-grained filtering based on semantic and pattern matching on the compressed query to form the latest candidate set; perform fine-grained context matching on the global context representation and the latest candidate set to obtain the context similarity;

[0065] S4. Compare the context similarity with the preset threshold value to determine whether the cache hits, if the cache hits, interact the hit SQL statement with the database and return the query result; if not, return to step S1 and update the cache.

[0066] It should be noted that the embodiment eliminates vocabulary redundancy while retaining SQL key information through the semantic compression module, extracts the core intent of the natural language query to preprocess the user query; introduce the context encoder, integrate the structural features of the historical SQL into the context modeling, accurately capture the logical dependence of the current and historical queries in the multi-round interaction; build a two-stage retrieval architecture, combine the database schema coarse-grained filtering and the context-based fine-grained verification, and balance the retrieval efficiency and reuse accuracy.

[0067] As a refinement and extension of the above embodiment, in order to completely describe the specific implementation process in the embodiment, another context and schema matching based Text2SQL semantic caching method is provided, as shown in Figure 2 The cooperation process schematic diagram of each module in the embodiment is shown, and the method comprises the following steps:

[0068] S1. input the user query into the semantic compression module, extract the semantic significant keywords, obtain the candidate set, extract and construct the SQL-centered vocabulary set in the candidate set as the core term through the large language model, and obtain the compressed query through the syntax organizer;

[0069] In the embodiment of the application, step S1 comprises:

[0070] S11. input the user query , extract the semantic significant keywords using the TextRank algorithm , obtain the candidate set , the ;

[0071] S12. extract the core term related to SQL construction, extract and construct the SQL-centered vocabulary set in the candidate set through the large language model , obtain the filtered core term ;

[0072] In the embodiment of the application, in step S12, the vocabulary set comprises:

[0073] SQL operation verbs, entities and relationship operators, and the filtered core term is .

[0074] S13. restore the syntax dependency and standard word order through the syntax organizer , reconfigure to a logically coherent form, and obtain the compressed query .

[0075] In the embodiment of the application, step S13 comprises:​

[0076] S131. Analyze the original query by a dependency parsing module provided by a natural language processing toolkit Spacy, and extract the grammatical relationship between core terms;

[0077] S132. Adopt a rule-based reordering strategy to move key operations to the front end, group modifiers with center words, and place filtering conditions at the end;

[0078] S133. Obtain a normalized sequence that is more suitable for SQL structure, and output a compressed query .

[0079] S2. Input the historical dialogue into a context encoder, encode by a TaBERT model, and obtain a global context representation through two-level attention;

[0080] In the embodiment of the application, step S2 comprises:

[0081] S21. Input the historical dialogue into the TaBERT model for encoding to obtain token-level embedding .

[0082] In the embodiment of the application, step S21 comprises:

[0083] S211. Input the historical dialogue , wherein represents the th historical user natural language query, is a SQL execution statement corresponding to the query;

[0084] Model the semantic association between the user query and the corresponding SQL statement in a single round of dialogue, for example, the alignment of age greater than 25 and WHERE age>25;

[0085] S212. Construct an input sequence containing SQL-aware syntax tags:

[0086] .

[0087] wherein, is a tagged , is a tagged .

[0088] S213. Differentiate structural elements including SQL keywords, columns and tables by inserting syntax tags;

[0089] S214. Contextually encode the sequence by a TaBERT model to obtain token-level embedding .

[0090] S22. processing by a two-level attention mechanism based on intra-wheel attention and inter-wheel attention to obtain a global context representation .

[0091] In the embodiment of the application, step S22 comprises:

[0092] S221. inputting the encoded segments of the first wheel , applying a multi-head self-attention layer to the input, calculating attention scores of queries , keys , and values , and deriving an intra-wheel semantic representation of the first wheel through a [CLS] token ; S222. concatenating the intra-wheel representations of all wheels to model the dependence of the current query on the historical SQL logic in the multi-wheel dialogue , wherein, is the dialogue wheel;

[0093] S223. inputting the into a Transformer encoder, and outputting a global context representation through a [CLS] token .

[0094] S3. performing coarse-grained filtering based on semantic and pattern matching on the compressed query to form a latest candidate set, and performing fine-grained context matching on the global context representation and the latest candidate set to obtain a context similarity In the embodiment of the application, step S3 comprises: S31. obtaining a semantic similarity by cosine similarity, and the formula is:

[0095]

[0096] ;

[0097] wherein, represents a word embedding generated by an ALBERT encoder,

[0098] represents a natural language query corresponding to the entry ;

[0099] S32. calculating a pattern matching score by a Jaccard similarity of two pattern element sets: ;

[0100]

[0101] ;

[0102] ​​​​wherein, is the set of pattern elements extracted from the compressed query by pattern linking of the compressed query via the mode of the DIN-SQL ; is the set of pattern elements extracted from the cached query by static SQL parsing of the cached query via the mode of the DIN-SQL ;

[0103] S33. Calculate the weighted average of the entries :

[0104] ;

[0105] wherein the hyperparameter , retains the entries satisfying to form the latest candidate set , wherein .

[0106] S34. From the latest candidate set , perform fine-grained context matching using ;

[0107] S35. Calculate the context similarity for each entry :

[0108] ;

[0109] wherein is the global context vector representation of the entry .

[0110] S4. Compare the context similarity with a preset threshold value to determine whether the cache hits, if the cache hits, interact the hit SQL statement with the database and return the query result; if not, return to step S1 and update the cache.

[0111] In the embodiment of the application, in step S4:

[0112] the preset threshold value is , if , the cache hits, the hit SQL statement is interacted with the database and the query result is returned;

[0113] if , the cache does not hit, step S1 is returned and the cache is updated.

[0114] The foregoing description of the disclosed embodiments enables a person skilled in the art to make or use the application. Modifications of these embodiments will occur to persons of skill in the art, and that the appended claims are intended to cover all such modifications that do not depart from the true spirit and scope of the application. Therefore, the application is not limited to the embodiments shown but is to be accorded the widest scope consistent with the principles and novel features disclosed herein.

Claims

1.A Text2SQL semantic caching method based on context and pattern matching, characterized in that, The method comprises the following steps: S1. inputting a user query into a semantic compression module, extracting a semantic significant keyword, obtaining a candidate set, extracting and constructing a SQL-centered vocabulary set in the candidate set as a core term through a large language model, and obtaining a compressed query through a syntax organizer; S2. inputting a historical dialogue into a context encoder, obtaining a global context representation through a TaBERT model coding and two-stage attention; S3. performing coarse-grained filtering based on semantic and pattern matching on the compressed query to form a latest candidate set; performing fine-grained context matching on the global context representation and the latest candidate set to obtain a context similarity; S4. comparing the context similarity with a preset threshold value, judging whether the cache hits, if the cache hits, interacting the hit SQL statement with a database, and returning a query result; if not, returning to step S1 and updating the cache; Step S2 comprises: S21. Input history dialogue to the TaBERT model to get token-level embeddings ; S22. processing by a two-stage attention mechanism based on intra-wheel attention and inter-wheel attention , to obtain a global context representation ; Step S21 comprises: S211. Input history conversation wherein represents the th historical user natural language query, is a SQL execution statement corresponding to the query; S212. constructing an input sequence containing SQL-aware syntax tags: ; wherein is labeled , is labeled ; S213. distinguishing structural elements including SQL keywords, columns and tables by inserting syntax labels; S214. Contextualize the sequence by a TaBERT model to get token-level embedding ; Step S22 comprises: S221. Input the... wheel code fragment ,right Apply a multi-head self-attention layer to compute the query. ,key ,value Attention score; through Exporting the [CLS] token In-wheel semantic representation of a wheel ; S222. Concatenate the in-round representations of all rounds, modeling the dependency of the current query and the historical SQL logic in the multi-round dialogue wherein, is a dialogue round; S223. Will In the input Transformer encoder, the global context representation is output by the [CLS] token . 2.The context and pattern matching based Text2SQL semantic caching method of claim 1, wherein, Step S1 comprises: S11. Input a user query , extract semantic significant keywords using TextRank algorithm , get candidate set , the ; S12. Extract core terms related to SQL construction, extract and construct candidate set through large language model SQL-centric vocabulary set , get filtered core terms ; S13. By a syntax organizer restore the syntactic dependencies and canonical word order, and reconstruct into a logically coherent form, obtaining a compressed query . 3.The context and pattern matching based Text2SQL semantic caching method of claim 2, wherein, In step S12, the vocabulary set includes: SQL operation verbs, entities and relationship operators, filtered core terms are . 4.The context and pattern matching based Text2SQL semantic caching method of claim 2, wherein, Step S13 comprises: S131. parsing the original query through a dependency parsing module provided by a natural language processing toolkit Spacy to extract the grammatical relationship between the core terms; S132. adopting a rule-based reordering strategy to move the key operation to the front end, group the modifiers with the center words, and place the filtering conditions at the end; S133. Obtain normalized sequence that fits SQL structure better, output compressed query . 5.The context and pattern matching based Text2SQL semantic caching method of claim 1, wherein, Step S3 comprises: S31. obtaining a semantic similarity through a cosine similarity, and the formula is: ; wherein, denotes a word embedding generated by an ALBERT encoder, denotes an entry a corresponding natural language query; S32. calculating a pattern matching score through a Jaccard similarity of two pattern element sets: ; in, To link from compressed queries via DIN-SQL schema The extracted set of pattern elements; To query from cache via static SQL parsing The set of pattern elements extracted from the corresponding SQL statement; S33. Calculate entry weighted average of the values: ; wherein the hyperparameters are reserved to form the latest candidate set wherein the entries satisfying are reserved ; S34. From the latest candidate set In China, utilizing Perform fine-grained context matching; S35. For each entry Compute context similarity: ; wherein, is a global context vector representation of the entry . 6.The context and pattern matching based Text2SQL semantic caching method of claim 1, wherein, In step S4: The preset threshold is If , a cache hit occurs, the hit SQL statement is interacted with the database, and a query result is returned. If then there is a cache miss and the process returns to step SI and the cache is updated.

Citation Information

Patent Citations

  • Techniques for efficient encoding in neural semantic parsing systems

    US20250225129A1

  • Method, apparatus and system for graph data caching

    WO2023028780A1