Database SQL query optimization method, terminal and storage medium

By obtaining and utilizing the latest metadata information for SQL query optimization and dynamically adjusting query strategies, the problem of traditional methods being unable to adapt to changes in the data environment is solved, and database query performance and efficiency are improved.

CN118410068BActive Publication Date: 2024-09-20TIANJIN NANKAI UNIV GENERAL DATA TECH
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202410874325.6
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2024-07-02
Publication Date
2024-09-20
Estimated Expiration
2044-07-02

AI Technical Summary

Technical Problem

Traditional database query optimization methods cannot fully adapt to rapid changes in the data environment, resulting in reduced query performance and processing efficiency.

Method used

By obtaining the SQL query statement to be processed, converting it into an abstract syntax tree, obtaining the latest metadata information, generating a metadata-enhanced abstract syntax tree, dynamically reorganizing and optimizing the query, dynamically adjusting the query strategy to adapt to changes in the data environment.

Benefits of technology

It improves query performance and processing efficiency, and can intelligently and dynamically adjust and optimize SQL query statements to adapt to the changing data environment.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN118410068B
    Figure CN118410068B
Patent Text Reader

Abstract

The present invention provides a database SQL query optimization method, terminal and storage medium, and relates to the field of database. The method includes: obtaining an SQL query statement to be processed, and converting the SQL query statement into a corresponding abstract syntax tree; obtaining the latest metadata information based on a metadata query interface; generating an abstract syntax tree enhanced with metadata according to the latest metadata information and the abstract syntax tree; performing dynamic query reorganization and optimization based on the abstract syntax tree enhanced with metadata to obtain a reorganized abstract syntax tree; and executing the SQL query statement based on the reorganized abstract syntax tree. The present application can intelligently and dynamically adjust and optimize SQL query statements based on the latest metadata information, can adapt to the ever-changing data environment, and can thereby improve query performance and processing efficiency.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the field of database technology, and in particular to a database SQL query optimization method, a terminal and a storage medium. Background Art

[0002] With the development of database technology and the surge in data volume, database query optimization has become a key technology in database management systems. Traditional database query optimization relies on static, pre-defined rules and statistical information, which often performs poorly in situations where data distribution changes and query patterns are diverse. Especially when faced with big data and high-concurrency query requirements, this static optimization strategy cannot fully adapt to the rapid changes in the data environment, resulting in reduced query performance and processing efficiency. Summary of the invention

[0003] The embodiments of the present invention provide a database SQL query optimization method, a terminal and a storage medium to solve the problem that the existing methods cannot fully adapt to the rapid changes in the data environment, resulting in decreased query performance and processing efficiency.

[0004] In a first aspect, an embodiment of the present invention provides a database SQL query optimization method, comprising:

[0005] Obtain the SQL query statement to be processed, and convert the SQL query statement into a corresponding abstract syntax tree;

[0006] Based on the metadata query interface, obtain the latest metadata information;

[0007] Generate an abstract syntax tree with metadata enhancement according to the latest metadata information and abstract syntax tree;

[0008] Based on the abstract syntax tree enhanced by metadata, query dynamic reorganization and optimization are performed to obtain a reorganized abstract syntax tree;

[0009] Execute SQL query statements based on the reorganized abstract syntax tree.

[0010] In a possible implementation, the metadata query interface is an interface provided by the metadata analysis component for querying the latest metadata information;

[0011] The metadata analysis component monitors database changes in event subscription mode through metadata listeners, and records database change information in a differential manner when monitoring database change information. It also adopts delayed loading and lazy update strategies to load and update database change information when the database change information is actually applied to SQL query optimization.

[0012] In a possible implementation, the metadata analysis component dynamically determines whether to retain or eliminate metadata in the memory cache according to the access frequency and modification frequency of the metadata, and ensures the atomicity and consistency of metadata changes through transaction log analysis or a database write-ahead log;

[0013] Among them, the metadata analysis component retains metadata with an access frequency higher than a first preset access frequency and / or a modification frequency lower than the first preset modification frequency in the memory cache, and eliminates metadata with an access frequency lower than a second preset access frequency and / or a modification frequency higher than the second preset modification frequency; the first preset access frequency is greater than or equal to the second preset access frequency, and the first preset modification frequency is less than or equal to the second preset modification frequency.

[0014] In a possible implementation, based on the abstract syntax tree enhanced by the metadata, the query is dynamically reorganized and optimized to obtain a reorganized abstract syntax tree, including:

[0015] If a subquery or view exists, the target table on which the subquery or view depends is determined according to the latest metadata information, and the subquery or view is expanded when the access frequency of the target table is higher than a third preset access frequency, the index of the target table meets the preset efficient index condition, the data volume of the target table is less than the preset data volume, the data change frequency of the target table is lower than the first preset change frequency, or the result set size after executing the subquery or view is less than the preset size;

[0016] If there is an aggregation operation, then based on the statistical information in the latest metadata information, unnecessary data is filtered out in advance before the aggregation operation to reduce the amount of data input for the aggregation operation;

[0017] If there is a sorting operation, determine whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted according to the latest metadata information, and determine the sorting strategy according to whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted;

[0018] If the database supports column storage, adjust the storage method of the data in the database according to the latest metadata information.

[0019] In a possible implementation, based on the abstract syntax tree enhanced by the metadata, query dynamic reorganization and optimization are performed to obtain a reorganized abstract syntax tree, which also includes:

[0020] If there is a WHERE clause, reorder the predicates in descending order of index selectivity in the latest metadata information;

[0021] If there are multiple table connections, adjust the order and strategy of the multiple table connections based on the table size information, index coverage information and / or distribution characteristics of the data in the table in the latest metadata information;

[0022] Based on the latest metadata information, determine whether to use index scanning during the query process, and for composite indexes, analyze the matching degree between the columns in the query conditions and the index columns in the composite index, and determine the most effective index utilization strategy in the composite index;

[0023] According to the partition information in the latest metadata information, the query operation is performed on the partitions related to the query condition.

[0024] In a possible implementation, executing a SQL query statement based on the reorganized abstract syntax tree includes:

[0025] Generate multiple execution plans based on the reorganized abstract syntax tree;

[0026] Select the target execution plan with the lowest execution cost from multiple execution plans, and execute the SQL query statement according to the target execution plan;

[0027] Get real-time performance indicators during the execution of SQL query statements;

[0028] If the real-time performance indicators do not meet the expected targets, a new execution plan is generated and the SQL query statement is executed according to the new execution plan;

[0029] Get the actual execution result of the SQL query statement and update the metadata information according to the actual execution result.

[0030] In a possible implementation, after executing the SQL query statement based on the reorganized abstract syntax tree, the database SQL query optimization method further includes:

[0031] Based on the query log and the latest metadata information, determine the execution frequency of SQL query statements and the data change frequency of the execution results of SQL query statements;

[0032] If the execution frequency of the SQL query statement is higher than the preset execution frequency and / or the data change frequency of the execution result of the SQL query statement is lower than the second preset change frequency, the execution result of the SQL query statement is stored in the query cache.

[0033] In a possible implementation, the validity period of the query cache is associated with the data change pattern of the metadata.

[0034] In a second aspect, an embodiment of the present invention provides a database SQL query optimization device, comprising:

[0035] A first acquisition module is used to acquire the SQL query statement to be processed and convert the SQL query statement into a corresponding abstract syntax tree;

[0036] The second acquisition module is used to acquire the latest metadata information based on the metadata query interface;

[0037] A generation module, used for generating an abstract syntax tree after metadata enhancement according to the latest metadata information and the abstract syntax tree;

[0038] A query optimization module is used to dynamically reorganize and optimize queries based on the abstract syntax tree enhanced by metadata to obtain a reorganized abstract syntax tree;

[0039] The execution module is used to execute SQL query statements based on the reorganized abstract syntax tree.

[0040] In a third aspect, an embodiment of the present invention provides a terminal, comprising a memory and a processor, the memory being used to store a computer program, the processor being used to call and run the computer program stored in the memory, and executing the database SQL query optimization method as described in the first aspect or any possible implementation of the first aspect.

[0041] In a fourth aspect, an embodiment of the present invention provides a computer-readable storage medium, which stores a computer program. When the computer program is executed by a processor, it implements the steps of the database SQL query optimization method described in the first aspect or any possible implementation method of the first aspect.

[0042] The embodiment of the present invention provides a database SQL query optimization method, a terminal and a storage medium. When processing an SQL query statement to be processed, the method obtains the latest metadata information of the database through a metadata query interface, generates a metadata-enhanced abstract syntax tree corresponding to the SQL query statement through the latest metadata information, performs dynamic query reorganization and optimization through the metadata-enhanced abstract syntax tree, intelligently and dynamically adjusts and optimizes the SQL query statement based on the latest metadata information, adapts to a constantly changing data environment, and further improves query performance and processing efficiency. BRIEF DESCRIPTION OF THE DRAWINGS

[0043] In order to more clearly illustrate the technical solutions in the embodiments of the present invention, 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 only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0044] Figure 1It is a flowchart of a database SQL query optimization method provided by an embodiment of the present invention;

[0045] Figure 2 It is a structural diagram of a database SQL query optimization device provided by an embodiment of the present invention;

[0046] Figure 3 is a schematic diagram of a terminal provided by an embodiment of the present invention. DETAILED DESCRIPTION

[0047] In the following description, specific details such as specific system structures, technologies, etc. are provided for the purpose of illustration rather than limitation, so as to provide a thorough understanding of the embodiments of the present invention. However, it should be clear to those skilled in the art that the present invention may be implemented in other embodiments without these specific details. In other cases, detailed descriptions of well-known systems, devices, circuits, and methods are omitted to prevent unnecessary details from obstructing the description of the present invention.

[0048] In order to make the purpose, technical solutions and advantages of the present invention more clear, specific embodiments will be described below in conjunction with the accompanying drawings.

[0049] See also Figure 1 , which shows a flowchart of the implementation of the database SQL query optimization method provided by an embodiment of the present invention. The execution subject of the above database SQL query optimization method can be a terminal.

[0050] The above database SQL query optimization method may include:

[0051] In S101, a SQL (Structured Query Language) query statement to be processed is obtained, and the SQL query statement is converted into a corresponding Abstract Syntax Tree (AST).

[0052] After the SQL query statement to be processed is obtained, it is preprocessed and parsed through the parser inside the database, and converted into an internal AST that the database can understand.

[0053] S101 to S105 may be executed by a query optimizer in the database.

[0054] In S102, the latest metadata information is obtained based on the metadata query interface.

[0055] Among them, the latest metadata information can also be understood as real-time metadata information, which can be used to enrich the parsed query representation, namely AST, to provide necessary data support for subsequent query reorganization and optimization.

[0056] The metadata analysis component of the database will provide a metadata query interface to the outside world, through which the latest metadata information of the database can be obtained.

[0057] In S103, an abstract syntax tree with metadata enhancement is generated according to the latest metadata information and the abstract syntax tree.

[0058] The metadata-enhanced abstract syntax tree is an abstract syntax tree that incorporates the latest metadata information. The latest metadata information can support the query optimizer to reorganize and optimize SQL query statements more intelligently.

[0059] In S104, based on the abstract syntax tree enhanced by the metadata, the query is dynamically reorganized and optimized to obtain a reorganized abstract syntax tree.

[0060] This application can dynamically reorganize (or rewrite) and optimize SQL queries based on the metadata-enhanced abstract syntax tree, using the latest metadata information (such as index usage and statistical information, etc.) integrated into the metadata-enhanced abstract syntax tree, so as to obtain a reorganized abstract syntax tree.

[0061] Compared with the abstract syntax tree before reorganization, the reorganized abstract syntax tree has higher execution efficiency and better query performance.

[0062] In S105 , the SQL query statement is executed based on the reorganized abstract syntax tree.

[0063] When processing an SQL query statement to be processed, the embodiment of the present application obtains the latest metadata information of the database through a metadata query interface, and generates a metadata-enhanced abstract syntax tree corresponding to the SQL query statement through the latest metadata information. The query is dynamically reorganized and optimized through the metadata-enhanced abstract syntax tree, and the SQL query statement can be intelligently and dynamically adjusted and optimized based on the latest metadata information, so as to adapt to the ever-changing data environment, thereby improving query performance and processing efficiency.

[0064] In some embodiments, the metadata query interface is an interface provided by the metadata analysis component for querying the latest metadata information;

[0065] The metadata analysis component monitors database changes in event subscription mode through metadata listeners, and records database change information in a differential manner when monitoring database change information. It also adopts delayed loading and lazy update strategies to load and update database change information when the database change information is actually applied to SQL query optimization.

[0066] The metadata analysis component is a component in the database that analyzes the metadata in the database. It provides a metadata query interface that allows the query optimizer and other components in the database to access the latest metadata information. The metadata query interface can respond to metadata query requests and return the latest, incrementally updated metadata information.

[0067] The metadata listener is embedded in the database management system. Through the metadata listener, using the event subscription mode, you can monitor the changes in the database, including changes in the database schema, which can include the creation, update or deletion of tables, views, indexes, stored procedures and functions, etc.

[0068] In actual applications, when a DDL (Data Definition Language) statement is executed, the metadata listener captures the change event and parses the change event content to determine the nature of the change. For example, if it is an ALTER TABLE operation, the metadata listener will identify which columns are added, deleted, or the data type is changed, etc.

[0069] After capturing the database change information, the metadata listener can record the database change information in the form of differences to complete the synchronization process. The synchronization process only records the database change information instead of replacing all existing metadata, so that only the changed part of the database can be synchronized, improving synchronization efficiency.

[0070] For metadata updates, a delayed loading and lazy update strategy can be adopted, and the database change information is not updated immediately to reduce the impact on database performance. The database change information can be loaded and updated only when the database change information is actually used as metadata for SQL query optimization. Exemplarily, when the query optimizer accesses the latest metadata information through the metadata query interface, the database change information can be loaded and updated so that the query optimizer can obtain the latest metadata information.

[0071] In some embodiments, the metadata analysis component dynamically determines whether to retain or eliminate metadata in the memory cache based on the access frequency and modification frequency of the metadata, and ensures the atomicity and consistency of metadata changes through transaction log analysis or database write-ahead logging (WAL);

[0072] Among them, the metadata analysis component retains metadata with an access frequency higher than a first preset access frequency and / or a modification frequency lower than the first preset modification frequency in the memory cache, and eliminates metadata with an access frequency lower than a second preset access frequency and / or a modification frequency higher than the second preset modification frequency; the first preset access frequency is greater than or equal to the second preset access frequency, and the first preset modification frequency is less than or equal to the second preset modification frequency.

[0073] In this embodiment, the efficient metadata cache mechanism is to retain metadata with high access frequency (important) and / or low modification frequency (stable) by comprehensively considering access frequency and modification frequency, and eliminate metadata with low access frequency (unimportant or infrequently used) and / or high modification frequency (unstable, frequently changing). The metadata cache mechanism helps to optimize the utilization efficiency of the cache and ensure the optimization of database performance. In addition, this cache mechanism can not only reduce the demand for storage, but also improve the access speed.

[0074] If some metadata is frequently accessed, for example, it is often used by the query optimizer to optimize queries, it indicates that this metadata is very important for database operations. Therefore, this metadata should be retained in the cache. If some metadata does not change frequently (i.e., the modification frequency is low), their retention value increases because they provide stable and long-term valid information and do not need to be updated frequently.

[0075] On the other hand, if some metadata is rarely accessed, it may not be important for the current database operation and can be considered to be eliminated from the cache to free up resources. If some metadata changes frequently, its value in the cache may be low because the stored information will become outdated quickly. In this case, frequently updated metadata may not be suitable for long-term retention in the cache.

[0076] Exemplarily, in this embodiment, metadata with an access frequency higher than a first preset access frequency may be referred to as metadata with a high access frequency, metadata with a modification frequency lower than the first preset modification frequency may be referred to as metadata with a low modification frequency, metadata with an access frequency lower than a second preset access frequency may be referred to as metadata with a low access frequency, and metadata with a modification frequency higher than the second preset modification frequency may be referred to as metadata with a high modification frequency.

[0077] The first preset access frequency, the second preset access frequency, the first preset modification frequency, and the second preset modification frequency may vary according to the actual workload, scale, and specific requirements of the database. In actual applications, these parameters are configurable to adapt to different environments and performance requirements.

[0078] Access frequency can be understood as the number of accesses within a certain period of time. Within a certain time window (for example, every day or every hour), if the number of times metadata is accessed exceeds a certain threshold, that is, the access frequency is higher than the first preset access frequency, it can be considered as metadata with a high access frequency. In contrast to high access frequency, low access frequency may mean that within the same time window, the number of accesses is lower than a certain threshold, that is, the access frequency is lower than the second preset access frequency. For example, if a certain metadata is accessed more than 100 times per hour, it can be considered as metadata with a high access frequency. If a certain metadata is accessed less than 10 times per day, it can be considered as metadata with a low access frequency, and so on.

[0079] Modification frequency can be understood as the number of modifications within a certain period of time. Within a certain time window (for example, weekly, monthly or quarterly), the number of times the metadata is modified exceeds a certain threshold, that is, the modification frequency is higher than the second preset modification frequency, which can be considered as metadata with a high modification frequency. In contrast to high modification frequency, low modification frequency may mean that within the same time window, the number of modifications is lower than a certain threshold, or within a longer time window, modifications rarely occur, that is, the modification frequency is lower than the first preset modification frequency. For example, if the metadata of the table structure is modified 3 times or more per week, it can be considered as a high modification frequency. If the metadata of the index is only changed once within six months, it can be considered as a low modification frequency, and so on.

[0080] To ensure data consistency, the metadata analysis component implements transaction log analysis or uses the database's write-ahead log (WAL) to ensure the atomicity and consistency of metadata changes and ensure that metadata updates do not interrupt the ongoing query optimization process.

[0081] The metadata analysis component is the foundation of the entire database management system, which can provide real-time and accurate metadata information and provide important data support for subsequent query optimization. By effectively tracking and synchronizing changes in the database schema, the embodiment of the present application can quickly respond to changes in the data structure and ensure that the query optimization strategy is always based on the latest database status, which can not only improve query efficiency, but also reduce the potential risk of errors caused by outdated metadata.

[0082] In some possible implementations, the latest metadata information may include the latest table information, index information, and statistical information, etc. Specifically, it may include the latest table structure information, table size information (information such as the number of rows in the table or the size of the physical space occupied by the table), table statistical information, table relationship information, index coverage information (information such as the existence of the index, the type of index, the attributes of the index, and the index selectivity), data distribution characteristics (whether the data is evenly distributed or concentrated, the partitioning of the data, whether there are frequently occurring values ​​in the data, and whether the data has a long-tail distribution), index update frequency, index efficiency, partition information (partition range, partition size, and partition key, etc.), table and column access frequency, table and column data change frequency, column value distribution, column value frequency, column value cardinality, data timestamp or date, column access count, column access mode, and query mode, etc., or one or more of the following information.

[0083] It should be noted that the above only provides some examples of metadata information, and the metadata information may also include other related metadata, which is not specifically limited here.

[0084] In some embodiments, the above S104 may include:

[0085] If a subquery or view exists, the target table on which the subquery or view depends is determined according to the latest metadata information, and the subquery or view is expanded when the access frequency of the target table is higher than a third preset access frequency, the index of the target table meets the preset efficient index condition, the data volume of the target table is less than the preset data volume, the data change frequency of the target table is lower than the first preset change frequency, or the result set size after executing the subquery or view is less than the preset size;

[0086] If there is an aggregation operation, then based on the statistical information in the latest metadata information, unnecessary data is filtered out in advance before the aggregation operation to reduce the amount of data input for the aggregation operation;

[0087] If there is a sorting operation, determine whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted according to the latest metadata information, and determine the sorting strategy according to whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted;

[0088] If the database supports column storage, adjust the storage method of the data in the database according to the latest metadata information.

[0089] In this embodiment, if there are subqueries or views in the metadata-enhanced AST, that is, a complex query containing subqueries or views, determining whether to "expand" (inline) the subquery or view is based on the latest metadata information, which can simplify the query and improve efficiency.

[0090] Based on the latest metadata information, the target table and / or target column that the subquery or view depends on is determined, and the access frequency of the target table and / or target column is determined. If the target table and / or target column is frequently accessed in the database and has a high access frequency (i.e., the access frequency is higher than the third preset access frequency), then expanding the subquery or view can reduce repeated data access and processing, thereby improving query efficiency. The third preset access frequency can be set according to actual needs and is not specifically limited here.

[0091] Determine whether the target table and / or target column has an index based on the latest metadata information. If the target table and / or target column has an index and it is an efficient index (i.e., it meets the preset efficient index condition), then expanding the subquery or view can improve the utilization of the index and accelerate data access. Among them, the preset efficient index condition may include that the index selectivity is higher than the preset selectivity value, and the index update frequency is lower than the preset update frequency. The preset selectivity value and the preset update frequency can be set according to actual needs and are not specifically limited here.

[0092] The data volume of the target table can be determined based on the latest metadata information, and the data volume of the target table can be represented by the size information of the target table. If the data volume of the target table is small (that is, the data volume of the target table is less than the preset data volume), expanding the subquery or view can directly reduce the amount of data to be processed and improve query efficiency. The preset data volume can be set according to actual needs and is not specifically limited here.

[0093] Based on the latest metadata information, the frequency of data changes in the target table can be determined. Tables with stable data are suitable for expansion operations because the expanded query does not need to be updated frequently, which is conducive to maintaining query performance. If the data in the table changes frequently, the expansion operation may not be the best choice because it may lead to frequent recalculations and updates. Therefore, if the frequency of data changes in the target table is lower than the first preset change frequency, that is, the frequency of data changes in the target table is low, the subquery or view can be expanded. Among them, the first preset change frequency can be set according to actual usage requirements and is not specifically limited here.

[0094] Based on the size information of the target table and the query conditions, the size of the result set after executing the subquery or view can be estimated, and the subquery or view with a smaller result set is more suitable for expansion. Therefore, when the size of the result set after executing the subquery or view is less than the preset size, that is, when the result set size is small, the subquery or view can be expanded. The preset size can be set according to actual usage requirements and is not specifically limited here.

[0095] In summary, the expansion operation can be performed on subqueries or views that can directly reduce the amount of data processing and improve index utilization without introducing new complexity or unnecessary calculations after expansion.

[0096] If there is an aggregation operation in the AST after metadata enhancement, the aggregation operation can be optimized according to the statistical information in the latest metadata information, and unnecessary data can be filtered out in advance before performing the aggregation operation, thereby reducing the amount of data input for the aggregation operation.

[0097] For example, the characteristics of the data can be determined based on the distribution of column values, the frequency of column values, and the cardinality of column values ​​(the number of different column values) in the statistical information, and whether to filter out the data can be determined based on the characteristics of the data. For example, assuming that a value in a column is very rare, that is, the frequency and cardinality of the value are relatively low, the value may be filtered out before aggregation; based on the statistical information, high-frequency values ​​or extreme values ​​are identified, and based on the purpose of aggregation, it is determined whether to include or filter out these values; based on the timestamp or date field in the statistical information, the timeliness of the data is determined, and based on the timeliness of the data, old data, that is, data that has exceeded the timeliness, is filtered out; and so on.

[0098] If there is a sorting operation in the metadata-enhanced AST, the sorting operation can be optimized based on the index, data volume, and data distribution information of the column to be sorted.

[0099] Exemplarily, the above-mentioned determining the sorting strategy according to whether there is an index in the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted may include:

[0100] If the sequence to be sorted has an index, and the index type is a preset type suitable for sorting operations (such as a B-tree index), the sorting operation can be performed directly based on the index of the sequence to be sorted without an additional sorting step; otherwise, the most suitable sorting algorithm is determined based on the data volume and / or data distribution information of the sequence to be sorted. The preset type suitable for sorting operations can be used as the preset type suitable for sorting operations based on the index type.

[0101] Because indexes usually store data in some order (such as ascending or descending), sorting operations can be performed directly on the index if the index contains all the required fields.

[0102] The data distribution information of the sequence to be sorted may include whether the data is evenly distributed, whether there are frequently occurring values, or whether there is a long-tail distribution, etc.

[0103] Exemplarily, when the amount of data in the sequence to be sorted is large, external sorting may be used, and when the amount of data in the sequence to be sorted is small, quick sorting may be used, and so on.

[0104] The above method can effectively utilize the latest metadata information to optimize aggregation and sorting operations, thereby improving query performance and response speed.

[0105] If the database supports columnar storage, you can adjust the data storage and access methods based on the columns involved in the query and the access mode of the columns.

[0106] Determine which columns are frequently accessed, which columns are frequently accessed together, and which columns are rarely queried or accessed based on the statistics in the latest metadata information (such as the number of accesses / frequency or access pattern of each column). For column-based storage databases, columns that are frequently accessed together can be stored physically close together, or optimizations can be made in the storage structure to speed up joint access to these columns. For columns that are frequently queried or accessed, their storage format can be optimized, such as optimizing the compression method, to improve reading efficiency. For example, for columns with low access frequency (cold data), you can consider using a higher compression ratio to save storage space; for columns with high access frequency (hot data), use a lower compression ratio or a more optimized storage format to speed up reading operations. You can also partition by time or a common column based on access patterns to optimize related queries.

[0107] Through the above steps, the storage and access methods of data in the column storage database can be effectively adjusted according to the metadata information. Especially for wide tables (tables with a large number of columns), such optimization can significantly improve the performance of analytical queries.

[0108] In some embodiments, the above S104 may further include:

[0109] If there is a WHERE clause, reorder the predicates in descending order of index selectivity in the latest metadata information;

[0110] If there are multiple table connections, adjust the order and strategy of the multiple table connections based on the table size information, index coverage information and / or distribution characteristics of the data in the table in the latest metadata information;

[0111] Based on the latest metadata information, determine whether to use index scanning during the query process, and for composite indexes, analyze the matching degree between the columns in the query conditions and the index columns in the composite index, and determine the most effective index utilization strategy in the composite index;

[0112] According to the partition information in the latest metadata information, the query operation is performed on the partitions related to the query condition.

[0113] If there is a WHERE clause in the metadata-enhanced AST, the query optimizer will reorder the predicates and give priority to conditions based on highly selective indexes to reduce the amount of data scanning.

[0114] If there are multi-table connections in the metadata-enhanced AST, that is, multi-table JOINs are involved, the order and strategy of the multi-table connections can be adjusted according to the size information, index coverage information and / or distribution characteristics of the data in the tables involved in the multi-table connection to improve query efficiency.

[0115] The size information of a table includes information such as the number of rows in the table or the size of the physical space occupied by the table. Adjusting the order and strategy of multi-table joins based on the size information of the table may include: selecting the smallest table as the driving table for multi-table join operations based on the size information of the tables involved. Adjusting the order and strategy of multi-table joins based on the size information of the tables can effectively reduce the number of searches and comparisons on large tables, especially in Nested Loops Join, where the effect is more obvious.

[0116] Index coverage information may include information such as the existence of an index (whether an index exists), the type of index (e.g., primary key index or non-unique index, etc.), and index selectivity (i.e., the distribution of different values ​​on the index key). Adjusting the multi-table join strategy based on index coverage information may include: using effective indexes to speed up multi-table join operations based on the existence of indexes, the type of indexes, and the index selectivity. For example, if there is a highly selective index on the JOIN condition column, an Indexed Nested Loops Join operation may be used.

[0117] The distribution characteristics of the data in the table may include information such as whether the data distribution is uniform or concentrated, and the partitioning of the data. Adjusting the multi-table connection strategy according to the distribution characteristics of the data in the table may include: selecting the appropriate JOIN type based on different data distribution characteristics to perform multi-table connection operations. Exemplarily, for a table with uneven data distribution, select HashJoin to complete the multi-table connection operation; for two or more tables with uniform data distribution and similar sizes, select MergeJoin to complete the multi-table connection operation. Similar size means that the absolute value of the size difference is less than the preset size difference, and the preset size difference is a small value that can be set according to actual usage requirements.

[0118] This embodiment can also optimize the index selection based on the latest metadata information. The above-mentioned determination of whether to use index scanning in the query process based on the latest metadata information may include: determining whether to use index scanning in the query process based on the index information in the latest metadata information (for example, index selectivity and index update frequency, etc.). Exemplarily, if the index of a column is efficient, the query will be reorganized to give priority to using the index. Among them, an index with high index selectivity and low update frequency is an efficient index, an index with an index selectivity higher than a preset selectivity value is determined to have high selectivity, and an index update frequency lower than a preset update frequency is determined to have low update frequency. That is, an index with an index selectivity higher than a preset selectivity value and an index update frequency lower than a preset update frequency is an efficient index.

[0119] For a composite index, the index utilization strategy with the highest degree of matching between the columns in the query condition and the index columns in the composite index is determined as the most effective index utilization strategy for the composite index. Among them, in the index utilization strategy, the more the number of index columns matching the columns in the query condition is, the more the order of the columns matching the columns in the query condition is in the front, and the higher the selectivity of the index columns matching the columns in the query condition is, the higher the degree of matching between the columns in the query condition and the index columns in the composite index is.

[0120] Specifically, you can first analyze the structure of the composite index to determine the index columns contained in the composite index and the order of the index columns; then, check whether there are corresponding index columns for each column in the query condition, and consider the order of the corresponding index columns in the composite index. In the composite index, the front columns are heavier than the back columns. If the query condition only uses the first column in the composite index, the efficiency of the index is usually the highest; then, analyze the selectivity of the corresponding index column in the query condition. If the corresponding index column in the query condition has high selectivity, it will be more efficient to use the index.

[0121] If all columns in the query conditions are included in the composite index and the index covers all the data required for the query, the index in this case is called a "covering index", which can greatly improve query efficiency.

[0122] This embodiment can also optimize the query of the partition table according to the partition information in the latest metadata information. If the data is stored in the partition table, the query reorganization can consider scanning only the relevant partitions instead of the entire table, thereby improving the query efficiency, which can be achieved by analyzing the conditions related to the partition key in the WHERE clause.

[0123] The partition information may include information such as partition range, partition size, and partition key. The partition information may be used to optimize the data scan range. Specifically, when the query condition is related to a partition key, only the partition containing the relevant data may be scanned instead of the entire table scan. For example, if a table is partitioned by date and the query condition specifies a specific date range, only the partition containing these dates may be scanned.

[0124] By accessing only the partitions containing query-related data, the amount of data that needs to be processed is reduced, thereby speeding up queries and improving overall query efficiency. Accessing fewer partitions means fewer disk I / O operations, which is especially important in large database systems because I / O operations are often performance bottlenecks. Partition positioning can reduce disk I / O operations. For some specific types of queries (such as range queries, aggregation queries, etc.), by locating specific partitions, the size of the data set that needs to be processed can be significantly reduced, optimizing specific types of queries.

[0125] In some embodiments, the above S105 may include:

[0126] Generate multiple execution plans based on the reorganized abstract syntax tree;

[0127] Select the target execution plan with the lowest execution cost from multiple execution plans, and execute the SQL query statement according to the target execution plan;

[0128] Get real-time performance indicators during the execution of SQL query statements;

[0129] If the real-time performance indicators do not meet the expected targets, a new execution plan is generated and the SQL query statement is executed according to the new execution plan;

[0130] Get the actual execution result of the SQL query statement and update the metadata information according to the actual execution result.

[0131] Based on the reorganized abstract syntax tree, the query optimizer can generate multiple potential execution plans, each with different data access paths and resource usage estimates. Using the latest metadata information, such as the number of rows, index selectivity, etc., the query optimizer can evaluate the expected cost of each execution plan, select the execution plan with the lowest cost as the target execution plan, and submit the target execution plan to the database execution engine for processing.

[0132] During the execution of the SQL query statement, relevant real-time performance indicators can be obtained, such as at least one of the indicators such as execution time, CPU usage, memory consumption and number of I / O operations. Thresholds for each real-time performance indicator can be set based on historical performance data, expected resource usage or experience values, etc. The real-time performance indicator is compared with the corresponding threshold. If the real-time performance indicator does not meet the corresponding threshold, it is determined that the real-time performance indicator does not meet the expected target and deviates from expectations. At this time, the query optimizer can generate a new execution plan, for example, it can change the JOIN type, select different indexes or change the order of operations, etc. According to the new execution plan, the current SQL query statement is executed. The new execution plan can also be used to execute similar queries in the future.

[0133] When there are at least two real-time performance indicators, and all the real-time performance indicators do not meet the corresponding thresholds, it is determined that the real-time performance indicators do not meet the expected target.

[0134] This embodiment can also update the statistical information in the metadata information according to the actual execution results of the SQL query statement, for example, it can include the number of updated rows, index efficiency or data distribution, etc., which can help the query optimizer to make more accurate execution plan predictions in the future.

[0135] In some embodiments, after S105, the database SQL query optimization method may further include:

[0136] Based on the query log and the latest metadata information, determine the execution frequency of SQL query statements and the data change frequency of the execution results of SQL query statements;

[0137] If the execution frequency of the SQL query statement is higher than the preset execution frequency and / or the data change frequency of the execution result of the SQL query statement is lower than the second preset change frequency, the execution result of the SQL query statement is stored in the query cache.

[0138] This embodiment can identify frequently executed queries and / or queries with relatively stable result sets by analyzing statistical information in query logs and the latest metadata information. These queries can significantly improve the speed of data retrieval. Therefore, the query results (i.e., execution results) of these queries can be saved in the query cache.

[0139] Among them, the execution frequency and / or execution mode of each query can be captured in real time by regularly or continuously monitoring the query log, and the data change frequency of the execution result can be continuously monitored through event monitoring. The cache strategy can be dynamically adjusted based on the real-time execution frequency and the data change frequency of the execution result. For example, when the execution frequency of a query increases significantly and is higher than the preset execution frequency, its execution result can be added to the cache; for queries with low data change frequency (lower than the second preset change frequency) but high execution frequency (higher than the preset execution frequency), the execution result can be added to the cache.

[0140] Among them, the value of the preset execution frequency and the second preset change frequency can be determined according to actual usage requirements and is not specifically limited here.

[0141] The query cache strategy of the embodiment of the present application can be executed based on the cache management component in the database system.

[0142] In some embodiments, the validity period of the query cache is associated with a data change pattern of the metadata.

[0143] The validity period of the query cache in this embodiment is associated with the data change mode of the metadata. The data change mode of the metadata can be determined by the update timestamp or change log of the metadata, and then the validity period of the query cache is determined according to the data change mode of the metadata. For example, assuming that the data change mode of the metadata is updated every night, the validity period of the query cache can be one day.

[0144] In this embodiment, when the basic data of the database changes, the relevant cache content is automatically refreshed to ensure the real-time and accuracy of the data.

[0145] This embodiment can dynamically manage the query cache by analyzing and applying the latest metadata information, intelligently determine which query results should be cached, and determine the validity period of the cache, rather than relying on manual intervention, which can further improve query efficiency, reduce unnecessary data processing, and significantly reduce the processing time of repeated queries. The adaptive cache management mechanism can ensure the timeliness and accuracy of cached data, while optimizing the use of system resources, which is an important supplement to improve query performance.

[0146] The embodiments of the present application analyze and apply metadata in real time, not only to optimize before query execution, but also to dynamically adjust the execution plan during query execution, and even intelligently manage the cache of query results, so as to comprehensively improve the efficiency and flexibility of database queries. The potential of metadata can be fully utilized and expanded, and a comprehensive, dynamic and adaptive solution for database query optimization is provided through the innovative integration of incremental metadata tracking and synchronization, real-time metadata-driven SQL query optimization, and metadata-based adaptive query cache management. The query strategy of the database has shifted from traditional static query optimization to a more dynamic and intelligent data-driven optimization strategy.

[0147] The method provided in the embodiment of the present application can be applied to gbase8s, and can also be applied to other databases used, and is suitable for big data environments and high concurrency scenarios, such as large database systems, real-time data analysis platforms, business intelligence and data warehouses. It can solve the query delay problem when processing complex and large-scale data sets, optimize the use efficiency of database resources, and reduce the operating cost of the system. By dynamically adjusting the query strategy and optimizing the execution plan, efficient processing of complex queries can be achieved. The method can achieve performance improvement, resource optimization, and strong adaptability. The execution efficiency of SQL queries can be significantly improved, especially in large and complex database environments, which can effectively reduce response time and increase data processing rate; through intelligent index management and query path optimization, unnecessary data reading, writing and calculation are reduced, thereby optimizing system resource utilization and reducing operating costs; query strategies are dynamically adjusted according to real-time database status and metadata so that they can adapt to different data modes and query requirements.

[0148] It should be understood that the order of execution of the steps in the above embodiment does not necessarily mean the order of execution. The execution order of each process should be determined by its function and internal logic, and should not constitute any limitation on the implementation process of the embodiment of the present invention.

[0149] The following is an embodiment of the device of the present invention. For details not described in detail, reference may be made to the corresponding method embodiments described above.

[0150] Figure 2 The following is a schematic diagram showing the structure of a database SQL query optimization device provided by an embodiment of the present invention. For ease of explanation, only the parts related to the embodiment of the present invention are shown, which are described in detail as follows:

[0151] like Figure 2 As shown, the database SQL query optimization device 20 may include: a first acquisition module 21, a second acquisition module 22, a generation module 23, a query optimization module 24 and an execution module 25.

[0152] A first acquisition module 21 is used to acquire a SQL query statement to be processed and convert the SQL query statement into a corresponding abstract syntax tree;

[0153] The second acquisition module 22 is used to acquire the latest metadata information based on the metadata query interface;

[0154] A generating module 23, used for generating an abstract syntax tree after metadata enhancement according to the latest metadata information and the abstract syntax tree;

[0155] A query optimization module 24, configured to dynamically reorganize and optimize queries based on the abstract syntax tree enhanced by the metadata, and obtain a reorganized abstract syntax tree;

[0156] The execution module 25 is used to execute the SQL query statement based on the reorganized abstract syntax tree.

[0157] In a possible implementation, the metadata query interface is an interface provided by the metadata analysis component for querying the latest metadata information;

[0158] The metadata analysis component monitors database changes in event subscription mode through metadata listeners, and records database change information in a differential manner when monitoring database change information. It also adopts delayed loading and lazy update strategies to load and update database change information when the database change information is actually applied to SQL query optimization.

[0159] In a possible implementation, the metadata analysis component dynamically determines whether to retain or eliminate metadata in the memory cache according to the access frequency and modification frequency of the metadata, and ensures the atomicity and consistency of metadata changes through transaction log analysis or a database write-ahead log;

[0160] Among them, the metadata analysis component retains metadata with an access frequency higher than a first preset access frequency and / or a modification frequency lower than the first preset modification frequency in the memory cache, and eliminates metadata with an access frequency lower than a second preset access frequency and / or a modification frequency higher than the second preset modification frequency; the first preset access frequency is greater than or equal to the second preset access frequency, and the first preset modification frequency is less than or equal to the second preset modification frequency.

[0161] In a possible implementation, the query optimization module 24 is specifically used to:

[0162] If a subquery or view exists, the target table on which the subquery or view depends is determined according to the latest metadata information, and the subquery or view is expanded when the access frequency of the target table is higher than a third preset access frequency, the index of the target table meets the preset efficient index condition, the data volume of the target table is less than the preset data volume, the data change frequency of the target table is lower than the first preset change frequency, or the result set size after executing the subquery or view is less than the preset size;

[0163] If there is an aggregation operation, then based on the statistical information in the latest metadata information, unnecessary data is filtered out in advance before the aggregation operation to reduce the amount of data input for the aggregation operation;

[0164] If there is a sorting operation, determine whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted according to the latest metadata information, and determine the sorting strategy according to whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted;

[0165] If the database supports column storage, adjust the storage method of the data in the database according to the latest metadata information.

[0166] In a possible implementation, the query optimization module 24 may also be used to:

[0167] If there is a WHERE clause, reorder the predicates in descending order of index selectivity in the latest metadata information;

[0168] If there are multiple table connections, adjust the order and strategy of the multiple table connections based on the table size information, index coverage information and / or distribution characteristics of the data in the table in the latest metadata information;

[0169] Based on the latest metadata information, determine whether to use index scanning during the query process, and for composite indexes, analyze the matching degree between the columns in the query conditions and the index columns in the composite index, and determine the most effective index utilization strategy in the composite index;

[0170] According to the partition information in the latest metadata information, the query operation is performed on the partitions related to the query condition.

[0171] In a possible implementation, the execution module 25 is specifically configured to:

[0172] Generate multiple execution plans based on the reorganized abstract syntax tree;

[0173] Select the target execution plan with the lowest execution cost from multiple execution plans, and execute the SQL query statement according to the target execution plan;

[0174] Get real-time performance indicators during the execution of SQL query statements;

[0175] If the real-time performance indicators do not meet the expected targets, a new execution plan is generated and the SQL query statement is executed according to the new execution plan;

[0176] Get the actual execution result of the SQL query statement and update the metadata information according to the actual execution result.

[0177] In a possible implementation, the database SQL query optimization device 20 may further include: a query cache optimization module.

[0178] The query cache optimization module is used to: after executing the SQL query statement based on the reorganized abstract syntax tree, determine the execution frequency of the SQL query statement and the data change frequency of the execution result of the SQL query statement based on the query log and the latest metadata information; if the execution frequency of the SQL query statement is higher than the preset execution frequency and / or the data change frequency of the execution result of the SQL query statement is lower than the second preset change frequency, the execution result of the SQL query statement is stored in the query cache.

[0179] In a possible implementation, the validity period of the query cache is associated with the data change pattern of the metadata.

[0180] Figure 3 is a schematic diagram of a terminal provided by an embodiment of the present invention. Figure 3 As shown, the terminal 3 of this embodiment includes: a processor 30 and a memory 31. The memory 31 is used to store a computer program 32, and the processor 30 is used to call and run the computer program 32 stored in the memory 31 to execute the steps in the above-mentioned various database SQL query optimization method embodiments, such as Figure 1 Alternatively, the processor 30 is used to call and run the computer program 32 stored in the memory 31 to implement the functions of each module / unit in the above-mentioned device embodiments, for example Figure 2 Functions of modules / units 21 to 25 are shown.

[0181] Exemplarily, the computer program 32 may be divided into one or more modules / units, which are stored in the memory 31 and executed by the processor 30 to implement the present invention. The one or more modules / units may be a series of computer program instruction segments capable of implementing specific functions, which are used to describe the execution process of the computer program 32 in the terminal 3. For example, the computer program 32 may be divided into Figure 2 Modules / units 21 to 25 are shown.

[0182] The terminal 3 may include, but is not limited to, a processor 30 and a memory 31. Those skilled in the art will appreciate that Figure 3 It is only an example of terminal 3 and does not constitute a limitation on terminal 3. It may include more or fewer components than shown in the figure, or a combination of certain components, or different components. For example, the terminal may also include input and output devices, network access devices, buses, etc.

[0183] The processor 30 may be a central processing unit (CPU), or other general-purpose processors, digital signal processors (DSP), application-specific integrated circuits (ASIC), field-programmable gate arrays (FPGA) or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc. A general-purpose processor may be a microprocessor or any conventional processor, etc.

[0184] The memory 31 may be an internal storage unit of the terminal 3, such as a hard disk or memory of the terminal 3. The memory 31 may also be an external storage device of the terminal 3, such as a plug-in hard disk, a smart media card (SMC), a secure digital (SD) card, a flash card, etc. equipped on the terminal 3. Further, the memory 31 may also include both an internal storage unit and an external storage device of the terminal 3. The memory 31 is used to store the computer program and other programs and data required by the terminal. The memory 31 may also be used to temporarily store data that has been output or is to be output.

[0185] The technicians in the relevant field can clearly understand that for the convenience and simplicity of description, only the division of the above-mentioned functional units and modules is used as an example for illustration. In practical applications, the above-mentioned function allocation can be completed by different functional units and modules as needed, that is, the internal structure of the device can be divided into different functional units or modules to complete all or part of the functions described above. The functional units and modules in the embodiment can be integrated in a processing unit, or each unit can exist physically separately, or two or more units can be integrated in one unit. The above-mentioned integrated unit can be implemented in the form of hardware or in the form of software functional units. In addition, the specific names of the functional units and modules are only for the convenience of distinguishing each other, and are not used to limit the scope of protection of this application. The specific working process of the units and modules in the above-mentioned system can refer to the corresponding process in the aforementioned method embodiment, which will not be repeated here.

[0186] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described or recorded in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.

[0187] Those skilled in the art will appreciate that the units and algorithm steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.

[0188] In the embodiments provided by the present invention, it should be understood that the disclosed devices / terminals and methods can be implemented in other ways. For example, the device / terminal embodiments described above are only schematic. For example, the division of the modules or units is only a logical function division. There may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. Another point is that the mutual coupling or direct coupling or communication connection shown or discussed can be through some interfaces, indirect coupling or communication connection of devices or units, which can be electrical, mechanical or other forms.

[0189] The units described as separate components may or may not be physically separated, and the components shown as units may or may not be physical units, that is, they may be located in one place or distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the solution of this embodiment.

[0190] In addition, each functional unit in each embodiment of the present invention may be integrated into one processing unit, or each unit may exist physically separately, or two or more units may be integrated into one unit. The above-mentioned integrated unit may be implemented in the form of hardware or in the form of software functional units.

[0191] If the integrated module / unit is implemented in the form of a software functional unit and sold or used as an independent product, it can be stored in a computer-readable storage medium. Based on this understanding, the present invention implements all or part of the processes in the above-mentioned embodiment method, and can also be completed by instructing the relevant hardware through a computer program. The computer program can be stored in a computer-readable storage medium. When the computer program is executed by the processor, the steps of the above-mentioned database SQL query optimization method embodiments can be implemented. Among them, the computer program includes computer program code, and the computer program code can be in source code form, object code form, executable file or some intermediate form. The computer-readable medium may include: any entity or device that can carry the computer program code, recording medium, U disk, mobile hard disk, disk, optical disk, computer memory, read-only memory (ROM), random access memory (RAM), electric carrier signal, telecommunication signal and software distribution medium.

[0192] The embodiments described above are only used to illustrate the technical solutions of the present invention, rather than to limit the same. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that the technical solutions described in the aforementioned embodiments may still be modified, or some of the technical features may be replaced by equivalents. Such modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present invention, and should all be included in the protection scope of the present invention.

Claims

1. A database SQL query optimization method, characterized in that: include: Obtaining a SQL query statement to be processed, and converting the SQL query statement into a corresponding abstract syntax tree; Based on the metadata query interface, obtain the latest metadata information; Generate an abstract syntax tree after metadata enhancement according to the latest metadata information and the abstract syntax tree; Based on the abstract syntax tree enhanced by the metadata, dynamically reorganize and optimize the query to obtain a reorganized abstract syntax tree; Execute the SQL query statement based on the reorganized abstract syntax tree; The step of dynamically reorganizing and optimizing the query based on the abstract syntax tree enhanced by the metadata to obtain the reorganized abstract syntax tree includes: If a subquery or view exists, determining a target table on which the subquery or view depends according to the latest metadata information, and expanding the subquery or view when an access frequency of the target table is higher than a third preset access frequency, an index of the target table meets a preset efficient index condition, a data volume of the target table is less than a preset data volume, a data change frequency of the target table is lower than a first preset change frequency, or a result set size after executing the subquery or the view is less than a preset size; If there is an aggregation operation, then according to the statistical information in the latest metadata information, unnecessary data is filtered out in advance before the aggregation operation to reduce the data input amount of the aggregation operation; If there is a sorting operation, determine whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted according to the latest metadata information, and determine the sorting strategy according to whether there is an index for the sequence to be sorted, the amount of data in the sequence to be sorted, and the data distribution information of the sequence to be sorted; If the database supports column storage, the storage method of the data in the database is adjusted according to the latest metadata information.

2. The database SQL query optimization method according to claim 1, characterized in that: The metadata query interface is an interface provided by the metadata analysis component for querying the latest metadata information; The metadata analysis component monitors database changes in event subscription mode through a metadata listener, and when monitoring database change information, records the database change information in a differential manner, and adopts delayed loading and lazy update strategies to load and update the database change information when the database change information is actually applied to SQL query optimization.

3. The database SQL query optimization method according to claim 2, characterized in that: The metadata analysis component dynamically determines whether to retain or eliminate metadata in the memory cache according to the access frequency and modification frequency of the metadata, and ensures the atomicity and consistency of metadata changes through transaction log analysis or database pre-write logs; Among them, the metadata analysis component retains metadata with an access frequency higher than a first preset access frequency and / or a modification frequency lower than the first preset modification frequency in the memory cache, and eliminates metadata with an access frequency lower than a second preset access frequency and / or a modification frequency higher than the second preset modification frequency; the first preset access frequency is greater than or equal to the second preset access frequency, and the first preset modification frequency is less than or equal to the second preset modification frequency.

4. The database SQL query optimization method according to claim 1, characterized in that: The query is dynamically reorganized and optimized based on the abstract syntax tree enhanced by the metadata to obtain a reorganized abstract syntax tree, and further includes: If there is a WHERE clause, reorder the predicates in descending order of index selectivity in the latest metadata information; If there are multiple table connections, the order and strategy of the multiple table connections are adjusted according to the table size information, index coverage information and / or distribution characteristics of the data in the table in the latest metadata information; Determine whether to use index scanning in the query process according to the latest metadata information, and for composite indexes, analyze the matching degree between the columns in the query conditions and the index columns in the composite indexes, and determine the most effective index utilization strategy in the composite indexes; According to the partition information in the latest metadata information, a query operation is performed on the partition related to the query condition.

5. The database SQL query optimization method according to claim 1, characterized in that: The executing the SQL query statement based on the reorganized abstract syntax tree includes: Based on the reorganized abstract syntax tree, generate multiple execution plans; Selecting a target execution plan with the lowest execution cost from the multiple execution plans, and executing the SQL query statement according to the target execution plan; In the process of executing the SQL query statement, obtaining real-time performance indicators; If the real-time performance indicator does not meet the expected target, a new execution plan is generated, and the SQL query statement is executed according to the new execution plan; The actual execution result of the SQL query statement is obtained, and the metadata information is updated according to the actual execution result.

6. The database SQL query optimization method according to any one of claims 1 to 5, characterized in that: After executing the SQL query statement based on the reorganized abstract syntax tree, the database SQL query optimization method further includes: Determine, based on the query log and the latest metadata information, the execution frequency of the SQL query statement and the data change frequency of the execution result of the SQL query statement; If the execution frequency of the SQL query statement is higher than the preset execution frequency and / or the data change frequency of the execution result of the SQL query statement is lower than the second preset change frequency, the execution result of the SQL query statement is stored in the query cache.

7. The database SQL query optimization method according to claim 6, characterized in that: The validity period of the query cache is associated with the data change mode of the metadata.

8. A terminal, characterized in that: The system comprises a memory and a processor, wherein the memory is used to store a computer program, and the processor is used to call and run the computer program stored in the memory to execute the database SQL query optimization method according to any one of claims 1 to 7.

9. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the steps of the database SQL query optimization method according to any one of claims 1 to 7 are implemented.