Data query method
By splitting complex query statements into multiple logically simple query statements and using composite indexes to optimize the query process, the problem of inefficiency of the database in processing complex query conditions is solved and more efficient query performance is achieved.
Patent Information
- Application Number
- CN202410618836.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-05-17
- Publication Date
- 2024-08-23
AI Technical Summary
When existing databases handle complex query conditions, the query efficiency is low and the amount of calculation is large, resulting in long query times.
Split complex query statements into multiple logically simpler query statements, and the results of logical or connections are equal to the original query conditions, optimizing the query process, using composite indexes to quickly locate the data range, and reducing queries back to the original data table. frequency.
It improves the query efficiency, reduces the amount of calculation and resource consumption, reduces the long-tail delay, and improves the query performance.
Smart Images

Figure CN118535587A
Abstract
Description
Technical Field
[0001] The present application relates to the field of database technology, and in particular to a data query method. Background Art
[0002] A database is a system for storing and managing data. It can store various types of data. The database will perform query operations based on the user's query statements, retrieve data from the database, and filter based on the query conditions to find data that meets the query conditions.
[0003] In current databases, when the query conditions entered by users are numerous and complex, for example, the query conditions include relational expressions of multiple variables, and each variable corresponds to one or more relational expressions, the query efficiency is low and the query takes a long time. Summary of the invention
[0004] The present application provides a data query method, which can improve the query efficiency and reduce the amount of calculation and the time consumed by the query.
[0005] In a first aspect, an embodiment of the present application provides a data query method, including:
[0006] Acquire a first query statement input by a user, where the first query statement includes a first query condition;
[0007] Splitting the first query statement into multiple second query statements, wherein the second query condition included in at least one of the second query statements includes multiple relational expressions for different variables, and the relational expressions for the multiple different variables are in a logical AND relationship; the result of the logical OR connection of the second query conditions included in the multiple second query statements is equal to the first query condition;
[0008] Determine a data query result of each second query statement in the plurality of second query statements;
[0009] The data query result of the first query statement is obtained according to the data query result of each second query statement in the multiple second query statements.
[0010] It can be seen that in the embodiment of the present application, by splitting the first query statement into multiple second query statements, it is achieved that the first query condition in the first query statement is split into multiple second query conditions in the second query statements, wherein the result of the multiple second query conditions being connected by logical or is equivalent to the first query condition. Among them, the first query condition is a longer and more logically complex condition, and the second query condition is a relatively short and more logically simple condition. Splitting the complex first query statement into multiple logically simpler second query statements, and then aggregating the data query results of the multiple logically simpler second query statements, compared to directly querying the logically more complex first query statement, consumes less time and requires less calculation, thereby improving the efficiency of the query.
[0011] Based on the first aspect, in a possible implementation, when the second query condition included in the second query statement includes a relational expression of a composite index variable, determining a data query result of the second query statement includes:
[0012] Determine a composite index data set corresponding to the composite index variable;
[0013] Determine from the composite index data set a first data range that satisfies a relational expression corresponding to a left prefix variable in the composite index variable, wherein the relational expression of the composite index variable includes a relational expression corresponding to the left prefix variable and a relational expression corresponding to a non-left prefix variable;
[0014] A second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable is determined from the first data range to obtain a data query result of the second query statement.
[0015] It can be seen that in the case where the second query condition includes a relational expression of a composite index variable, the composite index data set corresponding to the composite index variable is first determined, and then the first data range of the relational expression corresponding to the left prefix variable of the composite index is determined from the composite index data set, so that the data that may meet the query condition can be quickly located. Secondly, from the first data range, the second data range of the relational expression corresponding to the non-left prefix variable of the composite index is determined, so as to obtain the result of the second query statement. The traditional query method is that after determining the composite index data set, based on the left prefix matching principle in the composite index, the data range of the relational expression corresponding to the left prefix variable is first determined from the composite index data set, and then according to the primary key data in this range, the data range of the relational expression corresponding to the non-left prefix variable is returned to the original data table step by step to obtain the data query result of the query statement. Since there are more data and a larger amount of data in the original data table, if the non-left prefix variable includes multiple, the traditional method needs to return to the original data table for multiple queries, and the time consumed by returning to the original data table for multiple queries is long (there is a long tail delay), the amount of calculation is large, and the query efficiency is low. Compared with traditional query methods, the query method of the present application can directly narrow the scope of the query step by step based on the composite index data set without returning to the original data table for query, thereby improving the query efficiency and reducing long-tail latency.
[0016] Based on the first aspect, in a possible implementation, the first query statement includes a statement for establishing a composite index;
[0017] Before determining the composite index data set corresponding to the composite index variable, the method further includes:
[0018] According to the composite index creation statement, data for each variable in the composite index variable and the data primary key are intercepted from the original data set to obtain the composite index data set.
[0019] Based on the first aspect, in a possible implementation, when the second query condition included in the second query statement also includes a relational expression of a non-composite index variable, after determining a second data range of the relational expression corresponding to the non-left prefix variable that satisfies the composite index, determining the data query result of the second query statement further includes:
[0020] According to the data primary key in the second data range, a third data range satisfying the relational expression of the non-composite index variable is determined from the original data set to obtain the data query result of the second query statement.
[0021] Based on the first aspect, in a possible implementation, when the second query condition included in the second query statement includes multiple relational expressions for the same variable, before determining the data query result of the second query statement, the method further includes:
[0022] In the second query condition, multiple relational expressions for the same variable are merged.
[0023] It can be seen that in the embodiment of the present application, by simplifying and merging multiple relational expressions corresponding to the same variable, the complexity of the query conditions can be reduced, and the accuracy and efficiency of the query can be improved. This method can optimize the query logic, simplify the query conditions, and reduce the query time and resource consumption.
[0024] Based on the first aspect, in a possible implementation, if the first data range is an empty set, the result of the second query statement is an empty set;
[0025] If the second data range is an empty set, the result of the second query statement is an empty set; or,
[0026] If the third data range is an empty set, the result of the second query statement is an empty set.
[0027] It can be seen that in the embodiment of the present application, if the first data range is an empty set, it means that there is no relational expression corresponding to the left prefix variable that satisfies the composite index. In this case, no matter what the subsequent second data range and the third data range are, the query result will be an empty set. If the second data range is an empty set, it means that there is no relational expression corresponding to the variable in the non-composite index. In this case, no matter what the subsequent third data range is, the query result will be an empty set. If the third data range is an empty set, it means that there is no data that satisfies the relational expression in the second data range. In this case, the query result will also be an empty set. By judging whether the first data range, the second data range, and the third data range are empty sets, it can be determined in advance whether the query result is an empty set to avoid unnecessary query operations. This method can save time and resources and improve the efficiency of queries.
[0028] Based on the first aspect, in a possible implementation, when the non-left prefix variable in the composite index variable includes a first variable and a second variable, and the first variable is in front and the second variable is in the back, determining, from the first data range, a second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable includes:
[0029] Determine, from the first data range, a fourth data range that satisfies the relational expression corresponding to the first variable;
[0030] From the fourth data range, determine the data range that satisfies the relational expression corresponding to the second variable, that is, obtain the second data range.
[0031] In a second aspect, an embodiment of the present application provides a secondary index query device for a database, comprising:
[0032] An acquisition module, configured to acquire a first query statement input by a user, wherein the first query statement includes a first query condition;
[0033] A splitting module, used for splitting the first query statement into multiple second query statements, wherein the second query condition included in at least one of the second query statements includes multiple relational expressions for different variables, and the relational expressions for the multiple different variables are in a logical AND relationship; the logical OR of the second query conditions included in the multiple second query statements is equal to the first query condition;
[0034] A determination module, configured to determine a data query result of each second query statement in the plurality of second query statements;
[0035] The determination module is further configured to obtain the data query result of the first query statement according to the data query result of each second query statement in the plurality of second query statements.
[0036] Based on the second aspect, in a possible implementation, when the second query condition included in the second query statement includes a relational expression of a composite index variable, the determining module is used to:
[0037] Determine a composite index data set corresponding to the composite index variable;
[0038] Determine from the composite index data set a first data range that satisfies a relational expression corresponding to a left prefix variable in the composite index variable, wherein the relational expression of the composite index variable includes a relational expression corresponding to the left prefix variable and a relational expression corresponding to a non-left prefix variable;
[0039] A second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable is determined from the first data range to obtain a data query result of the second query statement.
[0040] Based on the second aspect, in a possible implementation, the first query statement includes a statement for establishing a composite index;
[0041] The determination module is also used to: according to the statement for establishing a composite index, intercept data for each variable and a data primary key in the composite index variable from the original data set to obtain the composite index data set.
[0042] Based on the second aspect, in a possible implementation manner, when the second query condition included in the second query statement also includes a relational expression of a non-composite index variable, the determining module is used to:
[0043] According to the data primary key in the second data range, a third data range satisfying the relational expression of the non-composite index variable is determined from the original data set to obtain the data query result of the second query statement.
[0044] Based on the second aspect, in a possible implementation, when the second query condition included in the second query statement includes multiple relational expressions for the same variable, the determining module is used to:
[0045] In the second query condition, multiple relational expressions for the same variable are merged.
[0046] Based on the second aspect, in a possible implementation manner, the determining module is used to:
[0047] If the first data range is an empty set, determining that the result of the second query statement is an empty set;
[0048] If the second data range is an empty set, determining that the result of the second query statement is an empty set; or,
[0049] If the third data range is an empty set, it is determined that the result of the second query statement is an empty set.
[0050] Based on the second aspect, in a possible implementation, when the non-left prefix variable in the composite index variable includes a first variable and a second variable, and the first variable is in front and the second variable is in the back, the determination module is used to:
[0051] Determine, from the first data range, a fourth data range that satisfies the relational expression corresponding to the first variable;
[0052] From the fourth data range, determine the data range that satisfies the relational expression corresponding to the second variable, that is, obtain the second data range.
[0053] Each functional module in the second aspect is used to implement the method described in the first aspect and the possible implementation manner of the first aspect.
[0054] In a third aspect, an embodiment of the present application provides a computing device, comprising a memory and a processor, wherein the memory is used to store instructions, and the processor is used to execute the instructions stored in the memory to implement the method described in the above-mentioned first aspect and any possible implementation method of the first aspect.
[0055] In a fourth aspect, an embodiment of the present application provides a computer storage medium, comprising program instructions, which, when executed by a computing device, causes the computing device to execute the method described in the first aspect and any possible implementation manner of the first aspect.
[0056] In a fifth aspect, the present application provides a computer program product, including program instructions, and when the computer program product is executed by a computing device, the computing device is used to execute the method described in the first aspect and any possible implementation of the first aspect. The computer program product can be a software installation package, and when it is necessary to use the method provided by any possible design of the first aspect, the computer program product can be downloaded and executed on a computing device to implement the method described in the first aspect and any possible implementation of the first aspect. BRIEF DESCRIPTION OF THE DRAWINGS
[0057] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the background technology, the drawings required for use in the embodiments of the present application or the background technology will be described below.
[0058] Figure 1 It is a flowchart of a data query method provided by this application;
[0059] Figure 2 It is a flowchart of determining the data query result of the second query statement provided by the present application;
[0060] Figure 3 It is a schematic diagram of a query condition provided by this application abstracted into a logic tree;
[0061] Figure 4 This is a schematic diagram of a secondary index query of a database provided by this application;
[0062] Figure 5 It is a structural schematic diagram of a data query device provided by the present application;
[0063] Figure 6 It is a structural schematic diagram of a computing device provided by this application. DETAILED DESCRIPTION
[0064] The embodiments of the present application are described below in conjunction with the drawings in the embodiments of the present application. Obviously, the described embodiments are only part of the embodiments of the present application, rather than all of the embodiments. Based on the embodiments in the present application, all other embodiments obtained by ordinary technicians in this field without making creative work are within the scope of protection of this application.
[0065] It should be noted that the terms used in the embodiments of the present application are only for the purpose of describing specific embodiments, and are not intended to limit the present application. The singular forms of "a", "said" and "the" used in the embodiments of the present application and the appended claims are also intended to include plural forms, unless the context clearly indicates other meanings. It should also be understood that the term "and / or" used herein refers to and includes any or all possible combinations of one or more associated listed items.
[0066] It should be noted that when used in this specification and the appended claims, the terms "comprises" and any of their variations are intended to cover non-exclusive inclusions. For example, a system, product or device including a series of units / devices is not limited to the listed units / devices, but may optionally include units / devices not listed, or may also optionally include other units / devices inherent to these products or devices.
[0067] It will also be understood that the term “if” may be interpreted as “when” or “upon” or “in response to determining” or “in response to detecting” or “in the event of…” depending on the context.
[0068] Before introducing the embodiments of the present application, the technical terms involved in the embodiments of the present application are first introduced.
[0069] A database is a structured collection of data in which data is stored in an organized manner. A database usually consists of one or more tables, each of which contains several rows and columns. Each row represents a record, and each column represents an attribute or a field. Common databases include SQL databases, MySQL databases, Oracle databases, etc.
[0070] A secondary index, also called a secondary index, is a type of index in a database. Each table in a database has a column representing the primary key field. The primary key is used to uniquely identify each row of data. In addition to the column representing the primary key field, each table also includes columns representing other attributes or fields. Other attributes or fields are not used as unique identifiers for each row of data.
[0071] The primary key index is an index based on the primary key field. The secondary index is different from the primary key index. The secondary index is an index built on non-primary key columns (columns of other attributes or fields). Secondary indexes are used to speed up specific query operations and improve query performance by providing fast access to specific columns in the table. Specifically, a secondary index is an index for a column or combination of columns in a table to optimize retrieval, sorting, and other operations.
[0072] Composite index: A data structure in a database that creates indexes on multiple columns at the same time to provide fast search and data access for combinations of these columns. This means that when a query involves all or part of the columns of a composite index, the database engine can use this index to locate and retrieve data more efficiently, reducing the time and resources required for the search.
[0073] Long-tail latency: refers to the phenomenon that in a data distribution, there are very few data points or events whose response time far exceeds that of most data points, causing the latency in some extreme cases to deviate significantly from the average.
[0074] Left prefix: In the database, it usually refers to the leftmost part of the index, which is a continuous substring starting from the left side of the index column. When the query condition involves the leftmost column of the index, the database can effectively use the index through left prefix matching. Through left prefix matching, the database can quickly locate the index range that meets the conditions without scanning the entire index or table, thereby improving query efficiency.
[0075] The following is an introduction to the data query method provided by this application. The data query method provided by this application is based on non-primary keys, that is, a secondary index query method of the database. Figure 1 , Figure 1 A flowchart of a data query method provided in the present application, the method includes but is not limited to the description of the following content.
[0076] S101: Obtain a first query statement input by a user, where the first query statement includes a first query condition.
[0077] The first query statement refers to the statement used when performing a query operation in the database, which is usually written in structured query language (SQL). The first query statement may include multiple keywords and parameters to specify the query conditions, sorting methods, grouping methods, etc. Keywords may include: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY. SELECT is used to specify the columns to be queried, and the wildcard "*" can be used to represent all columns; FROM is used to specify the table to be queried, and the database will retrieve data from the table; WHERE is used to specify the query conditions, which may include multiple query conditions and operators, and only rows that meet the query conditions will be included in the query results; GROUP BY is used to group the result set by the specified column. It is usually used with aggregate functions such as SUM, COUNT, AVG, etc.; HAVING is used to specify the grouping conditions for the query, and is usually used with GROUP BY; ORDER BY is used to specify the sorting method for the query, and is sorted according to one or more columns. The first query statement may also include more keywords, and this application does not specifically limit the keywords of the first query statement.
[0078] The first query statement includes a first query condition, which refers to a condition used to filter data in the first query statement and may include multiple variables and operators. The first query condition is usually a combination of multiple conditions, each of which is used to limit the range of one of the variables.
[0079] The user can input the first query statement through a text box, a drop-down menu, a check box, etc. The first query statement is passed to the back-end application, and the back-end application parses the first query statement, converts it into a structured query language statement and executes the query operation, and finally returns the query result to the user.
[0080] S102. Split the first query statement into multiple second query statements, wherein the second query condition included in at least one of the second query statements includes multiple relational expressions for different variables, the relational expressions for the multiple different variables are in a logical AND relationship, and the logical OR of the second query conditions included in the multiple second query statements is equivalent to the first query condition.
[0081] The first query statement input by the user is split into multiple second query statements, wherein the multiple second query statements include a second query condition, wherein the second query condition involves relational expressions of multiple different variables, and the relational expressions of the multiple different variables have a logical AND relationship. The relational expression of each variable in the second query condition may include one or more, and when the relational expression of the same variable in the second query condition includes multiple, the multiple relational expressions have a logical AND or logical OR relationship.
[0082] There are also relational expressions involving only one variable in the second query statements in the multiple second query statements. There may be multiple or one relational expressions for one variable in the second query statement.
[0083] For example, the first query statement is: WHERE((A>A0 OR A>A1)AND(B>B0 AND B>B1)), and the first query condition included in the first query statement is (A>A0 OR A>A1)AND(B>B0 AND B>B1). The first query statement is split into multiple second query statements. For example, the first query statement can be split into WHERE(A>A0AND B>B0)OR WHERE(A>A0 AND B>B1)OR WHERE(A>A1 AND B>B0)OR WHERE(A>A1 AND B>B1), where WHERE(A>A0AND B>B0), WHERE(A>A0 AND B>B1), WHERE(A>A1 AND B>B0), and WHERE(A>A1 AND B>B1) are all second query statements. B>B1) are all second query statements. It can be seen that the first query statement is split into four second query statements, each of which includes a second query condition. Each of the second query conditions involves a relational expression of the A variable and a relational expression of the B variable. The relational expression of the A variable and the relational expression of the B variable in the second query condition are connected by a logical AND. Multiple second query statements are connected by a logical OR. The results of the logical OR connection of the second query conditions included in the multiple second query statements are equivalent to the first query condition in the first query statement.
[0084] The result of logically connecting the multiple query conditions included in the split multiple second query statements is equal to the first query condition in the first query statement.
[0085] Each second query statement in the multiple second query statements is a relatively simple and independent relational expression and can be optimized and indexed separately, thereby improving query efficiency and performance.
[0086] S103: Determine a data query result of each second query statement in the plurality of second query statements.
[0087] Each of the multiple second query statements is an independent relational expression. By determining the data query result of each second query statement according to a preset rule, the first query statement can be simplified, thereby optimizing the query performance. When the second query statement includes a second query condition and multiple different variables in the second query condition include a composite index variable, the following is executed: Figure 2 The method steps shown, Figure 2 A schematic diagram of a flow chart for determining a data query result of a second query statement provided in the present application.
[0088] S1031. Determine a composite index data set corresponding to the composite index variable.
[0089] In an optional embodiment, the first query statement may include a statement for establishing a composite index. Based on the statement for establishing a composite index, the database intercepts data for each variable in the composite index variable and the data primary key from the original data table in the database, thereby obtaining a composite index data set. The following example illustrates that Table 1 is an original data table in the database. The original data table includes a primary key column ID column, non-primary key columns A, B, C and D. The primary key column ID is used to uniquely identify the data of each row. The non-primary key columns A, B and C can be data representing height, weight, class, gender, etc. A composite index idx_a_b is established. The composite index represents a composite index of (column A, column B), and a composite index data set is obtained, as shown in Table 2. In another optional embodiment, the composite index data set can also be pre-created. According to the composite index variable included in the first query statement, the composite index data set corresponding to the composite index variable can be found.
[0090] Table 1
[0091]
[0092] Table 2
[0093]
[0094] S1032: Determine, from the composite index data set, a first data range of a relational expression corresponding to a left prefix variable of the composite index that satisfies the second query condition.
[0095] Optionally, if there are multiple relational expressions corresponding to the same variable in the second query condition, simplifying and merging the multiple relational expressions corresponding to the same variable can reduce redundancy and improve query efficiency. For example, taking the above Table 1 as an example, if the relational expression involving variable A in the second query condition is: A>=170AND A<175AND A<=178, the relational expression corresponding to variable A includes three simple relational expressions, and these three relational expressions are logically redundant. If the query is performed directly according to the original second query condition, the query efficiency is low. Simplify and merge these three relational expressions, and change the relational expression involving variable A to: A>=170AND A<175. For another example, taking the above Table 1 as an example, if the second query condition is: (A>170OR A>174)AND(B>=70AND B>=72), the second query condition includes two variables A and B. The relational expression corresponding to variable A includes two simple relational expressions. These two simple relational expressions are logically redundant. The two variables are merged to become: A>170; the relational expression corresponding to variable B also includes two simple relational expressions. These two simple relational expressions are logically redundant. The two variables are merged to become: B=70. According to operation (1), the second query condition is changed to: WHERE A>170AND B>=72.
[0096] The left prefix variable of a composite index refers to the variable that comes first in the query order in the composite index, which is generally the first variable of the composite index. For example, a composite index containing (A, B) is created. The composite index contains two variables, A and B. Variable A is the first variable in the composite index, so variable A is the left prefix variable of the composite index.
[0097] A first data range is determined from the composite index data set according to a relational expression corresponding to a left prefix variable of the composite index.
[0098] For example, taking the above Table 1 as an example, if after splitting, one of the second query statements is WHERE (A<178AND B>=65AND C<=2), and the composite index established in the first query statement is a composite index idx_a_b including variables A and variables B, then according to 1), the composite index data set corresponding to the composite index variable is determined as shown in Table 2; the relational expression corresponding to the left prefix variable of the composite index in the second query condition is A<178, then the first data range satisfying A<178 is determined from the composite index data set Table 2 as shown in Table 3.
[0099] Table 3
[0100] A (e.g. height) B (such as weight) ID (primary key) 172 73 2 173 68 3 168 62 4 160 65 5
[0101] S1033. Determine, from the first data range, a second data range of the relational expression corresponding to the non-left prefix variable that satisfies the composite index, and obtain a data query result of the second query statement.
[0102] The non-left prefix variables of a composite index refer to other columns that are not included in the left prefix from left to right in the composite index. These columns are not in the left prefix position, but in other positions in the composite index. For example, if a composite index containing two variables A and B is created, then variable A is the left prefix variable of the composite index. Variable B does not belong to the left prefix of the composite index, but it still belongs to the variables in the composite index, so variable B is the non-left prefix variable of the composite index. For another example, if the composite index created is a composite index containing variables A, B, and C, then variable A is the left prefix variable of the composite index, and variables B and C are non-left prefix variables of the composite index.
[0103] In one example, the second query statement only includes the relational expression corresponding to the composite index variable. After obtaining the first data range, the second data range that satisfies the relational expression corresponding to the non-left prefix variable of the composite index is determined from the first data range, and the data query result of the second query statement can be obtained. In a specific example, the non-left prefix variable includes 1, for example, the composite index is a composite index of variables A and B. If the second query statement is WHERE (A<178AND B>=65), the second query statement only includes the relational expression corresponding to the composite index variables A and B. After obtaining the first data range shown in Table 3, the second data range that satisfies the relational expression B>=65 corresponding to the non-left prefix variable is determined from Table 3, as shown in Table 4, and the data query result of the second query statement is obtained.
[0104] Table 4
[0105] A (e.g. height) B (such as weight) ID (primary key) 172 73 2 173 68 3 160 65 5
[0106] In another specific example, the non-left prefix variables include multiple ones, and the first variable and the second variable are used as examples here for explanation, and when the first variable comes first and the second variable comes later, a second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable is determined from the first data range, including: firstly, determining a fourth data range that satisfies the relational expression corresponding to the first variable from the first data range; and then determining a data range that satisfies the relational expression corresponding to the second variable from the fourth data range, that is, obtaining the second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable.
[0107] For example, the composite index is the composite index idx_a_b_c of variables A, variable B and variable C. The composite index data set is determined from the original data table Table 1, as shown in Table 5.
[0108] Table 5
[0109]
[0110] If the second query statement is WHERE(A<178AND B>=65AND C<=2), the second query statement includes the relational expressions corresponding to the composite index variables A, B and C. First, determine the first data range that satisfies the relational expression A<178 corresponding to the left prefix variable, and obtain the data shown in Table 6.
[0111] Table 6
[0112] A (e.g. height) B (such as weight) C (such as class) ID (primary key) 172 73 2 2 173 68 2 3 168 62 3 4 160 65 3 5
[0113] Based on the first data range shown in Table 6, a fourth data range that satisfies the relational expression B>=65 corresponding to the non-left prefix variable B is determined from Table 6, as shown in Table 7.
[0114] Table 7
[0115] A (e.g. height) B (such as weight) C (such as class) ID (primary key) 172 73 2 2 173 68 2 3 160 65 3 5
[0116] Secondly, a second data range satisfying the relational expression C<=2 corresponding to the non-left prefix variable C is determined from Table 7, and the data query result of the second query statement is obtained, as shown in Table 8.
[0117] Table 8
[0118] A (e.g. height) B (such as weight) C (such as class) ID (primary key) 172 73 2 2 173 68 2 3
[0119] Non-left prefix variables may also include more than two variables, which is not limited in this application.
[0120] This solution is to determine the second data range of the relational expression corresponding to the non-left prefix variable that satisfies the composite index based on the first data range, rather than returning to the original data table to query the second data range of the relational expression corresponding to the non-left prefix variable that satisfies the composite index based on the primary key in the first data range. Since the amount of data in the original data table is large, the amount of calculation required to query back to the original data table is large, and the query efficiency is low. Implementing this step can reduce the amount of calculation and improve query efficiency.
[0121] Optionally, the second query condition may also include non-composite index variables. Non-composite index variables refer to variables that are not included in the composite index. For example, the composite index is a composite index including variables A and B, the second query statement is WHERE (A<178AND B>=65AND C<=2), the second query condition is A<178AND B>=65AND C<=2, and the second query condition includes non-composite index variable C. For non-composite index variables, it is impossible to obtain the data range of the relational expression corresponding to the non-composite index variable based on the first data range or the second data range. It is necessary to return to the original data table based on the primary key in the second data range to query the third data range that satisfies the relational expression corresponding to the variable in the non-composite index.
[0122] When the second query condition includes a left prefix variable of the composite index, a non-left prefix variable of the composite index, and a non-composite index variable, the result of the second query statement is the third data range.
[0123] Optionally, the case where the result in the relational expression is an empty set is handled: since the relational expressions between different variables in the second query statement have a logical AND relationship, when the result of the relational expression corresponding to a variable is an empty set, the results of the second query statement are all empty sets.
[0124] For example, the second query condition is: WHERE((E>=0AND E<0)AND(F>='0'AND F<='2')), the relational expression corresponding to the E variable is an empty set, and the relational expression corresponding to the F variable is not an empty set, but the expression between the E variable and the F variable is a logical AND relationship, so the result of the second query statement is an empty set.
[0125] Therefore, if the first data range in the second query statement is an empty set, the result of the second query statement is an empty set; if the second data range in the second query statement is an empty set, the result of the second query statement is an empty set; if the third data range in the second query statement is an empty set, the result of the second query statement is an empty set.
[0126] To facilitate understanding, an example is used for description.
[0127] The second query condition is: WHERE((E>=E0 END E <E2 END E<=E1)END(F> F0 END F<=F1) END G<=G0), the composite index includes two fields, E and F. The relational expression in the second query condition is abstracted into a logical tree. Figure 3 , Figure 3 A query condition provided for this application is abstracted into a schematic diagram of a logic tree.
[0128] The second query condition contains three variables: E, F, and G. The relationship expression between the three different variables has a logical AND relationship. The E variable is a left prefix variable of the composite index, the F variable is a non-left prefix variable of the composite index, and the G variable is a non-composite index variable.
[0129] If E2<=E1, according to operation (1) Figure 2 The relational expression of the first region in is simplified to: E>=E0END E <E2。
[0130] In addition, if E2<=E0, the relational expression corresponding to the E variable is an empty set. According to operation (4), the entire logic tree can be replaced with an empty set and returned.
[0131] The E variable belongs to the left prefix variable of the composite index. Since the left prefix is part of the index range in the index plan, according to operation (2), the part related to the E variable can be pruned out in the logic tree, that is, the first area, which is the first data range in this application.
[0132] The variable F belongs to the non-left prefix variable of the composite index. Figure 3 The second region in is transplanted into a new logic tree, and according to operation (3), the data range of the relational expression corresponding to the F variable is used as the second data range.
[0133] Variable G is a non-composite index variable. The data range of the relational expression corresponding to the non-composite index variable cannot be obtained based on the first data range or the second data range. It is necessary to return to the table to query the third data range that satisfies the relational expression corresponding to the variable in the non-composite index based on the primary key in the second data range.
[0134] After the above operations, the second query condition can be used for data query. The following example introduces the data query process. Figure 4 , Figure 4 A schematic diagram of a secondary index query of a database provided in this application.
[0135] Figure 4 The table 9 and the composite index table idx_E_F are included. Table 9 includes four fields, namely ID, E, F, and G. ID is the primary key field of Table 9. The primary key is a special field in the database that is used to uniquely identify each row of data in the table. The function of the primary key is to ensure that each record in the table has a unique identifier, which helps to quickly and accurately locate and access specific data rows. The composite index table includes variables E, variable F, and primary keys.
[0136] The second query condition is: WHERE (E>0END F>'0')END G<0), and the composite index idx_E_F includes two fields, E and F. The result of the second query statement is obtained in Table 9 according to the second query condition.
[0137] (1) First, find the left prefix variable E of the composite index according to the second query condition. The database scans the part of the index data where E>0, that is, Figure 4 Region 1 in the idx_E_F table is the first data range in this application.
[0138] (2) Variable F is a composite index non-prefix variable, and the relational expression corresponding to variable F is F>'0'. The second data range is obtained based on the relational expression F>'0' for the data in the scanned first data range, that is, Figure 4 The part corresponding to area 2 in .
[0139] (3) The primary keys of area 2 are 3 and 4. Use primary keys 3 and 4 to query table 1 for data that meets the relational expression G<0, that is, the part corresponding to area 3 in table 1, which is the third data range, or the result of the second query statement.
[0140] Through the above three steps, the result of each second query statement in the multiple second query statements can be determined.
[0141] S104: Obtain a data query result of the first query statement according to the data query result of each second query statement in the plurality of second query statements.
[0142] When each second query statement is connected by a logical OR, a logical OR is performed on the results obtained by each second query statement to obtain a data query result that satisfies the first query statement.
[0143] It can be seen that the secondary index query method of the database provided by the present application splits the complex first query statement into multiple logically simple second query statements. The execution of simple query statements is faster than that of complex query statements. By splitting the first query statement, the query execution efficiency can be improved. By optimizing each second query statement by applying preset rules, the result of each second query statement can be determined. The application of query optimization rules can more accurately determine the data that meets the query conditions and avoid unnecessary data returns. By implementing this application, the number of secondary index return table requests and the waste of computing resources can be reduced, and long-tail latency can be reduced.
[0144] This application provides a data query device, see Figure 5 , Figure 5 A schematic diagram of the structure of a data query device 400 provided in the present application, the device 400 includes:
[0145] An acquisition module 410 is used to acquire a first query statement input by a user, where the first query statement includes a first query condition;
[0146] A splitting module 420 is used to split the first query statement into multiple second query statements, wherein the second query condition included in at least one of the second query statements includes multiple relational expressions for different variables, and the relational expressions for the multiple different variables are in a logical AND relationship; the result of the logical OR connection of the second query conditions included in the multiple second query statements is equal to the first query condition;
[0147] A determination module 430, configured to determine a data query result of each second query statement in a plurality of second query statements;
[0148] The determination module 430 is further configured to obtain a data query result of the first query statement according to a data query result of each second query statement in the plurality of second query statements.
[0149] In a possible implementation, when the second query condition included in the second query statement includes a relational expression of a composite index variable, the determination module 430 is used to:
[0150] Determine the composite index data set corresponding to the composite index variable;
[0151] Determine a first data range that satisfies a relational expression corresponding to a left prefix variable in the composite index variable from the composite index data set, wherein the relational expression of the composite index variable includes a relational expression corresponding to the left prefix variable and a relational expression corresponding to a non-left prefix variable;
[0152] A second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable is determined from the first data range to obtain a data query result of the second query statement.
[0153] In a possible implementation, the first query statement includes a statement for establishing a composite index;
[0154] The determination module 430 is further configured to: according to the composite index establishment statement, intercept data for each variable in the composite index variable and the data primary key from the original data set to obtain a composite index data set.
[0155] Based on the second aspect, in a possible implementation, when the second query condition included in the second query statement also includes a relational expression of a non-composite index variable, the determination module 430 is used to:
[0156] According to the data primary key in the second data range, a third data range satisfying the relational expression of the non-composite index variable is determined from the original data set to obtain the data query result of the second query statement.
[0157] In a possible implementation, when the second query condition included in the second query statement includes multiple relational expressions for the same variable, the determination module 430 is used to:
[0158] Combine multiple relational expressions for the same variable in the second query condition.
[0159] In a possible implementation, the determination module 430 is used to:
[0160] If the first data range is an empty set, determining that the result of the second query statement is an empty set;
[0161] If the second data range is an empty set, then determining that the result of the second query statement is an empty set; or,
[0162] If the third data range is an empty set, it is determined that the result of the second query statement is an empty set.
[0163] In a possible implementation, when the non-left prefix variable in the composite index variable includes a first variable and a second variable, and the first variable is in front and the second variable is in the back, the determination module 430 is used to:
[0164] Determine, from the first data range, a fourth data range that satisfies the relational expression corresponding to the first variable;
[0165] From the fourth data range, determine the data range that satisfies the relational expression corresponding to the second variable, that is, obtain the second data range.
[0166] Figure 5 The various functional modules in are used to implement Figures 1 to 4 For details of the steps of the method embodiment, please refer to Figures 1 to 4 The description of the relevant contents in the method embodiment will not be repeated here for the sake of brevity of the specification.
[0167] This application provides a computing device, see Figure 6 , Figure 6 A schematic diagram of the structure of a computing device 500 provided in this application, the computing device is used to implement Figures 1 to 4 In the method embodiment, the computing device 500 includes: a processor 510, a communication interface 520, and a memory 530. The processor 510, the communication interface 520, and the memory 530 can be interconnected via an internal bus 540, or can communicate via other means such as wireless transmission.
[0168] Taking the connection through bus 540 as an example, bus 540 may be a peripheral component interconnect (PCI) bus or an extended industry standard architecture (EISA) bus, etc. Bus 540 may be divided into an address bus, a data bus, a control bus, etc. For ease of representation, Figure 6 Only one thick line is used in the diagram, but this does not mean that there is only one bus or only one type of bus.
[0169] The processor 510 may be composed of at least one general-purpose processor, such as a CPU, or a combination of a CPU and a hardware chip. The hardware chip may be an application-specific integrated circuit (ASIC), a programmable logic device (PLD), or a combination thereof. The PLD may be a complex programmable logic device (CPLD), a field-programmable gate array (FPGA), a generic array logic (GAL), or any combination thereof. The processor 510 executes various types of digital storage instructions, such as software or firmware programs stored in the memory 530, which enables the computing device 500 to provide a wide variety of services.
[0170] The memory 530 is used to store program codes and is controlled by the processor 510 to execute the above Figures 1 to 4 For the steps described in the embodiment, please refer to the relevant description of the embodiment shown above, which will not be elaborated here.
[0171] The memory 530 may include a volatile memory, such as a RAM; the memory 530 may also include a non-volatile memory, such as a ROM, a flash memory; the memory 530 may also include a combination of the above types.
[0172] The communication interface 520 can be a wired interface (such as an Ethernet interface), an internal interface (such as a high-speed serial computer expansion bus (peripheral component interconnect express, PCIE) bus interface), a wired interface (such as an Ethernet interface) or a wireless interface (such as a cellular network interface or a wireless local area network interface) for communicating with other devices or modules.
[0173] The processor 510, communication interface 520, etc. in the computing device 500 can implement the functions and / or various steps and methods implemented in the above-mentioned method embodiments, and for the sake of brevity, they are not described here. The acquisition module 410, the splitting module 420, and the determination module 430 in the apparatus 400 can be located in the processor 510 in the computing device 500.
[0174] Need to explain, Figure 5 This is only a possible implementation of the embodiment of the present application. In practical applications, the computing device may also include more or fewer components, which is not limited here. For the contents not shown or described in the embodiment of the present application, please refer to the relevant description in the embodiment described in the aforementioned method, which will not be repeated here.
[0175] The present application also provides a readable storage medium, including program instructions. When the program instructions are executed by a computing device, the computing device executes part or all of the steps described in the above-mentioned database secondary index query method embodiment.
[0176] The present application also provides a computer program product, including program instructions. When the program instructions are executed by a computing device, the computing device executes part or all of the steps described in the above-mentioned database secondary index query method embodiment.
[0177] In the above embodiments, the description of each embodiment has its own emphasis. For parts that are not described in detail in a certain embodiment, reference can be made to the relevant descriptions of other embodiments.
[0178] In the above embodiments, it can be implemented in whole or in part by software, hardware or any combination thereof. When implemented using software, it can be implemented in whole or in part in the form of a computer program product. The computer program product may include code. When the computer program product is read and executed by a computer, some or all steps of the method recorded in the above method embodiment may be implemented. The computer may be a general-purpose computer, a special-purpose computer, a computer network, or other programmable devices. The computer instructions may be stored in a computer-readable storage medium, or transmitted from one computer-readable storage medium to another computer-readable storage medium, for example, the computer instructions may be transmitted from a website site, computer, server or data center by wired (e.g., coaxial cable, optical fiber, digital subscriber line) or wireless (e.g., infrared, wireless, microwave, etc.) mode to another website site, computer, server or data center. The computer-readable storage medium may be any available medium that a computer can access or a data storage device such as a server or data center that includes one or more available media integration. The available medium may be a magnetic medium (e.g., a floppy disk, a hard disk, a tape), an optical medium, or a semiconductor medium, etc.
[0179] The steps in the method of the embodiment of the present application can be adjusted in order, combined or deleted according to actual needs; the units in the device of the embodiment of the present application can be divided, combined or deleted according to actual needs.
[0180] The embodiments of the present application are introduced in detail above. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method and core idea of the present application. At the same time, for general technical personnel in this field, according to the idea of the present application, there will be changes in the specific implementation method and application scope. In summary, the content of this specification should not be understood as a limitation on the present application.
Claims
1. A data query method, characterized in that: The method comprises: Acquire a first query statement input by a user, where the first query statement includes a first query condition; Splitting the first query statement into multiple second query statements, wherein the second query condition included in at least one of the second query statements includes multiple relational expressions for different variables, and the relational expressions for the multiple different variables are in a logical AND relationship; the result of the logical OR connection of the second query conditions included in the multiple second query statements is equal to the first query condition; Determine a data query result of each second query statement in the plurality of second query statements; The data query result of the first query statement is obtained according to the data query result of each second query statement in the multiple second query statements.
2. The method according to claim 1, characterized in that In a case where the second query condition included in the second query statement includes a relational expression of a composite index variable, determining a data query result of the second query statement includes: Determine a composite index data set corresponding to the composite index variable; Determine from the composite index data set a first data range that satisfies a relational expression corresponding to a left prefix variable in the composite index variable, wherein the relational expression of the composite index variable includes a relational expression corresponding to the left prefix variable and a relational expression corresponding to a non-left prefix variable; A second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable is determined from the first data range to obtain a data query result of the second query statement.
3. The method according to claim 2, characterized in that The first query statement includes a statement for establishing a composite index; Before determining the composite index data set corresponding to the composite index variable, the method further includes: According to the composite index creation statement, data for each variable in the composite index variable and the data primary key are intercepted from the original data set to obtain the composite index data set.
4. The method according to claim 2 or 3, characterized in that In the case where the second query condition included in the second query statement also includes a relational expression of a non-composite index variable, after determining the second data range of the relational expression corresponding to the non-left prefix variable that satisfies the composite index, determining the data query result of the second query statement further includes: According to the data primary key in the second data range, a third data range satisfying the relational expression of the non-composite index variable is determined from the original data set to obtain the data query result of the second query statement.
5. The method according to any one of claims 2 to 4, characterized in that: In the case where the second query condition included in the second query statement includes multiple relational expressions for the same variable, before determining the data query result of the second query statement, the method further includes: In the second query condition, multiple relational expressions for the same variable are merged.
6. The method according to claim 4, characterized in that The method further comprises: If the first data range is an empty set, determining that the data query result of the second query statement is an empty set; If the second data range is an empty set, determining that the data query result of the second query statement is an empty set; or, If the third data range is an empty set, it is determined that the data query result of the second query statement is an empty set.
7. The method according to claim 2, characterized in that In the case where the non-left prefix variable in the composite index variable includes a first variable and a second variable, and the first variable is in front and the second variable is in the back, determining from the first data range a second data range that satisfies the relational expression corresponding to the non-left prefix variable in the composite index variable includes: Determine, from the first data range, a fourth data range that satisfies the relational expression corresponding to the first variable; From the fourth data range, determine the data range that satisfies the relational expression corresponding to the second variable, that is, obtain the second data range.
8. A data query device, characterized in that: The device comprises: An acquisition module, configured to acquire a first query statement input by a user, wherein the first query statement includes a first query condition; a splitting module, configured to split the first query statement into a plurality of second query statements, wherein a second query condition included in at least one of the second query statements includes a plurality of relational expressions for different variables, and the relational expressions for the plurality of different variables are in a logical AND relationship; and a result of a logical OR connection of the second query conditions included in the plurality of second query statements is equal to the first query condition; A determination module, configured to determine a data query result of each second query statement in the plurality of second query statements; The determination module is further configured to obtain the data query result of the first query statement according to the data query result of each second query statement in the plurality of second query statements.
9. A computing device, characterized in that The method comprises a memory and a processor, wherein the memory is used to store instructions, and the processor is used to execute the instructions stored in the memory to implement the method according to any one of claims 1 to 7.
10. A computer program product, characterized in that The method comprises program instructions, and when the program instructions are executed on a computing device, the computing device is caused to execute the method according to any one of claims 1 to 7.
11. A computer storage medium, characterized in that: The method comprises program instructions, and when the program instructions are executed by a computing device, the computing device is caused to execute the method according to any one of claims 1 to 7.
