A method for batch inserting data into distributed database
By using parameterized SQL statements and U messages for data shard routing in a distributed database, the inefficiency and routing efficiency problems of the traditional data batch insertion method when processing large amounts of data are solved, and efficient data insertion and system are achieved. Scalability.
Patent Information
- Application Number
- CN202410755952.8
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-06-13
- Publication Date
- 2024-10-11
- Estimated Expiration
- 2044-06-13
AI Technical Summary
The traditional data batch insertion method has high parsing overhead, low data routing efficiency, and limited parallel processing potential in scenarios where large amounts of data are processed or fine routing is required to specific data nodes.
By sending P messages of parameterized SQL statements, query trees are generated and cached, U messages are used for data fragmentation and routing calculations, data are sent in batches to target nodes for insertion, network round-trips and repeated SQL parsing are reduced, and data insertion efficiency is improved.
Significantly improves the efficiency of data batch insertion and system response speed, reduces CPU and memory consumption, supports larger data volumes and higher concurrent processing capabilities, and enhances the scalability of the system.
Smart Images

Figure CN118332010B_ABST
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of databases, and in particular relates to a method for inserting batch data into a distributed database. Background Art
[0002] In database management systems, batch data insertion is implemented through INSERT statements, which allows multiple data to be inserted into a table at the same time to improve operational efficiency. The traditional INSERT INTO table_name (column_name_list) VALUES (data1), (data2), (data3); statement achieves efficiency improvement by reducing the number of database calls. Distributed database systems such as openGauss further optimize this process. Through the improved PBE (Parse, Bind, Execute) process and U message mechanism, multiple data are sent at one time, reducing network round trips and repeated SQL parsing. However, this method still shows limitations in scenarios where large amounts of data are processed or fine routing to specific data nodes is required, such as high parsing overhead, low data routing efficiency, and limited parallel processing potential. Summary of the invention
[0003] In view of this, the present invention aims to propose a method for batch data insertion in a distributed database to optimize the U message processing logic and significantly improve the efficiency of batch data insertion and the system response speed.
[0004] To achieve the above object, the technical solution of the present invention is achieved as follows:
[0005] A method for batch inserting data into a distributed database.
[0006] Further, the following steps are included:
[0007] T1. Send P message: The client constructs and sends a P message containing a parameterized SQL statement to the database. After receiving the P message, the database parses the SQL statement and generates a corresponding query tree, which is cached locally.
[0008] T2. Send U message: The client sends a U message to the database. The database uses the query tree to parse the data in the U message, uses the data's shard key field and hash function to determine the target data node, and performs routing calculation.
[0009] T3. Batch data insertion: The database will pack the data group of the same data node into a new U message according to the hash value, and send this U message in batches to the corresponding data node for data insertion;
[0010] T4, end insertion: traverse all determined data nodes in T2 to confirm that data insertion is completed, and complete the data insertion process.
[0011] Furthermore, in T2, sending the U message further includes the following sub-steps:
[0012] T21, the client sends a U message to the database, the U message includes the number of data to be inserted and the data list;
[0013] T22. The database parses the actual data from the U message and uses the same hash function to process the sharding key of the data in the same table;
[0014] T23. Batch calculate the hash value of the data and obtain the data node number corresponding to each piece of data.
[0015] Further, the U message includes a P message and a B message, and the U message generation step includes:
[0016] S1. The client sends a B message to the database. The B message includes the SQL statement name and a piece of data to be inserted. The database binds the B message to the P message in T1.
[0017] S2: The client sends message E to the database, and the database executes the SQL statement of message B in S1 to insert a piece of data.
[0018] S3, return to S1 to insert the next piece of data.
[0019] Furthermore, in the T1, the P message includes a parameterized SQL statement to be executed, and the parameterized SQL statement is used to identify the SQL statement;
[0020] In the T2, the U message also includes the name of the parameterized SQL statement in the P message.
[0021] Furthermore, the step T3 comprises the following sub-steps:
[0022] T31, after receiving the U message, the database center node parses all the data and constructs a data set based on it;
[0023] T32. The database selects a suitable hash function according to the distribution column type of any data in the data set, and calculates the hash value for each data to ensure that each data is evenly distributed to each data node;
[0024] T33, initialize a new U message for the target data node of each data, traverse the data set according to the target data node of each data, and put the data into the corresponding new U message to complete the data allocation;
[0025] T34. The central node sends each new U message to the corresponding data node, and the data node performs data insertion according to the new U message.
[0026] Furthermore, an electronic device includes a processor and a memory that is communicatively connected to the processor and is used to store instructions executable by the processor, and the processor is used to execute the above-mentioned method for batch insertion of data for a distributed database.
[0027] Furthermore, a server includes at least one processor and a memory communicatively connected to the processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the processor so that the at least one processor executes the method for batch insertion of data for a distributed database.
[0028] Furthermore, a computer-readable storage medium stores a computer program, and when the computer program is executed by a processor, the method for batch insertion of data for a distributed database is implemented.
[0029] Compared with the prior art, the method for batch inserting data into a distributed database according to the present invention has the following beneficial effects:
[0030] (1) The data batch insertion method described in the present invention reduces the overhead of inserting a single piece of data by batch processing data and messages, thereby improving the overall data insertion efficiency, and significantly reduces the database load when processing a large amount of data.
[0031] (2) The data batch insertion method of the present invention reduces the repetitive work of SQL parsing in the database by using parameterized SQL statements and query tree cache, thereby reducing CPU and memory consumption and optimizing resource utilization;
[0032] (3) The data batch insertion method described in the present invention allows the distributed database to expand its processing capacity by adding data nodes. Through effective data sharding and routing, data can be evenly distributed on each node, thereby improving the scalability of the system and supporting larger data volumes and higher concurrent processing capabilities. BRIEF DESCRIPTION OF THE DRAWINGS
[0033] The accompanying drawings constituting a part of the present invention are used to provide a further understanding of the present invention. The exemplary embodiments of the present invention and their descriptions are used to explain the present invention and do not constitute an improper limitation of the present invention. In the accompanying drawings:
[0034] Figure 1 This is a flow chart of a method for inserting data in batches according to an embodiment of the present invention;
[0035] Figure 2 It is a schematic diagram of the principle of batch data insertion (existing technology) according to an embodiment of the present invention;
[0036] Figure 3 A schematic diagram of the principle of batch data insertion according to an embodiment of the present invention (this application);
[0037] Figure 4 This is a schematic diagram of a flow chart of sending a U message according to an embodiment of the present invention. DETAILED DESCRIPTION
[0038] It should be noted that, in the absence of conflict, the embodiments of the present invention and the features in the embodiments may be combined with each other.
[0039] The present invention will be described in detail below with reference to the accompanying drawings and in conjunction with embodiments.
[0040] like Figure 2 As shown, the existing data insertion process uses the PBE (parse, bind, execute) method to insert one piece of data at a time, specifically through one 'P' message, multiple 'B' and 'E' messages to complete the data insertion, where the P message is a parameterized SQL statement, the B message contains an actual piece of data to be inserted, and the E message is used to trigger the execution of the data insertion action to complete the data insertion. When inserting a large amount of data, this method has a low insertion efficiency due to the large number of message interactions. Therefore, this application combines multiple 'B' and 'E' messages into a 'U' message, and implements a large amount of time insertion through a U message, such as Figure 3 shown.
[0041] Specifically, a method for batch inserting data into a distributed database, such as Figure 1 The following steps are shown:
[0042] T1. Send P message: First, the client constructs and sends a P message containing a parameterized SQL statement to the database, for example: Insert into table_name(column_name_list) values ($1,$2,...,$n); then, the database receives the P message, parses its SQL statement, and generates a corresponding query tree, which is cached locally;
[0043] T2. Send U message: The client sends a U message to the database. The database parses the data in the U message through the query tree to determine the destination data node for each piece of data and performs routing calculation based on the data's shard key field and hash function.
[0044] T3. Batch data insertion: The database will package the data group of the same data node into a new U message according to the hash value, for example:
[0045] DN1 batch inserts messages: {1, 1, 1}{2, 2, 2};
[0046] DN2 batch inserts messages: {3, 3, 3};
[0047] Then, this new U message is sent in batches to the corresponding data nodes for data insertion;
[0048] T4, end insertion: traverse all determined data nodes in T2 to confirm that data insertion has been completed, completing the data insertion process.
[0049] Specifically, the U message in T2 includes a P message and a B message, and the steps of generating the U message include:
[0050] S1. The client sends a B message to the database. The B message includes the SQL statement name and a piece of data to be inserted. The database binds the B message to the P message in T1.
[0051] S2: The client sends message E to the database, and the database executes the SQL statement of message B in S1 to insert a piece of data.
[0052] S3, return to S1 to insert the next piece of data.
[0053] The advantage is that, for example, inserting data (1, 1, 1), (2, 2, 2), (3, 3, 3):
[0054] P{'sql_name', sql statement};
[0055] INSERT(1, 1, 1);
[0056] B{'sql_name',{(1,1,1)}}+E;
[0057] INSERT(2, 2, 2);
[0058] B{'sql_name',{(2,2,2)}}+E;
[0059] INSERT(3,3,3);
[0060] B{'sql_name', {(3, 3, 3)}}+E.
[0061] Insert multiple data through U message. Multiple data are in one U message. One message is used between the client and the database to implement batch insertion of multiple data. For example, insert (1, 1, 1), (2, 2, 2), (3, 3, 3):
[0062] U{'sql_name',3,{(1,1,1),(2,2,2),(3,3,3)}}.
[0063] Further Figure 4 As shown, in T2, sending the U message also includes the following sub-steps:
[0064] T21. The client sends a U message to the database. The U message includes the number of data to be inserted and the data list, for example:
[0065] {;
[0066] 3.
[0067] {1, 1, 1};
[0068] {2, 2, 2};
[0069] {3, 3, 3};
[0070] };
[0071] T22. The database parses the actual data from the U message. Since the data is stored in the same table, their sharding keys (one or more fields used to determine which data node the data should be stored on) are of the same type. The same hash function can be used to process the sharding keys of the data in the same table. The advantage is that the hash function used for the sharding key is determined before the data insertion operation is processed, so that the hash value of each data is calculated quickly and accurately, and the data is routed to the correct data node accordingly.
[0072] T23. For each piece of data to be inserted, the hash value of the data is calculated in batches, and the data node number corresponding to each piece of data is obtained.
[0073] Specifically, in T22, the shard key type of the data to be inserted is first determined, and then the hash value of all data is calculated in batches according to the shard key type.
[0074] Furthermore, T3 includes the following sub-steps:
[0075] T31, after receiving the U message, the database center node parses all the data and constructs a data set based on it;
[0076] T32. The database selects a suitable hash function according to the distribution column type of any data in the data set, and calculates the hash value for each data to ensure that each data is evenly distributed to each data node;
[0077] T33, initialize a new U message for the target data node of each data, traverse the data set according to the target data node of each data, and put the data into the corresponding new U message to complete the data allocation;
[0078] T34. The central node sends each new U message to the corresponding data node, and the data node performs data insertion according to the new U message.
[0079] For example, the original 'U' message:
[0080] U{'sql_name',3,{(1,1,1),(2,2,2),(3,3,3)}}.
[0081] New 'U' Message:
[0082] DN1 'U' message: {'sql_name', 2, {(1, 1, 1), (2, 2, 2)}};
[0083] DN2 'U' message: {'sql_name', 1, {(3, 3, 3)}}.
[0084] Specifically, in T1, the P message includes a parameterized SQL statement to be executed, and the parameterized SQL statement is used to identify the SQL statement; in T2, the U message also includes the name of the parameterized SQL statement in the P message.
[0085] Those of ordinary skill in the art will appreciate that the units and method steps of each example described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, computer software, or a combination of the two. In order to clearly illustrate the interchangeability of hardware and software, the composition and steps of each example have been generally described in terms of function in the above description. Whether these functions are performed in hardware or software depends on the specific application and design constraints of the technical solution. Professional and technical personnel can use different methods to implement the described functions for each specific application, but such implementation should not be considered to be beyond the scope of the present invention.
[0086] In the several embodiments provided in the present application, it should be understood that the disclosed methods and systems can be implemented in other ways. For example, the division of the units described above is only a logical function division, and there may be other division methods in actual implementation, such as multiple units or components can be combined or integrated into another system, or some features can be ignored or not executed. The above-mentioned units may or may not be physically separated, and the components displayed as units may or may not be physical units, that is, they may be located in one place, or they may be distributed on multiple network units. Some or all of the units may be selected according to actual needs to achieve the purpose of the embodiments of the present invention.
[0087] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention, rather than to limit it. Although the present invention has been described in detail with reference to the aforementioned embodiments, those skilled in the art should understand that they can still modify the technical solutions described in the aforementioned embodiments, or replace some or all of the technical features therein by equivalents. These modifications or replacements do not make the essence of the corresponding technical solutions deviate from the scope of the technical solutions of the embodiments of the present invention, and they should all be included in the scope of the claims and specification of the present invention.
[0088] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions, improvements, etc. made within the spirit and principle of the present invention should be included in the protection scope of the present invention.
Claims
1. A method for batch inserting data into a distributed database, characterized in that: The following steps are involved: T1. Send P message: The client constructs and sends a P message containing a parameterized SQL statement to the database. After receiving the P message, the database parses the SQL statement and generates a corresponding query tree, which is cached locally. T2. Send U message: The client sends a U message to the database. The database uses the query tree to parse the data in the U message, uses the data's shard key field and hash function to determine the target data node, and performs routing calculation. T3. Batch data insertion: The database will pack the data group of the same data node into a new U message according to the hash value, and send this U message in batches to the corresponding data nodes for data insertion; T4, end insertion: traverse all the determined data nodes in T2 to confirm that data insertion is completed, and complete the data insertion process; In T2, sending the U message further includes the following sub-steps: T21, the client sends a U message to the database, which includes the number of data to be inserted and the data list; T22. The database parses the actual data from the U message and uses the same hash function to process the sharding key of the data in the same table; T23, batch calculate the hash value of the data and obtain the data node number corresponding to each piece of data; The U message includes a P message and a B message, and the U message generation step includes: S1. The client sends a B message to the database. The B message includes the SQL statement name and a piece of data to be inserted. The database binds the B message to the P message in T1. S2: The client sends message E to the database, and the database executes the SQL statement of message B in S1 to insert a piece of data. S3, return to S1 to insert the next piece of data; The T3 comprises the following sub-steps: T31, after receiving the U message, the database center node parses all the data and constructs a data set based on it; T32. The database selects a suitable hash function according to the distribution column type of any data in the data set, and calculates the hash value for each data to ensure that each data is evenly distributed to each data node; T33, initialize a new U message for the target data node of each data, traverse the data set according to the target data node of each data, and put the data into the corresponding new U message to complete the data allocation; T34. The central node sends each new U message to the corresponding data node, and the data node performs data insertion according to the new U message.
2. A method for inserting data in batches into a distributed database according to claim 1, characterized in that: In the T1, the P message includes a parameterized SQL statement to be executed, and the parameterized SQL statement is used to identify the SQL statement; In the T2, the U message also includes the name of the parameterized SQL statement in the P message.
3. An electronic device, comprising a processor and a memory connected to the processor for storing instructions executable by the processor, characterized in that: The processor is used to execute a method for batch inserting data for a distributed database as described in any one of claims 1-2 above.
4. A server, characterized in that: It includes at least one processor and a memory communicatively connected to the processor, wherein the memory stores instructions executable by the at least one processor, and the instructions are executed by the processor so that the at least one processor executes a method for batch insertion of data for a distributed database as described in any one of claims 1-2.
5. A computer-readable storage medium storing a computer program, characterized in that: When the computer program is executed by a processor, the method for batch inserting data for a distributed database as described in any one of claims 1 to 2 is implemented.
