Database table structure changing method, device and equipment and storage medium
By migrating the data range of tables in the database in parallel using parallel resources, the problem of long table structure change time was solved, enabling rapid changes and ensuring business continuity.
Patent Information
- Application Number
- CN202211221538.6
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2022-09-30
- Publication Date
- 2026-02-10
- Estimated Expiration
- 2042-09-30
AI Technical Summary
Table structure changes take a long time to process in the database, especially when large tables are changed, which may affect peak business hours and lead to a decline in business performance.
By pre-setting P parallel resources, N data ranges from the first table in the database are migrated to the second table in parallel, enabling rapid changes to the table structure.
It improved the speed of table structure changes and ensured the normal operation of database services.
Smart Images

Figure CN117009346B_ABST
Abstract
Description
Technical Field
[0001] This application relates to the field of database technology, and in particular to a method, apparatus, device and storage medium for changing database table structure. Background Technology
[0002] A table is an object used to store data in a database. It is a structured collection of data and forms the foundation of the entire database system. Changing the table structure is a fundamental function of a database; it involves transforming a table with one structure into a table with another structure.
[0003] Table structure changes are time-consuming, especially for large tables, where they can take hours or even days. This period can overlap with peak business hours, disrupting normal operations. Therefore, improving the speed of table structure changes has always been a key research focus for database professionals. Summary of the Invention
[0004] This application provides a method, apparatus, device, and storage medium for changing database table structures, thereby improving the speed of table structure changes.
[0005] In a first aspect, embodiments of this application provide a method for modifying a database table structure, including:
[0006] In response to a table structure change instruction, a first table in the database whose table structure needs to be changed is identified. The table structure change instruction is used to indicate that the table structure of the first table be changed to the table structure of the second table.
[0007] Determine how the data in the first table is stored in the database;
[0008] Based on the storage method, N first data ranges corresponding to the first table are determined, and the data included in the N first data ranges constitute the data in the first table, where N is a positive integer greater than 1;
[0009] Using a preset P parallel resources, the data in the N first data ranges are migrated in parallel to the second table, where P is a positive integer greater than 1.
[0010] Secondly, embodiments of this application provide a database table structure modification apparatus method, comprising:
[0011] The table determination unit is used to determine the first table in the database whose table structure needs to be changed in response to a table structure change instruction. The table structure change instruction is used to indicate that the table structure of the first table is changed to the table structure of the second table.
[0012] A storage method determination unit is used to determine the storage method of the data in the first table in the database;
[0013] The processing unit is configured to determine, based on the storage method, N first data ranges corresponding to the first table, wherein the data included in the N first data ranges constitute the data in the first table, and N is a positive integer greater than 1;
[0014] The change unit is used to migrate data in the N first data ranges to the second table in parallel using a preset P parallel resources, where P is a positive integer greater than 1.
[0015] Thirdly, embodiments of this application provide an electronic device, including a processor and a memory. The memory is used to store a computer program, and the processor is used to call and run the computer program stored in the memory to perform the method described in the first aspect above.
[0016] Fourthly, embodiments of this application provide a chip for implementing the methods of any one of the first aspects or their implementations described above. Specifically, the chip includes a processor for calling and running a computer program from a memory, causing a device equipped with the chip to perform the methods as described in the first aspect.
[0017] Fifthly, embodiments of this application provide a computer-readable storage medium for storing a computer program that causes a computer to perform the method described in the first aspect above.
[0018] In a sixth aspect, embodiments of this application provide a computer program product, including computer program instructions that cause a computer to perform the method described in the first aspect above.
[0019] In a seventh aspect, embodiments of this application provide a computer program that, when run on a computer, causes the computer to perform the method described in the first aspect.
[0020] In summary, in this embodiment, the computing device, in response to a table structure change instruction, determines a first table in the database whose table structure needs to be changed. The table structure change instruction indicates that the table structure of the first table should be changed to that of a second table. The device then determines the storage method of the data in the first table within the database. Based on the storage method, it determines N first data ranges corresponding to the first table. The data included in the N first data ranges constitutes the data in the first table, where N is a positive integer greater than 1. Finally, using P preset parallel resources, the data in the N first data ranges is migrated in parallel to the second table, where P is a positive integer greater than 1. That is, this embodiment uses P parallel resources to migrate the data in the N first data ranges of the first table to the second table in parallel, achieving rapid table structure changes and ensuring the normal operation of database services. Attached Figure Description
[0021] To more clearly illustrate the technical solutions in the embodiments of the present invention, the accompanying drawings used in the description of the embodiments will be briefly introduced below. Obviously, the accompanying drawings described below are only some embodiments of the present invention. For those skilled in the art, other drawings can be obtained based on these drawings without creative effort.
[0022] Figure 1 This is a schematic diagram of the database system involved in the embodiments of this application;
[0023] Figure 2 This is a schematic diagram illustrating an application scenario according to an embodiment of this application;
[0024] Figure 3 A schematic flowchart illustrating a database table structure modification method provided in an embodiment of this application;
[0025] Figure 4 This is a schematic diagram of the TDSQL-3.0 system;
[0026] Figure 5 This is a schematic diagram illustrating the determination of a first data range according to an embodiment of this application;
[0027] Figure 6 This is a schematic flowchart of a database table structure modification method provided in an embodiment of this application;
[0028] Figure 7 This is a schematic diagram of an information exchange according to an embodiment of this application;
[0029] Figure 8 This is a schematic diagram illustrating another information exchange method involved in an embodiment of this application;
[0030] Figure 9 This is a schematic diagram of a fault detection method according to an embodiment of this application;
[0031] Figure 10 This is a schematic diagram of another fault detection method according to an embodiment of this application;
[0032] Figure 11 This is a schematic block diagram of a database table structure modification device provided in an embodiment of this application;
[0033] Figure 12 This is a schematic block diagram of a computing device provided in an embodiment of this application. Detailed Implementation
[0034] The technical solutions in the embodiments of this application will now be described with reference to the accompanying drawings.
[0035] It should be understood that, in the embodiments of the present invention, "B corresponding to A" means that B is associated with A. In one implementation, B can be determined based on A. However, it should also be understood that determining B based on A does not mean determining B solely based on A; B can also be determined based on A and / or other information.
[0036] In the description of this application, unless otherwise stated, "a plurality of" means two or more.
[0037] Furthermore, to facilitate a clear description of the technical solutions in the embodiments of this application, the terms "first" and "second" are used in the embodiments of this application to distinguish identical or similar items with substantially the same function and effect. Those skilled in the art will understand that the terms "first" and "second" do not limit the quantity or execution order, and the terms "first" and "second" are not necessarily different.
[0038] The database table structure modification method provided in this application embodiment can be applied to databases, specifically for modifying the structure of tables in the database.
[0039] To facilitate understanding of the embodiments of this application, the relevant concepts involved in the embodiments of this application will be briefly introduced as follows:
[0040] A database, simply put, can be viewed as an electronic filing cabinet, a place to store electronic files. Users can perform operations such as adding, querying, updating, and deleting data in the files. A "database" is a collection of data stored together in a certain way, capable of being shared by multiple users, with minimal redundancy, and independent of application programs.
[0041] A database system (DBS) is a system composed of a database and its management software. It is a relatively ideal data processing system developed to meet the needs of data processing, and also a software system that provides data for practical, operational storage, maintenance, and application systems. It is a collection of storage media, processing objects, and management systems. For example... Figure 1 As shown, a database system mainly includes a database, a database management system (DBMS), and a data application system. The database stores data and is managed uniformly by the DBMS; data insertion, modification, and retrieval all occur through the DBMS. The data administrator is responsible for creating, monitoring, and maintaining the entire database, ensuring that the data can be effectively used by anyone with the appropriate permissions. The database application system provides database applications, which can be understood as applications that can access the database. In practical use, an object can send read and write requests to the DBMS through an application within the database application system. The DBMS performs data reading operations on the database based on these requests. For example, based on a read request sent by the application, it reads data from the database and returns the read data to the application. Alternatively, based on a write request sent by the application, it writes data to the database. Optionally, the application can also perform other operations on the database through the DBMS; this embodiment does not limit this.
[0042] A Database Management System (DBMS) is a computer software system designed to manage databases, generally possessing basic functions such as storage, retrieval, security, and backup. DBMSs can be classified according to the database model they support, such as relational or XML (Extensible Markup Language); or according to the type of computer they support, such as server clusters or mobile devices; or according to the query language used, such as SQL (Structured Query Language) or XQuery; or according to performance priorities, such as maximum scale or maximum operating speed; or other classification methods. Regardless of the classification method used, some DBMSs can cross categories, for example, simultaneously supporting multiple query languages.
[0043] In some embodiments, the database uses cloud storage to store data. Cloud storage is a new concept that extends and develops from the concept of cloud computing. A distributed cloud storage system (hereinafter referred to as a storage system) refers to a storage system that uses cluster applications, grid technology, and distributed storage file systems to bring together a large number of storage devices (also called storage nodes) of various types in a network to work together through application software or application interfaces to provide data storage and business access functions.
[0044] Currently, the storage method of storage systems is as follows: Logical volumes are created. During the creation of a logical volume, physical storage space is allocated to each logical volume. This physical storage space may consist of a single storage device or the disks of several storage devices. Clients store data on a logical volume, which means storing the data on the file system. The file system divides the data into many parts, each part being an object. Each object contains not only the data but also additional information such as a data identifier (ID, ID entity). The file system writes each object to the physical storage space of that logical volume and records the storage location information of each object. Therefore, when a client requests access to data, the file system can allow the client to access the data based on the storage location information of each object.
[0045] The process by which a storage system allocates physical storage space to a logical volume is as follows: the physical storage space is pre-divided into strips according to the capacity estimate of the objects stored in the logical volume (this estimate often has a large margin relative to the actual capacity of the objects to be stored) and the grouping of Redundant Array of Independent Disks (RAID). A logical volume can be understood as a strip, thus allocating physical storage space to the logical volume.
[0046] A table is an object in a database used to store data. It is a structured collection of data and forms the foundation of the entire database system. Changing the table structure is a fundamental function of a database; it involves transforming a table from one structure to another.
[0047] Table structure changes are time-consuming, especially for large tables. A single table structure change can typically take hours or even days, potentially coinciding with peak business hours and disrupting normal operations. In other words, currently, changing the structure of a database table is slow and time-consuming. Furthermore, because the table is usually inaccessible during a structure change, if the business layer tries to access it during this process, it may fail or require waiting for the change to complete, causing significant delays and impacting business performance.
[0048] To address the aforementioned technical problem of improving the speed of database table structure changes while ensuring reliable access to the table, this application proposes a database table structure change method. In response to a table structure change request, a first table to be changed is determined, and the storage method of the data in the first table within the database is also determined. For example, different database types may have different data storage methods, or the same database may include different data storage methods. Next, based on the storage method of the data in the first table, N first data ranges corresponding to the first table are determined. These first data ranges can be understood as partitions of the data in the first table. Furthermore, this application pre-defines P parallel resources, which can be P parallel threads, P parallel processes, or P machines. This application does not limit the type of parallel resources, as long as they are used to implement parallel data migration. This application uses P parallel resources to migrate data in parallel from the N first data ranges corresponding to the first table to the second table, achieving parallel migration of data in the first table. In other words, this application uses P parallel resources to migrate data in parallel from the N data ranges of the first table to the second table, achieving rapid table structure changes and ensuring the normal operation of database services. Therefore, the database table structure modification method provided in this application embodiment can be applied in a variety of application scenarios, including but not limited to any one or more of the following: cloud technology, artificial intelligence, smart transportation, etc.
[0049] The above describes the relevant concepts involved in the embodiments of this application. The following describes the system architecture involved in the embodiments of this application.
[0050] Figure 2 This is a schematic diagram of an application scenario involved in an embodiment of this application, including an application client 110, a computing device 120 and a database 130, wherein the application client 110 is communicatively connected to the computing device 120 and the computing device 120 is communicatively connected to the database 130.
[0051] In the specific implementation of the embodiments of this application, the following can be adopted: Figure 2 The computing device 120 is used to execute the database table structure modification method provided in this application embodiment. The computing device 120 may include, but is not limited to, terminal devices and servers. That is, the computing device 120 may be a terminal device, a server, or a computing system composed of terminal devices and servers. This application embodiment does not limit this.
[0052] In some embodiments, the application client 110 runs on a terminal device. The application client 110 can be understood as the application client 110 corresponding to the database 130, and users can access the database 130 through the application client 110.
[0053] In the embodiments of this application, the terminal device may include, but is not limited to, smartphones, tablets, laptops, desktop computers, in-vehicle terminals, intelligent voice interaction devices, smart home appliances, aircraft, etc. In specific embodiments, the terminal device may also run various applications (APPs) and / or clients, such as multimedia playback clients, social clients, browser clients, information streaming clients, educational clients, and image processing clients, etc.
[0054] In some embodiments, the terminal device may also be configured with a display device, which may be a monitor, display screen, touch screen, etc., and the touch screen may be a touch screen, touch panel, etc.
[0055] In some embodiments, if the computing device 120 is a terminal device, the application client 110 can be directly installed on the computing device 120.
[0056] In this embodiment, the server can be one or more. If there are multiple servers, at least two servers may be used to provide different services, and / or at least two servers may be used to provide the same service, such as providing the same service through load balancing. This embodiment does not limit this. The server can be an independent physical server, a server cluster or distributed system composed of multiple physical servers, or a cloud server providing basic cloud computing services such as cloud services, cloud databases, cloud computing, cloud functions, cloud storage, network services, cloud communication, middleware services, domain name services, security services, CDN (Content Delivery Network), and big data and artificial intelligence platforms. The server can also be a node in a blockchain, etc.
[0057] It is understood that the computing device 120 used in this application embodiment for executing the database table structure change method is not specifically limited. In specific applications, the relevant devices mentioned above can be flexibly combined and used according to the actual application scenario.
[0058] This application does not limit the specific type of database 130. For example, database 130 can be a relational database (i.e., an SQL database) or a non-relational database (i.e., a Not Only SQL, NoSQL database).
[0059] The following is a general explanation of the principle of the database table structure modification method provided in the embodiments of this application, based on the above description, so that relevant personnel can more clearly understand the relevant implementation methods of the embodiments of this application.
[0060] In this embodiment, the user sends a table structure change instruction through the application client 110. After receiving the instruction, the computing device 120 determines the first table in the database 130 whose structure needs to be changed, and then determines the storage method of the data in the first table within the database 130. In this embodiment, different types of databases 130 may have different data storage methods, or the same type of database 130 may correspond to multiple data storage methods. After determining the storage method of the data in the first table within the database 130, the computing device 120 determines N first data ranges corresponding to the first table based on this storage method. These N first data ranges can be understood as being obtained by dividing the data in the first table. For example, during storage, the data in the first table is divided into blocks based on different storage methods. Thus, when retrieving data from the first table, the data is not directly read from the database 130, but rather the N first data ranges corresponding to the first table are determined based on the storage method corresponding to the first table. The data included in these N first data ranges constitute the data in the first table. In this embodiment, when the data in the first table is stored in the database 130 in different ways, the computing device 120 determines the N first data ranges corresponding to the first table in different ways. In this embodiment, P parallel resources are preset. After determining the N first data ranges corresponding to the first table, the computing device 120 uses the P parallel resources to migrate the data in these N first data ranges to the second table in parallel, thereby realizing the parallel migration of data and improving the speed of table structure changes.
[0061] It should be noted that the application scenarios of this application embodiment include, but are not limited to, those of other applications. Figure 2 As shown.
[0062] The technical solutions of the embodiments of this application will be described in detail below through some examples. The following embodiments can be combined with each other, and the same or similar concepts or processes may not be described again in some embodiments.
[0063] Figure 3 This is a flowchart illustrating a database table structure modification method according to an embodiment of this application. The method of this embodiment can be executed by a device with database table structure modification functionality, such as a database table structure modification device, which can be the one described above. Figure 2 The computing device shown is, or is Figure 2 This is a part of the computing device shown. For ease of description, the embodiments of this application are described using a computing device as an example of the execution subject.
[0064] like Figure 3 As shown, the method in this application embodiment includes:
[0065] S301. In response to a table structure change instruction, determine the first table in the database whose table structure needs to be changed.
[0066] The table structure change instruction is used to instruct the table structure of the first table to be changed to the table structure of the second table.
[0067] In this embodiment, the table structure of the first table is different from that of the second table. The second table can be empty or non-empty, and this embodiment does not restrict whether the second table already contains data. The table structure of the second table is the target table structure of the first table. For example, due to business needs or data slimming issues, it is desirable to change the table structure of the first table to the table structure of the second table. In this case, it involves a change in the table structure in the database. Specifically, the data in the first table is migrated to the second table to achieve the table structure change.
[0068] In this embodiment, after receiving a table structure change instruction sent by an application client, the computing device can, in response to the instruction, determine the first table in the database whose table structure needs to be changed. For example, the table structure change instruction includes an identifier for the first table, which can be located in a field that identifies the first table. This allows the computing device to accurately determine the first table in the database using this identifier.
[0069] This application does not limit the specific representation of the identifier in the first table.
[0070] In one example, the identifier of the first table is the name or naming of the first table. In this example, the database does not include a table with the same name as the first table, so that the computing device can identify the first table by its name or naming.
[0071] In one example, the first table is identified by its ID or number. That is, when storing tables, the database assigns a unique ID or number to each table, with different IDs or numbers for different tables. This ID or number allows the first table to be uniquely identified. Based on this, the computing device can parse the table structure change instruction to obtain the ID or number corresponding to the first table, accurately determining the first table in the database.
[0072] In some embodiments, if the database includes a second table, the table structure change instruction also includes an identifier for the second table. Before writing data from the first table to the second table, the computing device first needs to identify the second table. The computing device then locates the second table in the database based on its identifier. The computing device determines whether the free space in the second table can store the data from the first table. For example, assuming the database metadata in this embodiment includes the space resource size, used space resource size, and available space resource size for each table, where the available space resource size can be understood as the free space resource size. Based on this, the computing device can obtain the data size of the first table and the available space resource size of the second table from the database metadata. If the available space resource size of the second table is greater than or equal to the data size of the first table, it can be determined that the second table can store the data from the first table. In this case, the computing device does not recreate the second table. If the available space resources of the second table are less than the amount of data in the first table, it can be determined that the existing second table in the current database cannot completely store the data from the first table. In one example of this, the computing device expands the capacity of the second table in the current database to increase its size so that the enlarged second table can store the data from the first table. In another example of this, the computing device recreates the second table. For example, the computing device parses the table structure of the existing second table in the database and creates a new second table based on that table structure, then migrates the data from the first table to the new second table.
[0073] In some embodiments, if the database does not include a second table, the table structure change instruction also includes the table structure information of the second table. Thus, the computing device parses the table structure change instruction to obtain the table structure information of the second table, and then creates the second table in the database based on this information, migrating the data from the first table to the second table, thereby changing the table structure.
[0074] In response to the table structure change command, the computing device determines the first table in the database whose table structure needs to be changed, and then executes the following steps S302.
[0075] S302. Determine how the data in the first table is stored in the database.
[0076] In this application embodiment, different types of databases may have different data storage methods, and even databases of the same type may have different data storage methods.
[0077] In this embodiment of the application, when the table structure is changed, the data in the N first data ranges corresponding to the first table are migrated to the second table in parallel. The N first data ranges are determined based on the storage method of the data in the first table in the database. Therefore, in this embodiment of the application, it is necessary to determine the storage method of the data in the first table in the database.
[0078] In some embodiments, if the database in this application is a TDSQL database, TDSQL is a distributed database system that is compatible with MySQL, independently controllable, and highly consistent. It supports automatic horizontal partitioning, has complete business logic tables, and the data is evenly partitioned into multiple physical shards, effectively solving OLTP scenarios with ultra-high concurrency, ultra-high performance, and ultra-large capacity.
[0079] TDSQL has several versions, among which TDSQL-3.0 is a newly designed distributed database system with storage and compute separation architecture. Figure 4 This is a schematic diagram of the TDSQL-3.0 system, as shown below. Figure 4 As shown, the TDSQL system mainly consists of storage nodes, control nodes, compute nodes, and application clients from bottom to top.
[0080] The application client can be understood as the application client corresponding to the database. Users can perform database operations through the application client on the terminal device, allowing all database administrators (DBAs) to complete their operations on the user interface without having to log in to the backend.
[0081] Application clients can connect directly to compute nodes via Java Database Connectivity (JDBC) or Open Database Connectivity (ODBC), or they can connect via load balancer F5, load balancer, or Linux Virtual Server (LVS) to achieve traffic balancing.
[0082] The compute node is the computing engine of TDSQL, achieving separation between the compute and storage layers. The compute layer primarily handles distributed transaction-related coordination tasks, such as distributed optimization, execution of specific distributed plans, distributed transaction control, storage node load balancing, and user authentication and authorization. Furthermore, TDSQL compute nodes also possess online analytical processing (OLAP) capabilities, enabling algorithmic optimization for complex computations.
[0083] The management node is divided into cluster management and operation and maintenance management platforms. The cluster management part includes a metadata server, a cluster manager, and a proxy manager. This part manages and maintains the entire cluster metadata, data nodes, and compute nodes under the distributed database framework.
[0084] Storage nodes are used to store data. For example, TDSQL has two storage models: a no-shard database and a distributed database (also called a shared version of TDSQL). The no-shard model is a single-machine version of TDSQL. The second model is a distributed database with horizontal scalability. In TDSQL-3.0, storage nodes use a distributed architecture to store data.
[0085] In one data storage method of TDSQL, data is stored in storage nodes according to logical ranges, which are called ranges. For example, the data in a table is divided into multiple ranges and stored in storage nodes.
[0086] In other words, in TDSQL, the data in the first table is divided into multiple data ranges and stored in the storage nodes.
[0087] In some embodiments, if the database of this application is a database organized based on a B+ tree, non-leaf nodes in the B+ tree do not store data but only serve as indexes; only leaf nodes store data. For example, the root node includes at least two child nodes, and each node has at most m child nodes. All leaf nodes are at the same level, and all leaf nodes are connected to form a doubly linked list, with the leaf node keys sorted in ascending order. Pointers and key-value pairs are stored in nodes other than leaf nodes in the B+ tree. Leaf nodes store both key-value pairs and data.
[0088] In other words, in a database that organizes data based on a B+ tree, the data in the first table is stored in the leaf nodes.
[0089] In some embodiments, the database uses other methods to store data, and the specific storage method of the database in this application embodiment is not limited.
[0090] In this embodiment, the computing device can determine the storage method of the data in the first table in the database from the database metadata. For example, the computing device obtains the database type from the database metadata, and then determines the storage method of the data in the first table in the database based on the database type. For example, assuming the database type in this embodiment is... Figure 4 In the TDSQL-3.0 system database shown, it can be determined that the data in the first table is divided into multiple data ranges, that is, divided into multiple ranges and stored in the storage nodes. Assuming that the database in this embodiment is a MySQL database, it can be determined that the data in the first table is stored in the leaf nodes of the B+ tree.
[0091] After determining the storage method of the data in the first table in the database based on the above method, the computing device executes the following step S303.
[0092] S303. Based on the storage method, determine the N first data ranges corresponding to the first table.
[0093] The data in the first table consists of N data ranges, where N is a positive integer greater than 1.
[0094] In this embodiment of the application, the data in the first table is composed of the data in the N first data ranges. That is, the data in the first table is obtained by concatenating and combining the data in the N first data ranges.
[0095] In this embodiment, the storage method of the data in the first table in the database varies, thus the computing device determines the N first data ranges corresponding to the first table in different ways. In other words, in this embodiment, the N first data ranges corresponding to the first table are determined based on the storage method of the data in the first table in the database.
[0096] In some embodiments, when the data in the first table is stored in the database, a primary key is set for the first table. The primary key is one or more columns in the table whose values uniquely identify each row in the table. The primary key enforces the entity integrity of the table. A primary key is created by defining a primary key constraint when the table is created or modified. A table can only have one primary key constraint, and columns in the primary key constraint cannot accept null values. Because the primary key constraint ensures unique data, it is often used to define identity columns. Therefore, the primary key can uniquely identify the first table. In this case, the above S303 includes the following steps S303-A and S303-B:
[0097] S303-A, Determine the primary key corresponding to the first table;
[0098] S303-B: Based on the storage method and primary key, determine the N first data ranges corresponding to the first table.
[0099] In this embodiment, the computing device receives a table structure modification instruction and, based on the instruction, determines the first table in the database whose table structure needs to be modified, for example, determining the identifier or name of the first table. The database metadata in this embodiment includes primary keys corresponding to different tables. Thus, the computing device can query the primary key corresponding to the first table in the database metadata based on the identifier or name of the first table.
[0100] After determining the primary key corresponding to the first table, the computing device determines N first data ranges corresponding to the first table based on the way the data in the first table is stored in the database and the primary key. For example, based on the way the data in the first table is stored in the database, the device queries the database for the data range, data entries, or leaf nodes corresponding to the primary key, and then constructs the N data ranges corresponding to the first table based on the data range, data entries, or leaf nodes corresponding to the primary key.
[0101] The specific implementation process of S303-B above will be described below in an exemplary form. However, in the embodiments of this application, the implementation methods of S303-B above include, but are not limited to, the following.
[0102] Method 1: If the storage method involves dividing the data in the first table into multiple data ranges for storage, then the above S303-B includes the following steps S303-B-11 and S303-B-12:
[0103] S303-B-11. Obtain N second data ranges, including the primary key, from the storage nodes of the database;
[0104] S303-B-12. Based on the N second data ranges, determine the N first data ranges.
[0105] In this first method, the database in this application embodiment is Figure 4 In the TDSQL-3.0 system database shown, the data in the first table is stored in the database by dividing the data in the first table into multiple data ranges, that is, into multiple ranges, and storing them in storage nodes.
[0106] For example, the first table contains 10 data items from 1 to 10. In the storage node, these 10 data items may be divided into multiple data ranges according to logical order. Suppose they are divided into 2 data ranges, namely [1,5) and [5,10].
[0107] In one example, each piece of data in this database is organized as a key-value (KV) structure. The approach to mapping table structures to KV data is to assign a primary key to each table's data. This primary key serves as a prefix for the key, meaning all data in the first table shares this prefix, allowing data belonging to that first table to be stored contiguously. For example, the first table might have the structure (a INT PRIMARY KEY, b INT). Here, a INT PRIMARY KEY represents the primary key column of the first table, and b INT represents the data column. Assuming the primary key is assigned an id = 10001, during storage, this id will serve as a common prefix for all data in the first table, assembling the KV data entries to be stored using this prefix.
[0108] For example, in the first table, a data entry with a prefix can be represented as: Key[id + value of a + reserved information (denoted as ...)]Value[value of b]. Here, id can be understood as the primary key value of the first table. Assuming the first table contains data (1,1), (2,2), and (3,3), the three data entries in the first table are represented as {[100011…][1]}, {[100012…][2]}, and {[100013…][3]}, respectively. In the storage of the storage node, since the three data entries in the first table share the same prefix 10001, they can be physically stored in a contiguous area and logically divided into multiple ranges.
[0109] As described above, in this first method, each piece of data in the first table is assembled into a data entry, such as a key-value (KV) structure data entry. A prefix corresponding to the first table is added to each data entry, and these prefixed data entries are logically divided into multiple data ranges, i.e., multiple ranges, stored in the storage node. Different ranges are logically independent. Thus, during a table structure change operation in the first table, the computing device can query the database storage node for N second data ranges, including the primary key corresponding to the first table.
[0110] In some embodiments, a data range (i.e., range) includes only the data of one table. That is, each of the N second data ranges determined above includes only the data of the first table and does not include the data other than the data in the first table. Then, the N second data ranges obtained above are determined as the N first data ranges corresponding to the first table.
[0111] In some embodiments, if at least one of the N second data ranges includes data other than that in the first table, then data entries other than those including the primary key are removed from the data entries included in the N second data ranges to obtain N first data ranges.
[0112] In this embodiment of the application, when the storage node performs range division, it does not know or need to know which data belongs to the same table. Therefore, the N second data ranges determined in S303-B-11 may carry data belonging to other tables.
[0113] For example, assuming the ID (i.e., key value) of the primary key corresponding to the first table is 100001, the computing device scans the storage nodes of the database for all data ranges including the prefix 10001. Assume three second data ranges are found, denoted as Second Data Range 1 (Range1), Second Data Range 2 (Range2), and Second Data Range 3 (Range3). For example, these three ranges are shown below:
[0114] Range1->[100001,100011]
[0115] [100001…]
[0116] [100002…]
[0117] [100010…]
[0118] [100011…]
[0119] Range2->[100012,100014]
[0120] [100012…]
[0121] [100013…]
[0122] [100014…]
[0123] Range3->[100015,100021]
[0124] [100015…]
[0125] [100016…]
[0126] [100020…]
[0127] [100021…]
[0128] The second data range 1 (Range1) includes data from the first table as well as data from the table with primary key 10000. The second data range 2 (Range2) includes only data from the first table. The second data range 3 (Range3) includes data from the first table as well as data from the table with primary key 10002. Clearly, data not belonging to the first table cannot be written to the new second table; therefore, this data needs to be removed.
[0129] Specifically, the computing node removes data from the three second data ranges and places it into other tables, including only the data in the first table. That is, it removes all data entries except the primary key of the first table from the data entries included in the three second data ranges, thus obtaining N first data ranges corresponding to the first table.
[0130] For example, by finding the intersection of these intersections, data not from the first table can be removed from the three second data ranges mentioned above. For instance, Figure 5 As shown, find the intersection of each second data range with the corresponding primary key id (i.e., 10001) in the first table, such as... Figure 5 As shown, the part where the second data range 1 intersects with the primary key of the first table is determined as the first data range 1 corresponding to the second data range 1. The entire second data range 2 is determined as the first data range 2 corresponding to the second data range 2. The part where the second data range 3 intersects with the primary key of the first table is determined as the first data range 3 corresponding to the second data range 3, thus obtaining 3 first data ranges.
[0131] The first method described above, which involves dividing the data in the first table into multiple data ranges for storage in the database, is used to determine the N first data ranges corresponding to the first table. In some embodiments, the computing device can also use the second method to determine the N first data ranges corresponding to the first table.
[0132] Method 2: If the storage method involves storing the data from the first table in the leaf nodes of the tree, then S303-B above includes the following steps:
[0133] S303-B-21. From the database, retrieve the M leaf nodes corresponding to the primary key, where M is a positive integer greater than 1;
[0134] S303-B-22. Merge the data included in the M leaf nodes to obtain N first data ranges.
[0135] In this second method, assuming the database in this embodiment is a database organized based on a B+ tree, the data in the first table is stored in the leaf nodes of the B+ tree. Thus, after determining the primary key corresponding to the first table based on the above steps, the computing device queries the B+ tree based on that primary key, and then retrieves the M leaf nodes corresponding to that primary key from the database.
[0136] In some embodiments, at least one of the M leaf nodes includes only one piece of data from the first table.
[0137] In some embodiments, at least one of the M leaf nodes includes multiple data from the first table.
[0138] Since the leaf nodes store relatively little data, the computing device retrieves the M leaf nodes corresponding to the primary key from the database, merges the data contained in these M leaf nodes, and obtains N first data ranges.
[0139] In one example, the data contained in a predetermined number of leaf nodes out of M leaf nodes are merged into a first data range, resulting in N first data ranges. In this example, if M is an integer multiple of the predetermined number, and the data contained in each of the M leaf nodes is of the same size, then the data contained in each of the N determined first data ranges will also be of the same size. In this example, if at least one leaf node among the M leaf nodes contains a different number of data, then at least one of the N determined first data ranges may contain data of a different size than the other first data ranges.
[0140] In another example, the data contained in the M leaf nodes are merged to form N first data ranges, each containing the same amount of data. For instance, if the data contained in one of the M leaf nodes is large, the data in that leaf node can be treated as a single first data range without merging it with the data in other leaf nodes. Conversely, if the data contained in one of the M leaf nodes is small, the data in those leaf nodes can be merged into a single first data range. In this example, since each of the N first data ranges contains the same amount of data, load balancing can be achieved when the data in these N first data ranges is migrated in parallel.
[0141] The above-described methods one and two describe the process of determining the N first data ranges corresponding to the first table in the embodiments of this application. It should be noted that, in addition to methods one and two described above, other methods can also be used to determine the N first data ranges corresponding to the first table in the embodiments of this application. For example, the first table is stored in the database as a complete table, so the first table can be divided into N first data ranges.
[0142] After determining the N first data ranges corresponding to the first table based on the above method, the computing device executes the following step S304.
[0143] S304. Using P preset parallel resources, migrate the data in the first data range to the second table in parallel.
[0144] Where P is a positive integer greater than 1.
[0145] In this embodiment of the application, the computing device determines N first data ranges corresponding to the first table using the above method. Then, the computing device uses P preset parallel resources to migrate the data in the N first data ranges to the second table in parallel.
[0146] This application embodiment does not limit the specific method by which the computing device uses preset P parallel resources to migrate data from N first data ranges to the second table in parallel.
[0147] In some embodiments, if P is greater than N, that is, the number N of the first data ranges corresponding to the first table is greater than the preset number P of parallel resources, and if it is desired to complete the execution of N first data ranges at once, then the above S304 includes the following steps S304-A1 and S304-A2:
[0148] S304-A1: Merge N first data ranges into P third data ranges, where the P third data ranges do not overlap.
[0149] S304-A2: Allocate each of the P third data ranges to one of the P parallel resources, so that the P parallel resources can migrate the data in the P third data ranges to the second table in parallel.
[0150] In this embodiment, the computing device merges N first data ranges to obtain P third data ranges, such that the number P of third data ranges is the same as the number P of parallel resources. Then, the P third data ranges can be allocated one by one to the P parallel resources, so that one parallel resource processes the data in one third data range. This allows data from the first table to be migrated to the second table in parallel at once, improving the speed of table structure changes.
[0151] This application does not limit the specific method of merging N first data ranges into P third data ranges in the embodiments.
[0152] In one possible implementation, at least two logically adjacent first data ranges among the N first data ranges are merged to obtain P third data ranges.
[0153] For example, the left and right intervals of two first data ranges can be integrated, such as merging the first data range [1,3] and the first data range [4,5], to obtain the merged third data range [1,5].
[0154] For example, suppose N=3, and the three first data ranges are respectively Figure 5 The table contains three data ranges: first data range 1, first data range 2, and first data range 3. Assuming P = 2, these three first data ranges can be merged into two third data ranges. Specifically, first data range 1 and first data range 2 can be merged into one third data range, and first data range 3 can be kept as a separate third data range. Alternatively, second data range 2 and first data range 3 can be merged into one third data range, and first data range 1 can be kept as a separate third data range. It is important to note that first data range 1 and first data range 3 cannot be merged, because the resulting third data range includes first data range 2. Therefore, merging first data range 1 and first data range 3 would result in data from first data range 2 being written repeatedly into the second table.
[0155] In another possible implementation, S304-A1 above includes the following steps S304-A11 and S304-A12:
[0156] S304-A11, Sort N first data ranges;
[0157] S304-A12. Merge at least two adjacent first data ranges among the N sorted first data ranges to obtain P third data ranges.
[0158] In this implementation, the computing device sorts the N first data ranges corresponding to the first table, for example, by the size of the intervals, in ascending or descending order. For instance, if the N first data ranges are sorted in ascending order, the value of the left interval of the next first data range is not less than the value of the right interval of the previous first data range. Similarly, if the N first data ranges are sorted in descending order, the value of the left interval of the next first data range is not greater than the value of the right interval of the previous first data range.
[0159] After sorting the N first data ranges, the computing device merges at least two adjacent first data ranges among the sorted N first data ranges to obtain P third data ranges with non-overlapping intervals.
[0160] This application does not limit the specific method for merging the sorted N first data ranges in the embodiments.
[0161] In one example, at least two adjacent first data ranges from the sorted N first data ranges are merged to obtain P third data ranges.
[0162] For example, if N=10 and P=2, then any two adjacent first data ranges among the 10 sorted first data ranges will be merged to obtain two third data ranges. For example, the first 8 first data ranges among the 10 sorted first data ranges will be merged into one third data range, and the last 2 first data ranges will be merged into one third data range.
[0163] In one possible implementation of this example, the sorted N first data ranges are merged based on the data volume to obtain P third data ranges. For example, adjacent first data ranges with smaller data volumes are merged, while first data ranges with larger data volumes are not merged. This ensures that each of the generated P third data ranges contains the same or nearly the same amount of data, thereby achieving load balancing.
[0164] In one possible implementation of this example, the sorted N first data ranges can be merged based on the performance of the parallel resources. For example, if P=2, meaning that among the two parallel resources, parallel resource 1 has weaker performance and parallel resource 2 has stronger performance, then the third data range obtained by merging the larger number of sorted N first data ranges can be allocated to parallel resource 2, while the third data range obtained by merging the smaller number of first data ranges or the unmerged first data range can be allocated to parallel resource 1.
[0165] In another example, the computing device determines a first value based on the number of first data ranges and the number of parallel resources; then, from the sorted N first data ranges, each first value first data range is merged into a third data range, resulting in P third data ranges.
[0166] In this example, the same number of first data ranges (i.e., the first numerical value) from the sorted N first data ranges are merged into a third data range.
[0167] The embodiments of this application do not limit the specific method of determining the first value based on the number of first data ranges and the number of parallel resources.
[0168] For example, the first value is determined by rounding down the ratio of the number of data ranges to the number of parallel resources.
[0169] For example, the first value is determined based on the following formula:
[0170] M = Floor(N / P) (1)
[0171] Where M is the first value, and Floor is the floor value.
[0172] For example, suppose N=10 and P=3. Assume the ratio of the number of first data ranges (10) to the number of parallel resources (3), rounded down, is 3, meaning the first value is 3. Then, the computing device merges every three first data ranges from the sorted ten into a third data range. The remaining first data range is then merged into the last third data range, resulting in three third data ranges.
[0173] For example, the first value is determined by rounding up the ratio of the number of data ranges to the number of parallel resources.
[0174] For example, the first value is determined based on the following formula:
[0175] M = ceil(N / P) (2)
[0176] Where M is the first value, and ceil is the rounding up.
[0177] For example, suppose N=10 and P=3. Suppose the ratio of the number of first data ranges (10) to the number of parallel resources (3), rounded up, is 4, meaning the first value is 4. Then, the computing device merges every four first data ranges from the sorted ten into a third data range, and finally merges the remaining two first data ranges into another third data range, resulting in three third data ranges.
[0178] The above embodiment describes the process of merging N first data ranges into P third data ranges when N is greater than P, so that P parallel resources can migrate the data in the P third data ranges to the second table in parallel.
[0179] In some embodiments, if N is greater than P, parallel migration can also be performed in the following manner, i.e., the above S304 includes the following steps:
[0180] S304-B1: Obtain P first data ranges from N first data ranges;
[0181] S304-B2: Allocate P first data ranges to P parallel resources one by one, so that the P parallel resources can migrate the data in the P first data ranges to the second table in parallel.
[0182] S304-B3: When an idle parallel resource is detected among P parallel resources, the remaining first data range among N first data ranges is allocated to the idle parallel resource so that the idle parallel resource can migrate the data in the remaining first data range to the second table. The remaining first data range is the first data range among N first data ranges excluding P first data ranges.
[0183] This implementation avoids excessive processing, such as sorting or merging, and instead uses each first data range as a parallel unit. Specifically, P first data ranges are first selected from N first data ranges, while the others wait. These P first data ranges are then allocated to P parallel resources, allowing them to migrate data from the P first data ranges to the second table in parallel. When a parallel resource is found to be idle (e.g., if the data volume of first data range 1 is small), it is used up and returned. The remaining first data ranges are then allocated to this idle resource, allowing it to migrate data from those ranges to the second table. This approach minimizes resource waste by releasing parallel resources after the smaller first data range has used them, thus improving the efficiency of table structure changes.
[0184] The above describes the specific process by which the computing device, using P preset parallel resources, migrates data from the first data range to the second table in parallel when N is greater than P.
[0185] In some embodiments, if N is less than P, the computing device can obtain N parallel resources from P parallel resources and allocate the N first data ranges to the N parallel resources one by one, so that the N parallel resources can migrate the data in the N first data ranges to the second table in parallel. For example, when selecting parallel resources, the selection can be based on the amount of data in the first data range. For instance, if the amount of data in the first data range is large, a parallel resource with higher performance is selected; if the amount of data in the first data range is small, a parallel resource with lower performance is selected. This allows the N parallel resources to synchronously migrate the data in the N first data ranges to the second table, avoiding the problem that poor-performing parallel resources spend a lot of time migrating data in the first data range with a large amount of data, thus affecting the efficiency of the entire table structure change.
[0186] The database table structure modification method provided in this application embodiment involves a computing device responding to a table structure modification instruction. First, it determines a first table in the database whose table structure is to be modified. The table structure modification instruction instructs the modification of the first table's structure to that of a second table. Then, it determines the storage method of the data in the first table within the database. Based on the storage method, it determines N first data ranges corresponding to the first table. The data included in the N first data ranges constitutes the data in the first table, where N is a positive integer greater than 1. Finally, using P preset parallel resources, it migrates the data in the N first data ranges to the second table in parallel, where P is a positive integer greater than 1. In other words, this application embodiment uses P parallel resources to migrate the data in the N first data ranges of the first table to the second table in parallel, achieving rapid table structure modification and ensuring the normal operation of database services.
[0187] The above embodiments describe the database table structure modification method provided by this application from an overall perspective. The following description uses a TDSQL database as an example, where the data in the first table is stored in the database by dividing the data in the first table into multiple data ranges.
[0188] Figure 6 This is a schematic diagram of a database table structure modification method provided in an embodiment of this application. Figure 6 The embodiments shown can be understood as described above. Figure 3 The illustrated embodiment is a specific implementation. The execution subject of this embodiment is a computing device.
[0189] like Figure 6 As shown, the method in this application embodiment includes:
[0190] S401, The client sends a table structure change command to the computing device.
[0191] This table structure change instruction is used to instruct the table structure of the first table to be changed to the table structure of the second table.
[0192] S402. In response to the table structure change command, the computing device determines the first table in the database whose table structure needs to be changed.
[0193] S403. The computing device determines the primary key corresponding to the first table.
[0194] The specific descriptions of S401 to S403 above can be found in the descriptions of the above embodiments, and will not be repeated here.
[0195] S404. The computing device obtains N second data ranges, including the primary key, from the storage node of the database.
[0196] S405. The computing device determines N first data ranges based on N second data ranges.
[0197] For example, if each of the N second data ranges does not include data other than the data in the first table, then the N second data ranges are determined as N first data ranges.
[0198] For example, if at least one of the N second data ranges includes data other than that in the first table, then from the data entries included in the N second data ranges, remove the data entries other than the data entries including the primary key to obtain the N first data ranges.
[0199] S406. Determine whether N is greater than P.
[0200] If P is greater than N, that is, the number of parallel resources P is greater than the number of the first data range N, then execute step S407 or S408.
[0201] If N is less than or equal to P, then perform the following steps S409.
[0202] S407. The computing device merges N first data ranges into P third data ranges, and allocates the P third data ranges to P parallel resources one by one, so that the P parallel resources can migrate the data in the P third data ranges to the second table in parallel.
[0203] The specific description of S407 above can be found in the description of the above embodiments, and will not be repeated here.
[0204] S408. The computing device obtains P first data ranges from N first data ranges; allocates the P first data ranges one by one to P parallel resources, so that the P parallel resources migrate the data in the P first data ranges to the second table in parallel; when it is detected that there are idle parallel resources among the P parallel resources, the remaining first data ranges from the N first data ranges are allocated to the idle parallel resources, so that the idle parallel resources migrate the data in the remaining first data ranges to the second table.
[0205] Among them, the remaining first data range is the first data range other than P first data ranges out of N first data ranges.
[0206] S409. The computing device obtains N parallel resources from P parallel resources; it allocates N first data ranges to the N parallel resources one by one, so that the N parallel resources can migrate the data in the N first data ranges to the second table in parallel.
[0207] The specific description of S409 above can be found in the description of the above embodiments, and will not be repeated here.
[0208] In this embodiment of the application, if the data in the first table is stored in the database by dividing the data in the first table into multiple data ranges, that is, when the database in this embodiment of the application is a TDSQL database, by determining the N first data ranges corresponding to the first table, and based on the number of P parallel resources and the number of N first data ranges, the method of parallel migration of the data in the first data ranges by the parallel resources is determined, thereby ensuring that the data in the N first data ranges corresponding to the first table are quickly migrated to the second table in parallel, and realizing the rapid change of the table structure.
[0209] In some embodiments, the table structure change operation can also be canceled during the table structure change process.
[0210] In one example, the computing device receives a first request instructing the cancellation of a table structure change operation on a first table; in response to the first request, it controls P parallel resources to stop migrating data from the first table to a second table. For example, Figure 7 As shown, users can operate on parallel resources through this application client. For example, if a user sends a first request through the application client during the table structure change process of the first table, the first request is used to request the cancellation of the table structure change operation of the first table. In response to the first request, the computing device controls P parallel resources to stop migrating data from the first table to the second table. Optionally, the above application client can be installed on the computing device or on other terminal devices.
[0211] In another example, this application can configure a signal processing device for each of the P parallel resources. This signal processing device can be hardware, software, or a hybrid hardware-software module. The client can communicate directly with this signal processing device without going through a computing device. In this case, such as... Figure 8 As shown, the user sends a first request to the signal processing device in the parallel resources through the client. Based on the first request, the signal processing device stops migrating data from the first table to the second table.
[0212] In some embodiments, fault detection can also be performed.
[0213] In one example, when the computing device detects a failure in the first of P parallel resources, it sends a fault message to the client indicating that the first parallel resource has failed. For example, Figure 9 As shown, the computing device monitors parallel resources in real time. When a parallel resource failure is detected, such as a system failure or data failure, the device reports the failure information to the application client. The user or scheduler then decides whether to ignore the error or send a signal to all parallel resources to cancel the changed task, depending on the error type.
[0214] In another example, this application can configure an error handling device for each of the P parallel resources. This error handling device can be hardware, software, or a hybrid hardware-software module. The client can communicate directly with this error handling device without going through a computing device. In this case, as... Figure 10 As shown, each parallel resource may face various failures while processing its own task, including but not limited to system failures and data failures. Therefore, parallel resources need to rely on error handling mechanisms for error processing. For example, if parallel resource 1 encounters a system failure, the error handling mechanism will capture the failure and report it to the application client. For instance, the user or scheduler decides, based on the error type, whether to ignore the error or send a signal to all parallel resources to cancel the task change via a signal processing device.
[0215] In some embodiments, if the user or scheduler does not receive any error reports and does not actively send a signal to cancel the task, then the parallel change is completed once all parallel resources have finished their tasks, and the user or scheduler records the parallel change as successful. However, if any parallel resource fails to complete its task due to various exceptions, the data becomes incomplete, and all parallel resources cease operation. Alternatively, if system factors such as power outages prevent the scheduler from recording the change as successful, the entire table structure change fails, and the system rolls the data back to its state before the change.
[0216] It should be understood that Figures 3 to 10 This is merely an example of what is being done and should not be construed as limiting the scope of this application.
[0217] The preferred embodiments of this application have been described in detail above with reference to the accompanying drawings. However, this application is not limited to the specific details of the above embodiments. Within the scope of the technical concept of this application, various simple modifications can be made to the technical solutions of this application, and these simple modifications all fall within the protection scope of this application. For example, the various specific technical features described in the above specific embodiments can be combined in any suitable manner without contradiction. To avoid unnecessary repetition, this application will not describe the various possible combinations separately. Furthermore, various different embodiments of this application can also be arbitrarily combined, as long as they do not violate the spirit of this application, they should also be considered as the content disclosed in this application.
[0218] The above text combined Figures 3 to 10 The method embodiments of this application are described in detail below, in conjunction with... Figures 11 to 12 The following describes in detail the device embodiments of this application.
[0219] Figure 11This is a schematic block diagram of a database table structure modification device provided in an embodiment of this application. The device 10 can be the aforementioned computing device or part of a computing device.
[0220] like Figure 11 As shown, the database table structure modification device 10 includes:
[0221] Table determination unit 11 is used to determine the first table in the database whose table structure is to be changed in response to a table structure change instruction. The table structure change instruction is used to indicate that the table structure of the first table is changed to the table structure of the second table.
[0222] Storage method determination unit 12 is used to determine the storage method of the data in the first table in the database;
[0223] Processing unit 13 is configured to determine N first data ranges corresponding to the first table based on the storage method, wherein the data included in the N first data ranges constitute the data in the first table, and N is a positive integer greater than 1;
[0224] The modification unit 14 is used to migrate data in the N first data ranges to the second table in parallel using a preset P parallel resources, where P is a positive integer greater than 1.
[0225] In some embodiments, the processing unit 13 is specifically used to determine the primary key corresponding to the first table; and to determine the N first data ranges corresponding to the first table based on the storage method and the primary key.
[0226] In some embodiments, the processing unit 13 is specifically configured to, if the storage method is to divide the data in the first table into multiple data ranges for storage, obtain N second data ranges including the primary key from the storage nodes of the database; and determine the N first data ranges based on the N second data ranges.
[0227] In some embodiments, the processing unit 13 is specifically configured to determine the N second data ranges as the N first data ranges if each of the N second data ranges does not include data other than the data in the first table.
[0228] In some embodiments, the processing unit 13 is specifically configured to, if at least one of the N second data ranges includes data other than that in the first table, remove data entries other than those including the primary key from the data entries included in the N second data ranges to obtain the N first data ranges.
[0229] In some embodiments, the processing unit 13 is specifically configured to, if the storage method is to store the data in the first table in the leaf nodes of the tree, obtain M leaf nodes corresponding to the primary key from the database, where M is a positive integer greater than 1; merge the data included in the M leaf nodes to obtain the N first data ranges.
[0230] In some embodiments, the modification unit 14 is specifically configured to, if N is greater than P, merge the N first data ranges into P third data ranges, wherein the P third data ranges do not overlap; and allocate the P third data ranges one by one to the P parallel resources, so that the P parallel resources can migrate the data in the P third data ranges to the second table in parallel.
[0231] In some embodiments, the changing unit 14 is specifically used to sort the N first data ranges; and to merge at least two adjacent first data ranges among the sorted N first data ranges to obtain the P third data ranges.
[0232] In some embodiments, the changing unit 14 is specifically used to determine a first value based on the number of the first data ranges and the number of the parallel resources; and to merge each of the first value first data ranges in the sorted N first data ranges into a third data range to obtain the P third data ranges.
[0233] In some embodiments, the changing unit 14 is specifically used to determine the first value as the value obtained by rounding up the ratio of the number of parallel resources to the number of the first data range.
[0234] In some embodiments, the modification unit 14 is specifically configured to: if N is greater than P, obtain P first data ranges from the N first data ranges; allocate the P first data ranges one by one to the P parallel resources, so that the P parallel resources migrate the data in the P first data ranges to the second table in parallel; when an idle parallel resource is detected among the P parallel resources, allocate the remaining first data ranges among the N first data ranges to the idle parallel resource, so that the idle parallel resource migrates the data in the remaining first data ranges to the second table, wherein the remaining first data ranges are the first data ranges among the N first data ranges excluding the P first data ranges.
[0235] In some embodiments, the change unit 14 is specifically configured to: if N is less than or equal to P, obtain N parallel resources from the P parallel resources; and allocate the N first data ranges one by one to the N parallel resources, so that the N parallel resources can migrate the data in the N first data ranges to the second table in parallel.
[0236] In some embodiments, the processing unit 13 is further configured to receive a first request, the first request being used to instruct the cancellation of the table structure change operation of the first table; and in response to the first request, to control the P parallel resources to stop migrating data from the first table to the second table.
[0237] In some embodiments, the processing unit 13 is further configured to send fault information to the client when a fault is detected in the first parallel resource among the P parallel resources, the fault information being used to indicate the fault in the first parallel resource.
[0238] It should be understood that the device embodiments and method embodiments can correspond to each other, and similar descriptions can be referred to the method embodiments. To avoid repetition, further details will not be provided here. Specifically, Figure 11 The apparatus shown can execute an embodiment of the database table structure modification method, and the foregoing and other operations and / or functions of each module in the apparatus are for implementing the above method embodiment, which will not be described in detail here for the sake of brevity.
[0239] The apparatus of this application embodiment has been described above from the perspective of functional modules in conjunction with the accompanying drawings. It should be understood that this functional module can be implemented in hardware, in software instructions, or in a combination of hardware and software modules. Specifically, the steps of the method embodiments in this application can be completed by integrated logic circuits in the processor's hardware and / or by software instructions. The steps of the method disclosed in this application embodiment can be directly embodied as being executed by a hardware decoding processor, or by a combination of hardware and software modules in the decoding processor. Optionally, the software module can reside in a mature storage medium in the art, such as random access memory, flash memory, read-only memory, programmable read-only memory, electrically erasable programmable memory, registers, etc. This storage medium is located in memory, and the processor reads information from the memory and, in conjunction with its hardware, completes the steps in the above method embodiments.
[0240] Figure 12 This is a schematic block diagram of a computing device provided in an embodiment of this application, which is used to execute the above-described method embodiments.
[0241] like Figure 12 As shown, the computing device 30 may include:
[0242] The system includes a memory 31 and a processor 32. The memory 31 stores a computer program 33 and transfers the program code 33 to the processor 32. In other words, the processor 32 can retrieve and run the computer program 33 from the memory 31 to implement the methods described in the embodiments of this application.
[0243] For example, the processor 32 can be used to execute the above method steps according to the instructions in the computer program 33.
[0244] In some embodiments of this application, the processor 32 may include, but is not limited to:
[0245] General-purpose processors, digital signal processors (DSPs), application-specific integrated circuits (ASICs), field-programmable gate arrays (FPGAs), or other programmable logic devices, discrete gate or transistor logic devices, discrete hardware components, etc.
[0246] In some embodiments of this application, the memory 31 includes, but is not limited to:
[0247] Volatile memory and / or non-volatile memory. Non-volatile memory can be read-only memory (ROM), programmable read-only memory (PROM), erasable programmable read-only memory (EPROM), electrically erasable programmable read-only memory (EEPROM), or flash memory. Volatile memory can be random access memory (RAM), used as an external cache. By way of example, but not limitation, many forms of RAM are available, such as Static RAM (SRAM), Dynamic RAM (DRAM), Synchronous DRAM (SDRAM), Double Data Rate SDRAM (DDR SDRAM), Enhanced Synchronous DRAM (ESDRAM), Synchronous Link DRAM (SLDRAM), and Direct Rambus RAM (DR RAM).
[0248] In some embodiments of this application, the computer program 33 may be divided into one or more modules, which are stored in the memory 31 and executed by the processor 32 to complete the page recording method provided in this application. The one or more modules may be a series of computer program instruction segments capable of performing specific functions, which describe the execution process of the computer program 33 in the computing device.
[0249] like Figure 12 As shown, the computing device 30 may further include:
[0250] Transceiver 34, which can be connected to processor 32 or memory 31.
[0251] The processor 32 can control the transceiver 34 to communicate with other devices; specifically, it can send information or data to other devices or receive information or data sent by other devices. The transceiver 34 may include a transmitter and a receiver. The transceiver 34 may further include antennas, and the number of antennas may be one or more.
[0252] It should be understood that the various components in the computing device 30 are connected through a bus system, which includes a data bus, a power bus, a control bus, and a status signal bus.
[0253] According to one aspect of this application, a computer storage medium is provided that stores a computer program thereon, which, when executed by a computer, enables the computer to perform the methods of the above-described method embodiments.
[0254] This application also provides a computer program product containing instructions that, when executed by a computer, cause the computer to perform the method described in the above method embodiments.
[0255] According to another aspect of this application, a computer program product or computer program is provided, comprising computer instructions stored in a computer-readable storage medium. A processor of an electronic device reads the computer instructions from the computer-readable storage medium and executes the computer instructions, causing the electronic device to perform the method described in the above-described method embodiments.
[0256] In other words, when implemented using software, it can be implemented wholly or partially in the form of a computer program product. This computer program product includes one or more computer instructions. When these computer program instructions are loaded and executed on a computer, all or part of the processes or functions described in the embodiments of this application are generated. The computer can be a general-purpose computer, a special-purpose computer, a computer network, or other programmable device. The computer instructions can be stored in a computer-readable storage medium or transmitted from one computer-readable storage medium to another. For example, the computer instructions can be transmitted from one website, computer, server, or data center to another via wired (e.g., coaxial cable, fiber optic, digital subscriber line (DSL)) or wireless (e.g., infrared, wireless, microwave, etc.) means. The computer-readable storage medium can be any available medium that a computer can access or a data storage device such as a server or data center that integrates one or more available media. The available medium can be a magnetic medium (e.g., floppy disk, hard disk, magnetic tape), an optical medium (e.g., digital video disc (DVD)), or a semiconductor medium (e.g., solid-state disk (SSD)).
[0257] Those skilled in the art will recognize that the modules and algorithm steps of the various examples described in conjunction with the embodiments disclosed herein can be implemented in electronic hardware, or a combination of computer software and electronic hardware. Whether these functions are implemented in hardware or software depends on the specific application and design constraints of the technical solution. Those skilled in the art can use different methods to implement the described functions for each specific application, but such implementation should not be considered beyond the scope of this application.
[0258] In the several embodiments provided in this application, it should be understood that the disclosed systems, apparatuses, and methods can be implemented in other ways. For example, the apparatus embodiments described above are merely illustrative; for instance, the division of modules is only a logical functional division, and in actual implementation, there may be other division methods. For example, multiple modules or components may be combined or integrated into another system, or some features may be ignored or not executed. Furthermore, the coupling or direct coupling or communication connection shown or discussed may be through some interfaces; the indirect coupling or communication connection between apparatuses or modules may be electrical, mechanical, or other forms.
[0259] The modules described as separate components may or may not be physically separate. The components shown as modules may or may not be physical modules; 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. For example, the functional modules in the various embodiments of this application may be integrated into one processing module, or each module may exist physically separately, or two or more modules may be integrated into one module.
[0260] The above description is merely a specific embodiment of this application, but the scope of protection of this application is not limited thereto. Any variations or substitutions that can be easily conceived by those skilled in the art within the scope of the technology disclosed in this application should be included within the scope of protection of this application. Therefore, the scope of protection of this application should be determined by the scope of the claims.
Claims
1. A method for modifying a database table structure, characterized in that, include: In response to a table structure change instruction, a first table in the database whose table structure needs to be changed is identified. The table structure change instruction is used to indicate that the table structure of the first table be changed to the table structure of the second table. Determine the storage method of the data in the first table in the database. The storage method includes dividing the data in the first table into multiple data ranges for storage, or storing the data in the first table in the leaf nodes of a tree. Based on the storage method, N first data ranges corresponding to the first table are determined, and the data included in the N first data ranges constitute the data in the first table, where N is a positive integer greater than 1; Using a preset P parallel resources, the data in the N first data ranges are migrated in parallel to the second table, where P is a positive integer greater than 1; The step of migrating data from the N first data ranges to the second table in parallel using P preset parallel resources includes: if N is greater than P, merging the N first data ranges into P third data ranges, and allocating the P third data ranges one by one to the P parallel resources, so that the P parallel resources can migrate data from the P third data ranges to the second table in parallel, wherein the P third data ranges do not overlap.
2. The method according to claim 1, characterized in that, The step of determining the N first data ranges corresponding to the first table based on the storage method includes: Determine the primary key corresponding to the first table; Based on the storage method and the primary key, the N first data ranges corresponding to the first table are determined.
3. The method according to claim 2, characterized in that, The step of determining the N first data ranges corresponding to the first table based on the storage method and the primary key includes: If the storage method is to divide the data in the first table into multiple data ranges for storage, then N second data ranges including the primary key are obtained from the storage nodes of the database. Based on the N second data ranges, the N first data ranges are determined.
4. The method according to claim 3, characterized in that, The step of determining the N first data ranges based on the N second data ranges includes: If each of the N second data ranges does not include data other than the data in the first table, then the N second data ranges are determined as the N first data ranges.
5. The method according to claim 3, characterized in that, The step of determining the N first data ranges based on the N second data ranges includes: If at least one of the N second data ranges includes data other than that in the first table, then from the data entries included in the N second data ranges, all data entries except those including the primary key are removed to obtain the N first data ranges.
6. The method according to claim 2, characterized in that, The step of determining the N first data ranges corresponding to the first table based on the storage method and the primary key includes: If the storage method is to store the data in the first table in the leaf nodes of the tree, then retrieve the M leaf nodes corresponding to the primary key from the database, where M is a positive integer greater than 1; The data included in the M leaf nodes are merged to obtain the N first data ranges.
7. The method according to claim 1, characterized in that, The step of merging the N first data ranges into P third data ranges includes: Sort the N first data ranges; The N sorted first data ranges are merged into at least two adjacent first data ranges to obtain the P third data ranges.
8. The method according to claim 7, characterized in that, The step of merging at least two adjacent first data ranges from the sorted N first data ranges to obtain the P third data ranges includes: A first value is determined based on the number of data ranges and the number of parallel resources. Each of the sorted N first data ranges is merged into a third data range, resulting in the P third data ranges.
9. The method according to claim 8, characterized in that, Determining the first value based on the number of the first data ranges and the number of the parallel resources includes: The first value is determined by rounding up the ratio of the number of parallel resources to the number of data ranges.
10. The method according to any one of claims 1-6, characterized in that, If N is greater than P, then the step of migrating the data in the N first data ranges to the second table in parallel using P preset parallel resources includes: Obtain P first data ranges from the N first data ranges; The P first data ranges are allocated one by one to the P parallel resources, so that the P parallel resources can migrate the data in the P first data ranges to the second table in parallel. When an idle parallel resource is detected among the P parallel resources, the remaining first data range among the N first data ranges is allocated to the idle parallel resource, so that the idle parallel resource migrates the data in the remaining first data range to the second table. The remaining first data range is the first data range among the N first data ranges excluding the P first data ranges.
11. The method according to any one of claims 1-6, characterized in that, If N is less than or equal to P, then the step of migrating the data in the N first data ranges to the second table in parallel using P preset parallel resources includes: Obtain N parallel resources from the P parallel resources; The N first data ranges are assigned one by one to the N parallel resources, so that the N parallel resources can migrate the data in the N first data ranges to the second table in parallel.
12. A database table structure modification device, characterized in that, include: The table determination unit is used to determine the first table in the database whose table structure needs to be changed in response to a table structure change instruction. The table structure change instruction is used to indicate that the table structure of the first table is changed to the table structure of the second table. A storage method determination unit is used to determine the storage method of the data in the first table in the database. The storage method includes dividing the data in the first table into multiple data ranges for storage, or storing the data in the first table in the leaf nodes of a tree. The processing unit is configured to determine, based on the storage method, N first data ranges corresponding to the first table, wherein the data included in the N first data ranges constitute the data in the first table, and N is a positive integer greater than 1; The modification unit is used to migrate data in the N first data ranges to the second table in parallel using a preset P parallel resources, where P is a positive integer greater than 1; Specifically, the change unit is used to merge the N first data ranges into P third data ranges if N is greater than P, and to allocate the P third data ranges one by one to the P parallel resources, so that the P parallel resources can migrate the data in the P third data ranges to the second table in parallel, and the P third data ranges do not overlap with each other.
13. An electronic device, characterized in that, Including processor and memory; The memory is used to store computer programs; The processor is configured to execute the computer program to implement the method as described in any one of claims 1 to 11.
14. A computer-readable storage medium, characterized in that, Used to store computer programs that cause a computer to perform the method as described in any one of claims 1 to 11.
Citation Information
Patent Citations
Database capacity changing method and device
CN110851427A
Data migration method, system and equipment and medium
CN111291023A