A KV-based database logical plan caching method and device
By adopting the KV-based logical plan caching method in the distributed NewSQL database, parsing and caching SQL sentence templates and table information, and recording the relationship between KV constant expressions and user input variables, the reuse and update of logical plans are realized and the solution is solved It solves the problem that non-template mode SQL statements cannot be reused, and improves system resource utilization and database performance.
Patent Information
- Application Number
- CN202210382223.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-04-13
- Publication Date
- 2024-10-29
- Estimated Expiration
- 2042-04-13
AI Technical Summary
In a distributed NewSQL database, non-template mode SQL statements cannot reuse existing logical plans due to the variable number of constants corresponding to variables in SQL, resulting in a large amount of system resources being consumed under high load conditions and reducing the system's carrying capacity.
The database logical plan caching method based on KV is used to parse user SQL request text, extract user input variables, generate SQL sentence templates and related table information, compile logical plans, record the relationship between KV constant expressions and user input variables, and dynamically adjust And optimization generates a complete logical plan, and caches it through index and reverse index to realize the reuse and update of the logical plan.
It improves SQL execution efficiency, improves database access performance, reduces system resource loss, and enhances the system's carrying capacity.
Smart Images

Figure CN114610724B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of database SQL computing technology, and in particular to a KV-based database logic plan caching method and device. Background Art
[0002] In the distributed NEWSQL database management system, the plan of the SQL engine must go through plan optimization operations such as RBO and CBO to finally obtain a complete logical plan that can be executed. The planning process is a time-consuming calculation process. At present, the plan cache of most databases is only for SQL queries in template mode, but for SQL statements executed in non-template mode, the number of constants corresponding to the variables in the SQL cannot be reused. Under high load conditions, a large amount of system resources will be consumed, reducing the system's carrying capacity. Therefore, it is necessary to increase the cache of such plans to improve the system's carrying capacity and reduce repeated waste of system resources.
[0003] In order to solve the problem that the existing logical plan cannot be reused when users frequently use non-template SQL requests in the existing distributed NewSQL database, the present invention proposes a KV-based database logical plan caching method and device. Summary of the invention
[0004] In order to overcome the defects of the prior art, the present invention provides a simple and efficient KV-based database logical plan caching method and device.
[0005] The present invention is achieved through the following technical solutions:
[0006] A KV-based database logical plan caching method and device, characterized by comprising the following steps:
[0007] Step S1: Creation of logical plan cache
[0008] Step S1.1, parse the user SQL request text, extract the user input variables, generate SQL sentence template and related table information;
[0009] Step S1.2, compile the logic plan, extract the KV constant expression in the logic plan, and record its relationship with the user input variable;
[0010] Step S1.3, generate a complete logical plan through RBO dynamic adjustment and CBO optimization;
[0011] Step S1.4, generate an index according to the generated SQL sentence template, and cache the logical plan according to the index;
[0012] Step S1.5, generating a reverse index cache according to the SQL sentence template related table information;
[0013] Step S2: Use of logical plan cache
[0014] Step S2.1, parse the subsequently received user SQL request text, extract the user input variables, and generate the SQL sentence template and related table information;
[0015] Step S2.2: Use the SQL sentence template to query the logical plan cache and obtain the corresponding logical plan and KV constant expression;
[0016] Step S2.3, recalculate the KV constant expression with the newly extracted user input variable, and replace the corresponding KV value in the logic plan to generate a new logic plan;
[0017] Step S3: Update of logical plan cache
[0018] After the distributed NEWSQL database management system creates a complete logical plan cache based on KV, if the table structure or number of table rows related to the cached logical plan changes, causing the corresponding statistical information to change, the reverse index is used to find the logical plan related to the changed table and eliminate it, clear the corresponding storage space, and update the logical plan cache.
[0019] In the step S1, when creating a distributed NEWSQL database management system based on KV logical plan cache, the SQL sentence template of the user SQL request text is extracted, and the input variables requested by the user in the user SQL request text are removed, and the input variables requested by the user are recorded separately and numbered sequentially, and only the basic sentence of the user SQL request text is retained as the index key value of the logical plan cache;
[0020] Perform lexical, grammatical and semantic analysis on the user's SQL request text, generate the corresponding AST tree, extract relevant table information as the key value of the reverse index, and generate a complete logical plan through RBO dynamic adjustment and CBO optimization conversion.
[0021] In the process of generating a logical plan, the relationship between all KV constant expressions and user input variables in the logical plan is recorded, and the extracted index key values, user input variables, KV constant expressions, complete logical plan, and the position of constant expressions in the logical plan are uniformly stored in the memory cache, and a logical plan cache is created for the query item with the index key value;
[0022] Create a reverse index based on the table information related to the logical plan, and use the table information to point to the corresponding logical plan to update the corresponding logical plan.
[0023] In the step S2, after the distributed NEWSQL database management system completes the creation of the KV-based logical plan cache, it continues to extract the SQL sentence template from the subsequently received user SQL request text, removes the user requested input variables in the user SQL text, records the user requested input variables separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logical plan cache;
[0024] Use the index key value to match the query item in the logical plan cache. If a match is found, directly obtain the logical plan corresponding to the SQL sentence and the related KV constant expression, and then enter the newly extracted user input variable to get a new SQL logical plan. If no match is found, start the plan cache creation process.
[0025] A KV-based database logical plan cache device, characterized by: comprising a logical plan cache creation module, a logical plan cache use module and a logical plan cache update;
[0026] The logical plan cache creation module is responsible for parsing the user SQL request text, extracting user input variables, generating SQL sentence templates and related table information; compiling the logical plan, extracting the KV constant expression in the logical plan, and recording its relationship with the user input variables; generating a complete logical plan through RBO dynamic adjustment and CBO optimization; generating an index based on the generated SQL sentence template, and caching the logical plan based on the index; generating a reverse index cache based on the SQL sentence template related table information;
[0027] The logic plan cache usage module is responsible for parsing the subsequently received user SQL request text, extracting user input variables, generating SQL sentence templates and related table information; using the SQL sentence template to query the logic plan cache, obtaining the corresponding logic plan and KV constant expression; recalculating the KV constant expression with the newly extracted user input variables, and replacing the corresponding KV value in the logic plan to generate a new logic plan;
[0028] The logical plan cache update module is responsible for using the reverse index to find the logical plan related to the changed table and eliminate it, clearing the corresponding storage space and updating the plan cache when the table structure or the number of table rows related to the cached logical plan changes, resulting in a change in the corresponding statistical information.
[0029] When extracting the SQL sentence template of the user SQL request text, the logic plan cache creation module removes the input variables requested by the user in the user SQL request text, records the input variables requested by the user separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logic plan cache;
[0030] The logical plan cache creation module performs lexical, grammatical and semantic analysis on the user SQL request text, generates a corresponding AST tree, extracts relevant table information as the key value of the reverse index, and generates a complete logical plan through RBO dynamic adjustment and CBO optimization conversion.
[0031] When generating a logical plan, the logical plan cache creation module records the relationship between all KV constant expressions and user input variables in the logical plan, stores the extracted index key values, user input variables, KV constant expressions, complete logical plan and the position of constant expressions in the logical plan in a memory cache, and creates a logical plan cache for the query item with the index key value;
[0032] Create a reverse index based on the table information related to the logical plan, and use the table information to point to the corresponding logical plan to update the corresponding logical plan.
[0033] The logic plan cache using module continues to extract SQL sentence templates from subsequently received user SQL request texts, removes the user requested input variables from the user SQL texts, records the user requested input variables separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logic plan cache;
[0034] Use the index key value to match the query item in the logical plan cache. If a match is found, directly obtain the logical plan corresponding to the SQL sentence and the related KV constant expression, and then enter the newly extracted user input variable to get a new SQL logical plan. If no match is found, start the plan cache creation process.
[0035] The beneficial effects of the present invention are as follows: the KV-based database logical plan caching method and device not only realizes the logical plan caching under the distributed NEWSQL database management system, but also realizes the logical plan reuse of user-requested SQL, further improves the SQL execution efficiency, improves the database access performance, and reduces the system resource loss. BRIEF DESCRIPTION OF THE DRAWINGS
[0036] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative work.
[0037] Attached Figure 1 It is a schematic diagram of the KV-based database logic plan caching method of the present invention. DETAILED DESCRIPTION
[0038] In order to enable those skilled in the art to better understand the technical solutions in the present invention, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the embodiments of the present invention. Obviously, the described embodiments are only a 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 ordinary technicians in this field without creative work should fall within the scope of protection of the present invention.
[0039] CBO: Cost-based SQL execution plan optimizer. The optimizer will dynamically adjust the execution plan based on existing statistical information.
[0040] KV storage: Each row of data in a table consists of two parts: a key and a value. The key consists of a timestamp and a search index for the value. Using the index of the data as a prefix, you can traverse all rows with the index, that is, the value.
[0041] RBO: Rule-Based Optimization, also known as "rule-based optimizer", determines the SQL execution plan according to a series of rules hard-coded in the database.
[0042] Abstract Syntax Tree (AST), or Syntax Tree for short: is an abstract representation of the syntax structure of source code. It represents the syntax structure of a programming language in a tree-like form, and each node in the tree represents a structure in the source code.
[0043] The KV-based database logical plan caching method includes the following steps:
[0044] Step S1: Creation of logical plan cache
[0045] Step S1.1, parse the user SQL request text, extract the user input variables, generate SQL sentence template and related table information;
[0046] Step S1.2, compile the logic plan, extract the KV constant expression in the logic plan, and record its relationship with the user input variable;
[0047] Step S1.3, generate a complete logical plan through RBO dynamic adjustment and CBO optimization;
[0048] Step S1.4, generate an index according to the generated SQL sentence template, and cache the logical plan according to the index;
[0049] Step S1.5, generating a reverse index cache according to the SQL sentence template related table information;
[0050] Step S2: Use of logical plan cache
[0051] Step S2.1, parse the subsequently received user SQL request text, extract the user input variables, and generate the SQL sentence template and related table information;
[0052] Step S2.2: Use the SQL sentence template to query the logical plan cache and obtain the corresponding logical plan and KV constant expression;
[0053] Step S2.3, recalculate the KV constant expression with the newly extracted user input variable, and replace the corresponding KV value in the logic plan to generate a new logic plan;
[0054] Step S3: Update of logical plan cache
[0055] After the distributed NEWSQL database management system creates a complete logical plan cache based on KV, if the table structure or number of table rows related to the cached logical plan changes, causing the corresponding statistical information to change, the reverse index is used to find the logical plan related to the changed table and eliminate it, clear the corresponding storage space, and update the logical plan cache.
[0056] In the step S1, when creating a distributed NEWSQL database management system based on KV logical plan cache, the SQL sentence template of the user SQL request text is extracted, and the input variables requested by the user in the user SQL request text are removed, the input variables requested by the user are recorded separately and numbered sequentially, and only the basic sentence of the user SQL request text is retained as the index key value of the logical plan cache;
[0057] Perform lexical, grammatical and semantic analysis on the user's SQL request text, generate the corresponding AST tree, extract relevant table information as the key value of the reverse index, and generate a complete logical plan through RBO dynamic adjustment and CBO optimization conversion.
[0058] In the process of generating a logical plan, the relationship between all KV constant expressions and user input variables in the logical plan is recorded, and the extracted index key values, user input variables, KV constant expressions, complete logical plan, and the position of constant expressions in the logical plan are uniformly stored in the memory cache, and a logical plan cache is created for the query item with the index key value;
[0059] Create a reverse index based on the table information related to the logical plan, and use the table information to point to the corresponding logical plan to update the corresponding logical plan.
[0060] In the step S2, after the distributed NEWSQL database management system completes the creation of the KV-based logical plan cache, it continues to extract the SQL sentence template from the subsequently received user SQL request text, removes the user requested input variables in the user SQL text, records the user requested input variables separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logical plan cache;
[0061] Use the index key value to match the query item in the logical plan cache. If a match is found, directly obtain the logical plan corresponding to the SQL sentence and the related KV constant expression, and then enter the newly extracted user input variable to get a new SQL logical plan. If no match is found, start the plan cache creation process.
[0062] The KV-based database logical plan cache device includes a logical plan cache creation module, a logical plan cache use module and a logical plan cache update;
[0063] The logical plan cache creation module is responsible for parsing the user SQL request text, extracting user input variables, generating SQL sentence templates and related table information; compiling the logical plan, extracting the KV constant expression in the logical plan, and recording its relationship with the user input variables; generating a complete logical plan through RBO dynamic adjustment and CBO optimization; generating an index based on the generated SQL sentence template, and caching the logical plan based on the index; generating a reverse index cache based on the SQL sentence template related table information;
[0064] The logic plan cache usage module is responsible for parsing the subsequently received user SQL request text, extracting user input variables, generating SQL sentence templates and related table information; using the SQL sentence template to query the logic plan cache, obtaining the corresponding logic plan and KV constant expression; recalculating the KV constant expression with the newly extracted user input variables, and replacing the corresponding KV value in the logic plan to generate a new logic plan;
[0065] The logical plan cache update module is responsible for using the reverse index to find the logical plan related to the changed table and eliminate it, clearing the corresponding storage space and updating the plan cache when the table structure or the number of table rows related to the cached logical plan changes, resulting in a change in the corresponding statistical information.
[0066] When extracting the SQL sentence template of the user SQL request text, the logic plan cache creation module removes the input variables requested by the user in the user SQL request text, records the input variables requested by the user separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logic plan cache;
[0067] The logical plan cache creation module performs lexical, grammatical and semantic analysis on the user SQL request text, generates a corresponding AST tree, extracts relevant table information as the key value of the reverse index, and generates a complete logical plan through RBO dynamic adjustment and CBO optimization conversion.
[0068] When generating a logical plan, the logical plan cache creation module records the relationship between all KV constant expressions and user input variables in the logical plan, stores the extracted index key values, user input variables, KV constant expressions, complete logical plan and the position of constant expressions in the logical plan in a memory cache, and creates a logical plan cache for the query item with the index key value;
[0069] Create a reverse index based on the table information related to the logical plan, and use the table information to point to the corresponding logical plan to update the corresponding logical plan.
[0070] The logic plan cache using module continues to extract SQL sentence templates from subsequently received user SQL request texts, removes the user requested input variables from the user SQL texts, records the user requested input variables separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logic plan cache;
[0071] Use the index key value to match the query item in the logical plan cache. If a match is found, directly obtain the logical plan corresponding to the SQL sentence and the related KV constant expression, and then enter the newly extracted user input variable to get a new SQL logical plan. If no match is found, start the plan cache creation process.
[0072] The embodiment described above is only one specific implementation of the present invention. Common changes and substitutions made by those skilled in the art within the scope of the technical solution of the present invention should be included in the protection scope of the present invention.
Claims
1. A KV-based database logical plan caching method, characterized in that: The following steps are involved: Step S1: Creation of logical plan cache Step S1.1, parse the user SQL request text, extract the user input variables, generate SQL sentence template and related table information; Step S1.2, compile the logic plan, extract the KV constant expression in the logic plan, and record its relationship with the user input variable; Step S1.3, generate a complete logical plan through RBO dynamic adjustment and CBO optimization; Step S1.4, generate an index according to the generated SQL sentence template, and cache the logical plan according to the index; Step S1.5, generating a reverse index cache according to the SQL sentence template related table information; Step S2: Use of logical plan cache Step S2.1, parse the subsequently received user SQL request text, extract the user input variables, and generate the SQL sentence template and related table information; Step S2.2: Use the SQL sentence template to query the logical plan cache and obtain the corresponding logical plan and KV constant expression; Step S2.3, recalculate the KV constant expression with the newly extracted user input variable, and replace the corresponding KV value in the logic plan to generate a new logic plan; Step S3: Update of logical plan cache After the distributed NEWSQL database management system creates a complete logical plan cache based on KV, if the table structure or number of table rows related to the cached logical plan changes, causing the corresponding statistical information to change, the reverse index is used to find the logical plan related to the changed table and eliminate it, clear the corresponding storage space, and update the logical plan cache.
2. The KV-based database logical plan caching method according to claim 1, characterized in that: In the step S1, when creating a distributed NEWSQL database management system based on KV logical plan cache, the SQL sentence template of the user SQL request text is extracted, and the input variables requested by the user in the user SQL request text are removed, the input variables requested by the user are recorded separately and numbered sequentially, and only the basic sentence of the user SQL request text is retained as the index key value of the logical plan cache; Perform lexical, grammatical and semantic analysis on the user's SQL request text, generate the corresponding AST tree, extract relevant table information as the key value of the reverse index, and generate a complete logical plan through RBO dynamic adjustment and CBO optimization conversion.
3. The KV-based database logical plan caching method according to claim 2, characterized in that: In the process of generating a logical plan, the relationship between all KV constant expressions and user input variables in the logical plan is recorded, and the extracted index key values, user input variables, KV constant expressions, complete logical plan, and the position of constant expressions in the logical plan are uniformly stored in the memory cache, and a logical plan cache is created for the query item with the index key value; Create a reverse index based on the table information related to the logical plan, and use the table information to point to the corresponding logical plan to update the corresponding logical plan.
4. The KV-based database logical plan caching method according to claim 1, characterized in that: In the step S2, after the distributed NEWSQL database management system completes the creation of the KV-based logical plan cache, it continues to extract the SQL sentence template from the subsequently received user SQL request text, removes the user requested input variables in the user SQL text, records the user requested input variables separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logical plan cache; Use the index key value to match the query item in the logical plan cache. If a match is found, directly obtain the logical plan corresponding to the SQL sentence and the related KV constant expression, and then enter the newly extracted user input variable to get the new SQL logical plan. If there is no match, the plan cache creation process begins.
5. A KV-based database logical plan caching device, characterized in that: Includes logical plan cache creation module, logical plan cache usage module and logical plan cache update; The logical plan cache creation module is responsible for parsing the user SQL request text, extracting user input variables, and generating SQL sentence templates and related table information; Compile the logical plan, extract the KV constant expression in the logical plan, and record its relationship with the user input variable; Generate a complete logical plan through RBO dynamic adjustment and CBO optimization; Generate indexes based on the generated SQL sentence templates and cache logical plans based on the indexes; Generate a reverse index cache based on the SQL sentence template related table information; The logic plan cache usage module is responsible for parsing the subsequently received user SQL request text, extracting user input variables, and generating SQL sentence templates and related table information; Use the SQL sentence template to query the logical plan cache and obtain the corresponding logical plan and KV constant expression; recalculate the KV constant expression with the newly extracted user input variable and replace the corresponding KV value in the logical plan to generate a new logical plan; The logical plan cache update module is responsible for using the reverse index to find the logical plan related to the changed table and eliminate it, clearing the corresponding storage space and updating the plan cache when the table structure or the number of table rows related to the cached logical plan changes, resulting in a change in the corresponding statistical information.
6. The KV-based database logical plan caching device according to claim 5, characterized in that: When extracting the SQL sentence template of the user SQL request text, the logic plan cache creation module removes the input variables requested by the user in the user SQL request text, records the input variables requested by the user separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logic plan cache; The logical plan cache creation module performs lexical, grammatical and semantic analysis on the user SQL request text, generates a corresponding AST tree, extracts relevant table information as the key value of the reverse index, and generates a complete logical plan through RBO dynamic adjustment and CBO optimization conversion.
7. The KV-based database logical plan caching device according to claim 6, characterized in that: When generating a logical plan, the logical plan cache creation module records the relationship between all KV constant expressions and user input variables in the logical plan, stores the extracted index key values, user input variables, KV constant expressions, complete logical plan and the position of constant expressions in the logical plan in a memory cache, and creates a logical plan cache for the query item with the index key value; Create a reverse index based on the table information related to the logical plan, and use the table information to point to the corresponding logical plan to update the corresponding logical plan.
8. The KV-based database logical plan caching device according to claim 5 or 7, characterized in that: The logic plan cache using module continues to extract SQL sentence templates from subsequently received user SQL request texts, removes the user requested input variables from the user SQL texts, records the user requested input variables separately and numbers them sequentially, and only retains the basic sentence of the user SQL request text as the index key value of the logic plan cache; Use the index key value to match the query item in the logical plan cache. If a match is found, directly obtain the logical plan corresponding to the SQL sentence and the related KV constant expression, and then enter the newly extracted user input variable to get the new SQL logical plan. If there is no match, the plan cache creation process begins.
Citation Information
Patent Citations
Interactive large data analysis query processing method
CN105279286A
Realization method of full-text retrieval based on Spark framework
CN107943952A
Systems And Methods For Processing Structured Queries Over Clusters
US20220050844A1
Methods and systems for database statement execution plan optimization
US7092931B1
