Data query method and device, electronic equipment and storage medium
By determining the execution conditions based on the query statement type in the distributed database, and prioritizing the query of the data table with the smallest data volume, the problems of low query efficiency and high resource consumption are solved, and more efficient data query is achieved.
Patent Information
- Application Number
- CN202311205473.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2023-09-18
- Publication Date
- 2026-11-20
- Estimated Expiration
- 2043-09-18
AI Technical Summary
When querying data in a distributed database, the query efficiency is low and the resource consumption is high, especially when the data table being queried is large, which leads to excessive communication time and storage space usage.
By obtaining the target query statement, determining whether it meets the preset execution conditions based on its type, prioritizing the query of the first data table with the smallest data volume, and then querying the remaining data tables in turn to obtain the target query result.
It reduces the overall communication time and storage space required for the query process, improves query efficiency, and reduces resource consumption.
Smart Images

Figure CN117370381B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of distributed database technology, and in particular to a data query method, apparatus, electronic device and storage medium. Background Technology
[0002] With economic and technological development, traditional centralized databases are increasingly unable to meet the growing demand for data. To adapt to the development of the digital economy, distributed databases, characterized by high concurrency, high performance, and high availability, have emerged. One of the most frequently used operations in distributed databases is data querying. Because distributed databases need to establish data relationships between data nodes, this adds extra communication time and storage space. Therefore, optimizing data query efficiency is particularly important in a distributed environment.
[0003] However, in existing technologies, when querying data in a distributed database, the data table is usually queried according to the original execution logic of the query statement. When the data table to be queried is large, the entire query statement will require a large amount of communication time and storage space during execution, resulting in low query efficiency and high resource consumption. Summary of the Invention
[0004] This application provides a data query method, apparatus, electronic device, and storage medium to solve the problems of low query efficiency and high resource consumption when querying data in a distributed database in the prior art.
[0005] Firstly, this application provides a data query method, the method comprising:
[0006] Obtain the target query statement, wherein the target query statement is associated with multiple data tables in a distributed database;
[0007] Based on the type of the target query statement, determine whether the target query statement meets the preset execution conditions, wherein the preset execution conditions are different for different types of target query statements;
[0008] When the target query statement meets the preset execution conditions, the data in the first data table and the remaining data table are queried in sequence, and the target query result of the target query statement is determined based on the data obtained from the query. The first data table is the data table with the smallest amount of data among the multiple data tables, and the remaining data tables are the other data tables remaining among the multiple data tables besides the first data table.
[0009] Furthermore, the target query statement includes a main query statement and subquery statements nested within the main query statement;
[0010] The step of determining whether the target query statement meets the preset execution conditions based on the type of the target query statement includes:
[0011] If the type of the target query statement is a first query type, determine whether the first data table is associated with the main query statement;
[0012] When the first data table is associated with the main query statement, it is determined that the target query statement satisfies the preset execution conditions.
[0013] Furthermore, the target query statement includes a main query statement and subquery statements nested within the main query statement;
[0014] The step of determining whether the target query statement meets the preset execution conditions based on the type of the target query statement includes:
[0015] If the type of the target query statement is a second query type or a third query type, determine whether the first data table is associated with the main query statement or whether the first data table is associated with the subquery statement;
[0016] When the first data table is associated with the main query statement, or when the first data table is associated with the subquery statement, it is determined that the target query statement satisfies the preset execution conditions.
[0017] Furthermore, the method also includes:
[0018] When the type of the target query statement is the third query type, the association connection type of the target query statement is determined, wherein the association connection type includes inner join, left join, right join and full join;
[0019] When the association connection type is the inner join type, the first data table is any data table associated with the main query statement or any data table associated with the subquery statement;
[0020] When the associated connection type is the left join type, and the subquery statement is associated with the left table in the main query statement, the first data table is the left table in the main query statement;
[0021] When the association connection type is the left join type and the subquery statement is associated with the right table in the main query statement, the first data table is the right table in the main query statement, or any data table associated with the subquery statement;
[0022] When the association connection type is the right join type, and the subquery statement is associated with the right table in the main query statement, the first data table is the right table in the main query statement;
[0023] When the associated connection type is the right join type and the subquery statement is associated with the left table in the main query statement, the first data table is the left table in the main query statement, or any data table associated with the subquery statement.
[0024] When the association connection type is the full connection type, the first data table is any data table associated with the main query statement.
[0025] Further, the step of sequentially querying data in the first data table and the remaining data table, and determining the target query result of the target query statement based on the queried data, includes:
[0026] Send a first data query request to the data node storing the first data table to obtain a first intermediate result, wherein the first data query request is used to request a query on the first data table;
[0027] Based on the first intermediate result, a second data query request is sent to the data node storing the remaining data table to obtain a second intermediate result, wherein the second data query request is used to request a query on the remaining data table;
[0028] Based on the first intermediate result and the second intermediate result, the target query result of the target query statement is determined.
[0029] Further, the step of sending a second data query request to the data node storing the remaining data table based on the first intermediate result to obtain a second intermediate result includes:
[0030] When the target query statement includes multiple sub-query statements and the first data table is associated with any of the sub-query statements, a third data query request is sent to the data node storing the second data table based on the first intermediate result to obtain a third intermediate result. The second data table is the data table in the remaining data table that is located in the same sub-query statement as the first data table. The third data query request is used to request a query on the second data table.
[0031] Based on the third intermediate result, a fourth data query request is sent to the data node storing the third data table to obtain the second intermediate result. The third data table is a data table in the remaining data table that is located in a different subquery statement than the first data table. The fourth data query request is used to request a query on the third data table.
[0032] Furthermore, after sequentially querying the data in the first data table and the remaining data table, and determining the target query result of the target query statement based on the queried data, the method further includes:
[0033] Output and display the target query results of the target query statement.
[0034] Secondly, this application provides a data query device, the device comprising:
[0035] The acquisition module is used to acquire the target query statement, wherein the target query statement is associated with multiple data tables in the distributed database;
[0036] The first determining module is used to determine whether the target query statement meets the preset execution conditions based on the type of the target query statement, wherein the preset execution conditions are different for different types of target query statements;
[0037] The second determining module is used to sequentially query the data in the first data table and the remaining data table when the target query statement meets the preset execution conditions, and determine the target query result of the target query statement based on the queried data, wherein the first data table is the data table with the smallest data volume among the plurality of data tables, and the remaining data tables are the other data tables remaining among the plurality of data tables besides the first data table.
[0038] Thirdly, this application provides an electronic device, including a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus;
[0039] Memory, used to store computer programs;
[0040] A processor, when executing a program stored in memory, implements the steps of the data query method described in any of the preceding claims.
[0041] Fourthly, this application also provides a computer-readable storage medium having a computer program stored thereon, which, when executed by a processor, implements the steps of the data query method described in any of the preceding claims.
[0042] Compared with the prior art, the technical solution provided in this application has the following advantages: The method provided in this application obtains a target query statement, wherein the target query statement is associated with multiple data tables in a distributed database; based on the type of the target query statement, it is determined whether the target query statement meets preset execution conditions, wherein different types of target query statements correspond to different preset execution conditions; when the target query statement meets the preset execution conditions, data in the first data table and the remaining data tables are queried sequentially, and the target query result of the target query statement is determined based on the queried data, wherein the first data table is the data table with the smallest data volume among the multiple data tables, and the remaining data tables are the other data tables remaining among the multiple data tables besides the first data table. Through the above method, when data querying is required on multiple data tables in a distributed database, and the target query statement meets the preset execution conditions, the first data table with the smallest data volume can be queried first, followed by the remaining data tables with larger data volumes. This reduces the overall communication time and storage space of the query process, thereby improving query efficiency and reducing resource consumption. Attached Figure Description
[0043] The accompanying drawings, which are incorporated in and form part of this specification, illustrate embodiments consistent with the invention and, together with the description, serve to explain the principles of the invention.
[0044] To more clearly illustrate the technical solutions in the embodiments of the present invention or the prior art, the drawings used in the description of the embodiments or the prior art will be briefly introduced below. Obviously, for those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0045] One or more embodiments are illustrated by way of example with reference numerals in the accompanying drawings. These illustrations do not constitute a limitation on the embodiments. Elements with the same reference numerals in the drawings are denoted as similar elements. Unless otherwise stated, the figures in the drawings are not to be limited by scale.
[0046] Figure 1 A flowchart illustrating a data query method provided in an embodiment of this application;
[0047] Figure 2 This is a schematic diagram of the structure of a data query device provided in an embodiment of this application;
[0048] Figure 3 This is a schematic diagram of the structure of an electronic device provided in an embodiment of this application. Detailed Implementation
[0049] To make the objectives, technical solutions, and advantages of the embodiments of this application clearer, the technical solutions of the embodiments of this application will be clearly and completely described below with reference to the accompanying drawings. Obviously, the described embodiments are only some embodiments of this application, not all embodiments. Based on the embodiments of this application, all other embodiments obtained by those skilled in the art without creative effort are within the scope of protection of this application.
[0050] The following disclosure provides numerous different embodiments or examples for implementing various structures of the invention. To simplify the disclosure, specific examples of components and arrangements are described below. These are merely examples and are not intended to limit the scope of the invention. Furthermore, reference numerals and / or letters may be repeated in different examples. Such repetition is for simplification and clarity and does not in itself indicate a relationship between the various embodiments and / or arrangements discussed.
[0051] See Figure 1 , Figure 1 This is a flowchart illustrating a data query method provided in an embodiment of this application. Figure 1 As shown, this data query method includes the following steps:
[0052] Step 101: Obtain the target query statement, which is associated with multiple data tables in the distributed database.
[0053] It should be noted that the data query method provided in this application can be used in fields such as databases, distributed databases, middleware, distributed middleware, and distributed storage. For ease of understanding, subsequent embodiments will use distributed databases as an example.
[0054] Specifically, the aforementioned target query statement refers to a statement that queries data in a distributed database. This target query statement can associate multiple data tables in the distributed database, and the final query result of the target query statement can be obtained by analyzing the query results of multiple data tables.
[0055] The target query statement can be obtained by receiving user input or by receiving a data query request and parsing it from the data query request. This application embodiment does not make specific limitations.
[0056] Step 102: Determine whether the target query statement meets the preset execution conditions based on the type of the target query statement. Different types of target query statements have different preset execution conditions.
[0057] Specifically, the types of the target query statements mentioned above can include, but are not limited to, first query types (such as query types containing SELECT LIST subqueries), second query types (such as query types containing WHERE subqueries), and third query types (such as query types containing ON subqueries). Since different types of target query statements correspond to different preset execution conditions, it is necessary to determine the preset execution conditions corresponding to the target query statement based on its type, and to determine whether the target query statement meets these preset execution conditions. If the target query statement meets the preset execution conditions, then step 103 is executed; if the target query statement does not meet the preset execution conditions, then the data table is queried according to the original execution logic to obtain the query results.
[0058] Step 103: If the target query statement meets the preset execution conditions, query the data in the first data table and the remaining data tables in sequence, and determine the target query result of the target query statement based on the data obtained. The first data table is the data table with the smallest amount of data among the multiple data tables, and the remaining data tables are the other data tables besides the first data table among the multiple data tables.
[0059] In this step, when the target query statement meets the preset execution conditions, the first data table with the smallest data volume among the multiple data tables associated with the target query statement can be queried first to obtain the data in the first data table. Then, the other remaining data tables with larger data volumes among the multiple data tables are queried to finally obtain the target query result of the target query statement.
[0060] In this embodiment, when it is necessary to query data from multiple tables in a distributed database, and the target query statement meets the preset execution conditions, the first data table with the smallest data volume can be queried first, followed by the remaining data tables with larger data volumes. This can reduce the overall communication time and storage space of the query process, thereby improving query efficiency and reducing resource consumption.
[0061] Furthermore, the target query statement includes the main query statement and subquery statements nested within the main query statement;
[0062] Step 102 above, based on the type of the target query statement, determines whether the target query statement meets the preset execution conditions, specifically including:
[0063] If the target query statement is of type 1, determine whether the first data table is associated with the main query statement;
[0064] When the first data table is associated with the main query statement, determine whether the target query statement meets the preset execution conditions.
[0065] Specifically, the first query type mentioned above can be understood as the query type of a SELECTLIST subquery statement nested within a main query statement.
[0066] In one embodiment, when the type of the target query statement is a first query type, it can be determined whether the first data table is associated with the main query statement. If the first data table is associated with the main query statement, it can be determined that the target query statement meets the preset execution conditions. If the first data table is not associated with the main query statement, it can be determined that the target query statement does not meet the preset execution conditions.
[0067] For example, suppose the target query is `select t1.*,(select b from t2 where t2.a=t1.a)from t1`, where table t1 is the smaller table (i.e., the first data table) and table t2 is the larger table. Therefore, in this target query, the first data table t1 is associated with the main query, and thus the target query meets the preset execution conditions. The execution logic of this target query is as follows:
[0068] Step 1: Query table t1 and issue the data node query statement: select * from t1;
[0069] Step 2: Using the association conditions and the data in table t1 obtained in Step 1, query the required data in table t2 and issue the data node query statement: select * from t2 where t2.a in (data in field a of table t1);
[0070] Step 3: Calculate the subquery and output the result.
[0071] In this example, t1 and t2 are equi-joined. Of course, other non-equi-joined relationships can also use a similar method. The core is still to reduce the amount of data retrieved from table t2, which will not be elaborated here. Assuming that table t1 is the large table and table t2 is the small table (i.e., the first data table), then the first data table t2 is not joined with the main query statement. Therefore, the target query statement does not meet the preset execution conditions at this time.
[0072] In this embodiment, for the target query statement of the first query type, since the number of rows in the result set of the target query statement is determined by the main query, the first data table can only be associated with the main query statement, and the subquery is driven by the result of the main query, that is, the small table drives the large table, thereby reducing the overall communication time and storage space of the query process.
[0073] Furthermore, the target query statement includes the main query statement and subquery statements nested within the main query statement;
[0074] Step 102 above, based on the type of the target query statement, determines whether the target query statement meets the preset execution conditions, specifically including:
[0075] If the target query statement is of type 2 or type 3, determine whether the first data table is associated with the main query statement or whether the first data table is associated with the subquery statement.
[0076] When the first data table is associated with the main query statement, or when the first data table is associated with the subquery statement, determine that the target query statement meets the preset execution conditions.
[0077] Specifically, the second query type mentioned above can be understood as a query type where the subquery nested within the main query statement is a WHERE subquery statement. The third query type mentioned above can be understood as a query type where the subquery nested within the main query statement is an ON subquery statement.
[0078] In one embodiment, when the target query statement is of the second query type or the third query type, it can be determined whether the first data table is associated with the main query statement or whether the first data table is associated with the subquery statement. If it is determined that the first data table is associated with the main query statement or the first data table is associated with the subquery statement, it can be determined that the target query statement meets the preset execution conditions. If the first data table is neither associated with the main query statement nor with the subquery statement, it can be determined that the target query statement does not meet the preset execution conditions.
[0079] For example, suppose the target query for the second query type is `select * from t1 where exists(select b from t2 where t1.a=t2.a)`, where table t1 is the smaller table (i.e., the first data table) and table t2 is the larger table. Therefore, in this target query, the first data table t1 is associated with the main query, and thus the target query satisfies the preset execution conditions. The execution logic of this target query is as follows:
[0080] Step 1: Query table t1 and issue the data node query statement: select * from t1;
[0081] Step 2: Using the association conditions and the data in table t1 obtained in Step 1, query the required data in table t2 and issue the data node query statement: select * from t2 where t2.a in (data in field a of table t1);
[0082] Step 3: Calculate the EXISTS subquery and output the result.
[0083] Continuing with this example, assuming table t1 is the larger table and table t2 is the smaller table (i.e., the first data table), then in this target query statement, the first data table t2 is associated with the subquery statement, thus satisfying the preset execution conditions. The execution logic of this target query statement is as follows:
[0084] Step 1: Query table t2 and issue the following query statement to the data node: select a from t2;
[0085] Step 2: Using the association conditions and the data in table t2 obtained in Step 1, query the required data in table t1 and issue the data node query statement: select * from t1 where t1.a in (data in field a of table t2);
[0086] Step 3: Output the data retrieved in Step 2 directly as the result.
[0087] For example, suppose the target query for the third query type is `select * from t1 join t2 on t1.a = t2.a and exists(select c from t3 where t3.b = t1.b)`. Since it's an inner join, the `on` condition can be placed in the `where` condition. Therefore, the execution logic of this target query is the same as the target query for the second query type in the example above, and will not be repeated here.
[0088] In this embodiment, for target query statements of the second or third query type, bidirectional driving between the inner and outer tables is supported. That is, if the first data table is associated with a subquery statement, the subquery is executed first, followed by the main query, using the subquery to filter the main query or filtering the main query in batches. If the first data table is associated with a main query statement, the main query is executed first, followed by the subquery, using the main query to filter the subquery or filtering the subquery in batches. This allows a small table to drive a large table, effectively reducing the overall communication time and storage space required for the query process.
[0089] Furthermore, the method also includes:
[0090] When the type of the target query statement is the third query type, determine the type of association connection of the target query statement, where the type of association connection includes inner join, left join, right join and full join;
[0091] When the join type is inner join, the first data table can be any data table associated with the main query or any data table associated with the subquery.
[0092] When the join type is left join and the subquery is related to the left table in the main query, the first data table is the left table in the main query.
[0093] When the join type is left join and the subquery is associated with the right table in the main query, the first data table is either the right table in the main query or any data table associated with the subquery.
[0094] When the join type is right join and the subquery is related to the right table in the main query, the first data table is the right table in the main query.
[0095] When the join type is right join and the subquery is associated with the left table in the main query, the first data table is either the left table in the main query or any data table associated with the subquery.
[0096] When the join type is full join, the first data table is any data table associated with the main query statement.
[0097] In one embodiment, the join type of the target query statement in the third query type may include inner join, left join, right join, and full join. An inner join returns only rows where the join fields in both tables are equal; a left join returns all records from the left table and all records from the right table with equal join fields; a right join returns all records from the right table and all records from the left table with equal join fields; and a full join returns all records from both the left and right tables. The location of the first data table differs for different join types.
[0098] Specifically, when the join type is inner join, the first data table can be any data table associated with the main query statement or any data table associated with the subquery statement. For details, please refer to the example where the target query statement is the second query type, which will not be repeated here.
[0099] When the join type is left join and the subquery is associated with the left table in the main query, the first table is the left table in the main query.
[0100] For example, suppose the target query is `select * from t1 left join t2 on t1.a=t2.a and exists(select c from t3 where t3.b=t1.b)`, where t1 is the smaller table (i.e., the first data table), and t2 and t3 are the larger tables. In this case, the execution logic of the target query is as follows:
[0101] Step 1: Query table t1 and issue the data node query statement: select * from t1;
[0102] Step 2: Using the association conditions and the data in table t1 obtained in Step 1, query the required data in table t2 and issue the data node query statement: select * from t2 where t1.a in (data in field a of table t1);
[0103] Step 3: Using the retrieved data from table t1 and the associated conditions, retrieve the required data from table t3 by issuing the data node query statement: select * from t3 where t3.b in (data from field b in table t1);
[0104] Step 4: Calculate the left join and subquery, and output the results.
[0105] For example, suppose the target query is `select * from t1 left join t2 on t1.a = t2.a and exists(select c from t3 where t2.b = t3.b)`, where t1 is the smaller table (i.e., the first data table), and t2 and t3 are the larger tables. In this case, the execution logic of the target query is as follows:
[0106] Step 1: Query table t1 and issue the data node query statement: select * from t1;
[0107] Step 2: Using the association conditions and the data in table t1 obtained in Step 1, query the required data in table t2 and issue the data node query statement: select * from t2 where t1.a in (data in field a of table t1);
[0108] Step 3: Using the retrieved data from table t2 and the associated conditions, retrieve the required data from table t3 and issue the data node query statement: select b from t3 where t3.b in (data from field b in table t2);
[0109] Step 4: Calculate the left join and subquery, and output the results.
[0110] It should be noted that `left join` indicates that the join type of the target query statement is a left join, where `t1` is the left table and `t2` is the right table. Since the subquery is in the `ON` condition of the `left join`, and the subquery statement is related to the left table in the main query statement, the subquery can only be driven by the main query; otherwise, the query result will be incorrect. Therefore, in this example, the first data table is the left table in the main query statement, i.e., table `t1` must be the smaller table. If the condition is not met, the data query method provided in this application cannot be used for optimization.
[0111] When the join type is left join and the subquery is associated with the right table in the main query, the first table is either the right table in the main query or any table associated with the subquery.
[0112] For example, suppose the target query is `select * from t1 left join t2 on t1.a=t2.a and exists(select c from t3 where t2.b=t3.b)`, where t1 is the larger table, t2 is the smaller table, and t3 is the smaller table (i.e., the first data table). The execution logic of this target query is as follows:
[0113] Step 1: Query table t1 and issue the data node query statement: select * from t1;
[0114] Step 2: Query table t3 and issue the data node query statement: select * from t3;
[0115] Step 3: Using the retrieved data from table t3 and the associated conditions, retrieve the required data from table t2 and issue the data node query statement: select * from t2 where t2.b in (data from field b in table t3);
[0116] Step 4: Calculate the left join and subquery, and output the results.
[0117] It's important to note that `left join` indicates that the join type of the target query is a left join, where `t1` is the left table and `t2` is the right table. Since the subquery is in the `ON` condition of the `left join`, and the subquery is related to the right table in the `left join`, you can filter the subquery using either the right table or the subquery using the right table. Therefore, in this example, the first data table is `t2` in the main query. Of course, you can also use `t3` in the subquery as the first data table for data retrieval.
[0118] Similarly, when a subquery is in the ON condition of a right join and is related to the right table in the right join, the subquery can only be driven by the main query. Otherwise, the query result will be incorrect. Therefore, when the join type is right join and the subquery is related to the right table in the main query, the first data table is the right table in the main query. If the condition is not met, the optimization method in this patent cannot be used.
[0119] Similarly, if a subquery is in the ON condition of a RIGHT JOIN and is related to the left table of the RIGHT JOIN, then either the left table can be used to filter the subquery, or the subquery can be used to filter the left table. Therefore, when the join type is RIGHT JOIN and the subquery is related to the left table in the main query, the first table is either the left table in the main query or any table related to the subquery.
[0120] When the join type is full join, since only the main query can drive the subquery, the first data table can be any data table associated with the main query statement.
[0121] In this embodiment, based on the association connection type of the target query statement of the third query type, it can be further determined whether the first data table is the data table corresponding to the main query statement or the subquery statement, and further determined whether the target query statement of the third query type meets the preset execution conditions, thereby ensuring the accuracy of the query results.
[0122] Further, step 103 above, which involves sequentially querying the data in the first data table and the remaining data tables, and determining the target query result of the target query statement based on the queried data, specifically includes:
[0123] Send a first data query request to the data node storing the first data table to obtain a first intermediate result, wherein the first data query request is used to request a query on the first data table;
[0124] Based on the first intermediate result, a second data query request is sent to the data node storing the remaining data table to obtain the second intermediate result. The second data query request is used to request a query on the remaining data table.
[0125] Based on the first and second intermediate results, determine the target query result of the target query statement.
[0126] In one embodiment, when the target query statement meets preset execution conditions, a first data query request can be sent to the data node storing the first data table to obtain a first intermediate result. Then, based on the first intermediate result, a second data query request can be sent to the data node storing the remaining data tables to obtain a second intermediate result. Finally, based on the first and second intermediate results, the target query result of the target query statement is determined. In this way, by prioritizing the query of the first data table with the smallest data volume and then querying the remaining data tables with larger data volumes, the overall communication time and storage space of the query process can be reduced, thereby improving query efficiency and reducing resource consumption.
[0127] Furthermore, based on the first intermediate result, the above steps involve sending a second data query request to the data node storing the remaining data table to obtain the second intermediate result, specifically including:
[0128] When the target query statement includes multiple subquery statements and the first data table is associated with any of the subquery statements, a third data query request is sent to the data node storing the second data table based on the first intermediate result to obtain the third intermediate result. The second data table is the data table in the remaining data table that is in the same subquery statement as the first data table. The third data query request is used to request a query on the second data table.
[0129] Based on the third intermediate result, a fourth data query request is sent to the data node storing the third data table to obtain the second intermediate result. The third data table is a data table in the remaining data table that is located in a different subquery statement than the first data table. The fourth data query request is used to request a query on the third data table.
[0130] In one embodiment, when the target query statement includes multiple subqueries, and the first data table is associated with any of the subqueries, after sending a first data query request to the data node storing the first data table and obtaining a first intermediate result, a third data query request can be sent to the data node storing the second data table, which is located in the same subquery as the first data table, based on the first intermediate result, to obtain a third intermediate result. Then, based on the third intermediate result, a fourth data query request can be sent to the data node storing the third data table, which is located in a different subquery than the first data table, to obtain a second intermediate result. It should be noted that the second and third data tables here can represent one data table or multiple data tables, and this is not limited here.
[0131] For example, suppose the target query is `select * from t1 where exists(select c from t2 where t1.a=t2.a)and exists(select c from t3 where t3.b=t1.b)`, where t1 is the larger table, t2 is the smaller table (i.e., the first data table), and t3 is the larger table. Therefore, this target query contains two subqueries. The first data table t2 is joined with table t1 in the first subquery. Thus, after querying table t2, table t1 is queried first, and then table t3 is queried.
[0132] At this point, the execution logic of the target query statement is as follows:
[0133] Step 1: Query table t2 and issue the data node query statement: select * from t2;
[0134] Step 2: Using the association conditions and the data in table t2 obtained in Step 1, query the required data in table t1 and issue the data node query statement: select * from t1 where t1.a in (data in field a of table t2);
[0135] Step 3: Using the retrieved data from table t1 and the associated conditions, retrieve the required data from table t3 by issuing the data node query statement: select * from t3 where t3.b in (data from field b in table t1);
[0136] Step 4: Calculate the subquery and output the result.
[0137] This approach effectively improves query efficiency while ensuring the accuracy of query results.
[0138] Furthermore, after step 103 above, sequentially querying the data in the first data table and the remaining data table, and determining the target query result of the target query statement based on the queried data, the method further includes:
[0139] Output and display the target query results of the target query statement.
[0140] In one embodiment, after sequentially querying the data in the first data table and the remaining data table, and determining the target query result of the target query statement based on the queried data, the target query result of the target query statement can be output and displayed to facilitate user querying and subsequent calculations using the target query result.
[0141] See Figure 2 , Figure 2 This is a schematic diagram of a data query device provided in an embodiment of this application. Figure 2 As shown, the data query device 200 may include:
[0142] The acquisition module 201 is used to acquire the target query statement, wherein the target query statement is associated with multiple data tables in the distributed database;
[0143] The first determining module 202 is used to determine whether the target query statement meets the preset execution conditions based on the type of the target query statement, wherein the preset execution conditions are different for different types of target query statements;
[0144] The second determining module 203 is used to sequentially query the data in the first data table and the remaining data table when the target query statement meets the preset execution conditions, and determine the target query result of the target query statement based on the data obtained from the query. The first data table is the data table with the smallest amount of data among the multiple data tables, and the remaining data tables are the other data tables remaining among the multiple data tables besides the first data table.
[0145] Furthermore, the target query statement includes a main query statement and subqueries nested within the main query statement; the first determining module 202 includes:
[0146] The first determination submodule is used to determine whether the first data table is associated with the main query statement when the type of the target query statement is the first query type.
[0147] The second determination submodule is used to determine whether the target query statement meets the preset execution conditions when the first data table is associated with the main query statement.
[0148] Furthermore, the target query statement includes a main query statement and subqueries nested within the main query statement; the first determining module 202 also includes:
[0149] The third determination submodule is used to determine whether the first data table is associated with the main query statement or whether the first data table is associated with the subquery statement when the target query statement is of the second query type or the third query type.
[0150] The fourth determination submodule is used to determine whether the target query statement meets the preset execution conditions when the first data table is associated with the main query statement or the first data table is associated with the subquery statement.
[0151] Furthermore, when the target query statement is of the third query type, the join type of the target query statement is determined, wherein the join type includes inner join, left join, right join and full join.
[0152] When the join type is inner join, the first data table can be any data table associated with the main query or any data table associated with the subquery.
[0153] When the join type is left join and the subquery is related to the left table in the main query, the first data table is the left table in the main query.
[0154] When the join type is left join and the subquery is associated with the right table in the main query, the first data table is either the right table in the main query or any data table associated with the subquery.
[0155] When the join type is right join and the subquery is related to the right table in the main query, the first data table is the right table in the main query.
[0156] When the join type is right join and the subquery is associated with the left table in the main query, the first data table is either the left table in the main query or any data table associated with the subquery.
[0157] When the join type is full join, the first data table is any data table associated with the main query statement.
[0158] Furthermore, the second determining module 203 includes:
[0159] The first sending submodule is used to send a first data query request to the data node storing the first data table and obtain a first intermediate result, wherein the first data query request is used to request a query on the first data table;
[0160] The second sending submodule is used to send a second data query request to the data node storing the remaining data table based on the first intermediate result, and obtain the second intermediate result. The second data query request is used to request a query on the remaining data table.
[0161] The fifth determination submodule is used to determine the target query result of the target query statement based on the first intermediate result and the second intermediate result.
[0162] Furthermore, the second transmitting submodule includes:
[0163] The first sending unit is configured to send a third data query request to the data node storing the second data table based on the first intermediate result when the target query statement includes multiple subquery statements and the first data table is associated with any subquery statement, thereby obtaining a third intermediate result. The second data table is the data table in the remaining data table that is located in the same subquery statement as the first data table. The third data query request is used to request a query on the second data table.
[0164] The second sending unit is used to send a fourth data query request to the data node storing the third data table based on the third intermediate result, and obtain the second intermediate result. The third data table is a data table in the remaining data table that is located in a different subquery statement than the first data table. The fourth data query request is used to request a query on the third data table.
[0165] Furthermore, the device 200 also includes:
[0166] The output and display module is used to output and display the target query results of the target query statement.
[0167] It should be noted that the device 200 can implement the steps of any of the above data query methods and achieve the same technical effect, which will not be elaborated here.
[0168] like Figure 3 As shown, this application embodiment also provides an electronic device, including a processor 311, a communication interface 312, a memory 313 and a communication bus 314, wherein the processor 311, the communication interface 312 and the memory 313 communicate with each other through the communication bus 314.
[0169] Memory 313 is used to store computer programs;
[0170] In one embodiment of this application, when the processor 311 executes a program stored in the memory 313, it implements the data query method provided in any of the foregoing method embodiments, including:
[0171] Retrieve the target query statement, which relates to multiple data tables in the distributed database;
[0172] Based on the type of the target query statement, determine whether the target query statement meets the preset execution conditions. The preset execution conditions are different for different types of target query statements.
[0173] If the target query statement meets the preset execution conditions, the data in the first data table and the remaining data tables are queried in sequence, and the target query result of the target query statement is determined based on the data obtained. The first data table is the data table with the smallest amount of data among the multiple data tables, and the remaining data tables are the other data tables besides the first data table among the multiple data tables.
[0174] This application also provides a computer-readable storage medium storing a computer program thereon, which, when executed by a processor, implements the steps of the data query method provided in any of the foregoing method embodiments.
[0175] The device embodiments described above are merely illustrative. The units described as separate components may or may not be physically separate. The components shown as units may or may not be physical units; that is, they may be located in one place or distributed across multiple network units. Some or all of the modules can be selected to achieve the purpose of this embodiment according to actual needs.
[0176] Through the above description of the embodiments, those skilled in the art can clearly understand that each embodiment can be implemented using software plus a general-purpose hardware platform, or of course, using hardware. Based on this understanding, the above technical solutions, in essence or the parts that contribute to the related technology, can be embodied in the form of a software product. This computer software product can be stored in a computer-readable storage medium, such as ROM / RAM, magnetic disk, optical disk, etc., and includes several instructions to cause a computer device (which may be a personal computer, server, or network device, etc.) to execute the methods described in the various embodiments or some parts of the embodiments.
[0177] It should be understood that the terminology used herein is for the purpose of describing particular exemplary embodiments only and is not intended to be limiting. Unless the context clearly indicates otherwise, the singular forms “a,” “an,” and “described” as used herein may also include the plural forms. The terms “comprising,” “including,” “containing,” and “having” are inclusive and therefore indicate the presence of the stated features, steps, operations, elements, and / or components, but do not exclude the presence or addition of one or more other features, steps, operations, elements, components, and / or combinations thereof. The method steps, processes, and operations described herein are not construed as requiring them to be performed in a particular order described or illustrated unless the order of performance is explicitly indicated. It should also be understood that additional or alternative steps may be used.
[0178] The above description is merely a specific embodiment of the present invention, enabling those skilled in the art to understand or implement the invention. Various modifications to these embodiments will be readily apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the invention. Therefore, the present invention is not to be limited to the embodiments shown herein, but is to be accorded the widest scope consistent with the principles and novel features claimed herein.
Claims
1. A data query method, characterized in that, The method includes: Obtain the target query statement, wherein the target query statement is associated with multiple data tables in the distributed database, and the target query statement includes a main query statement and subquery statements nested in the main query statement; Based on the type of the target query statement, determine whether the target query statement meets the preset execution conditions, wherein the preset execution conditions are different for different types of target query statements; When the target query statement meets the preset execution conditions, the data in the first data table and the remaining data table are queried in sequence, and the target query result of the target query statement is determined based on the data obtained from the query. The first data table is the data table with the smallest amount of data among the multiple data tables, and the remaining data tables are the other data tables among the multiple data tables other than the first data table. If the target query statement does not meet the preset execution conditions, the data table is queried according to the original execution logic of the target query statement to obtain the target query result; wherein, determining whether the target query statement meets the preset execution conditions based on the type of the target query statement includes: When the type of the target query statement is a first query type, determine whether the first data table is associated with the main query statement, wherein the first query type is the query type of the subquery statement nested in the main query statement, which is a SELECTLIST subquery statement. When the first data table is associated with the main query statement, it is determined that the target query statement satisfies the preset execution conditions; When the type of the target query statement is a second query type or a third query type, determine whether the first data table is associated with the main query statement or whether the first data table is associated with the subquery statement. The second query type is the query type of the subquery statement nested in the main query statement as a WHERE subquery statement, and the third query type is the query type of the subquery statement nested in the main query statement as an ON subquery statement. When the first data table is associated with the main query statement, or when the first data table is associated with the subquery statement, it is determined that the target query statement satisfies the preset execution conditions.
2. The method according to claim 1, characterized in that, The method further includes: When the type of the target query statement is the third query type, the association connection type of the target query statement is determined, wherein the association connection type includes inner join, left join, right join and full join; When the association connection type is the inner join type, the first data table is any data table associated with the main query statement or any data table associated with the subquery statement; When the associated connection type is the left join type, and the subquery statement is associated with the left table in the main query statement, the first data table is the left table in the main query statement; When the association connection type is the left join type and the subquery statement is associated with the right table in the main query statement, the first data table is the right table in the main query statement, or any data table associated with the subquery statement; When the associated connection type is the right join type, and the subquery statement is associated with the right table in the main query statement, the first data table is the right table in the main query statement; When the associated connection type is the right join type and the subquery statement is associated with the left table in the main query statement, the first data table is the left table in the main query statement, or any data table associated with the subquery statement. When the association connection type is the full connection type, the first data table is any data table associated with the main query statement.
3. The method according to claim 1, characterized in that, The step of sequentially querying data in the first data table and the remaining data table, and determining the target query result of the target query statement based on the queried data, includes: Send a first data query request to the data node storing the first data table to obtain a first intermediate result, wherein the first data query request is used to request a query on the first data table; Based on the first intermediate result, a second data query request is sent to the data node storing the remaining data table to obtain a second intermediate result, wherein the second data query request is used to request a query on the remaining data table; Based on the first intermediate result and the second intermediate result, the target query result of the target query statement is determined.
4. The method according to claim 3, characterized in that, The step of sending a second data query request to the data node storing the remaining data table based on the first intermediate result to obtain a second intermediate result includes: When the target query statement includes multiple sub-query statements and the first data table is associated with any of the sub-query statements, a third data query request is sent to the data node storing the second data table based on the first intermediate result to obtain a third intermediate result. The second data table is the data table in the remaining data table that is located in the same sub-query statement as the first data table. The third data query request is used to request a query on the second data table. Based on the third intermediate result, a fourth data query request is sent to the data node storing the third data table to obtain the second intermediate result. The third data table is a data table in the remaining data table that is located in a different subquery statement than the first data table. The fourth data query request is used to request a query on the third data table.
5. The method according to claim 1, characterized in that, After sequentially querying data in the first data table and the remaining data table, and determining the target query result of the target query statement based on the queried data, the method further includes: Output and display the target query results of the target query statement.
6. A data query device, characterized in that, The device includes: The acquisition module is used to acquire a target query statement, wherein the target query statement is associated with multiple data tables in a distributed database, and the target query statement includes a main query statement and subquery statements nested in the main query statement; The first determining module is used to determine whether the target query statement meets the preset execution conditions based on the type of the target query statement, wherein the preset execution conditions are different for different types of target query statements; The second determining module is used to sequentially query the data in the first data table and the remaining data table when the target query statement meets the preset execution conditions, and determine the target query result of the target query statement based on the data obtained from the query. The first data table is the data table with the smallest amount of data among the multiple data tables, and the remaining data table is the other data tables among the multiple data tables besides the first data table. The data query device is further configured to query the data table according to the original execution logic of the target query statement when the target query statement does not meet the preset execution conditions, so as to obtain the target query result of the target query statement; The first determining module includes: The first determining submodule is used to determine whether the first data table is associated with the main query statement when the type of the target query statement is a first query type. The second determining submodule is used to determine whether the target query statement satisfies the preset execution conditions when the first data table is associated with the main query statement; The third determining submodule is used to determine whether the first data table is associated with the main query statement or whether the first data table is associated with the subquery statement when the type of the target query statement is the second query type or the third query type. The fourth determination submodule is used to determine whether the target query statement satisfies the preset execution conditions when the first data table is associated with the main query statement or the first data table is associated with the subquery statement.
7. An electronic device, characterized in that, It includes a processor, a communication interface, a memory, and a communication bus, wherein the processor, the communication interface, and the memory communicate with each other through the communication bus; Memory, used to store computer programs; A processor, when executing a program stored in memory, implements the steps of the data query method according to any one of claims 1-5.
8. A computer-readable storage medium having a computer program stored thereon, characterized in that, When the computer program is executed by the processor, it implements the steps of the data query method according to any one of claims 1-5.
Citation Information
Patent Citations
Data query method and device, electronic equipment and medium
CN114528317A
SQL statement optimization method and device
CN116561154A
