A data access method, device, equipment and computer readable storage medium
By recording the memory addresses of children's nodes in the B+Tree data structure and locking only the parent node, the performance bottleneck problem in the hash table management memory is solved, and more efficient memory access and database throughput are achieved.
Patent Information
- Application Number
- CN202011049612.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2020-09-29
- Publication Date
- 2025-03-25
- Estimated Expiration
- 2040-09-29
AI Technical Summary
In the high concurrency scenario, the management of memory through hash tables leads to performance bottlenecks, affecting database throughput and system performance.
By recording the storage address of the data page corresponding to the child node in the memory in the B+Tree data structure of the database, only locking is added on the parent node, thereby realizing lock-free on the data page granularity, avoiding performance problems caused by locking by global hash tables.
In high concurrency scenarios, the granularity of locked data is reduced, memory access is avoided to become a performance bottleneck, and database throughput and computer system performance are improved.
Smart Images

Figure CN114328500B_ABST
Abstract
Description
Technical Field
[0001] The present disclosure relates to the field of information technology, and in particular to a data access method, device, equipment and computer-readable storage medium. Background Art
[0002] With the continuous development of information technology, people generate a large amount of data in their work and life, and usually store these large amounts of data in a database. Among them, the frequently accessed data in the database can be stored in the memory.
[0003] The prior art uses a hash table to manage memory, which is mainly reflected in the insertion, deletion, and search of data in the memory. Generally, before inserting, deleting, searching, and other access processes are performed on the data in the memory, the hash table needs to be locked.
[0004] When a thread in a computer system adds a mutex lock to a hash table, other threads will be unable to access the data in the memory, resulting in reduced performance of the computer system. Even if the hash table is split into multiple parts and mutex locks are added to parts of the hash table, the problem of low performance of the computer system cannot be fundamentally solved. Summary of the invention
[0005] In order to solve the above technical problems or at least partially solve the above technical problems, the present disclosure provides a data access method, apparatus, device and computer-readable storage medium, which avoid memory access becoming a performance bottleneck in high concurrency scenarios, ensure the throughput of the database, and effectively improve the performance of the computer system.
[0006] In a first aspect, an embodiment of the present disclosure provides a data access method, including:
[0007] Obtaining a data access request, wherein the data access request includes identification information of target data to be accessed;
[0008] According to the identification information of the target data to be accessed, traverse the data structure corresponding to the database to determine a first node and a second node in the data structure, wherein the first node is a child node of the second node, and the first node includes the identification information of the target data;
[0009] If the second node includes the storage address of the first data page corresponding to the first node in the memory, the second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released.
[0010] In a second aspect, an embodiment of the present disclosure provides a data access device, including:
[0011] An acquisition module, used to acquire a data access request, wherein the data access request includes identification information of target data to be accessed;
[0012] A traversal module, used to traverse the data structure corresponding to the database according to the identification information of the target data to be accessed;
[0013] A determination module, configured to determine a first node and a second node in the data structure, wherein the first node is a child node of the second node, and the first node includes identification information of the target data;
[0014] An access module is used to lock the second node if the second node includes the storage address of the first data page corresponding to the first node in the memory, and release the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory.
[0015] In a third aspect, an embodiment of the present disclosure provides a data access device, including:
[0016] Memory;
[0017] Processor; and
[0018] Computer programs;
[0019] The computer program is stored in the memory and is configured to be executed by the processor to implement the method as described in the first aspect.
[0020] In a fourth aspect, an embodiment of the present disclosure provides a computer-readable storage medium having a computer program stored thereon, wherein the computer program is executed by a processor to implement the method described in the first aspect.
[0021] The data access method, apparatus, device and computer-readable storage medium provided by the embodiments of the present disclosure traverse the data structure corresponding to the database through the identification information of the target data to be accessed, and determine the first node and the second node with a parent-child relationship from the data structure, wherein the first node is the child node of the second node, and the first node includes the identification information of the target data. Since the data structure is not only an organizational form of data in the database, but also used to manage memory, that is to say, in the present embodiment, there is no need to specially set up a hash table to manage the memory. In addition, in the present embodiment, when the data page corresponding to the child node has been cached in the memory, the storage address of the data page corresponding to the child node in the memory can be recorded in the parent node of the child node. In this way, when a thread needs to access the data page corresponding to a child node in the memory, it only needs to lock the parent node of the child node. Since the number of child nodes under a parent node is limited, locking a parent node only prevents other threads from accessing the data pages corresponding to the limited number of child nodes in the memory, and will not cause other threads to be unable to access all or most of the data pages in the memory, thereby achieving lock-free at the memory granularity. Since only the lock is added at the granularity of the data page, the granularity of the locked data is greatly reduced, thereby preventing memory access from becoming a performance bottleneck in high concurrency scenarios, ensuring the throughput of the database, and effectively improving the performance of the computer system. BRIEF DESCRIPTION OF THE DRAWINGS
[0022] The accompanying drawings, which are incorporated in and constitute a part of this specification, illustrate embodiments consistent with the present disclosure and, together with the description, serve to explain the principles of the present disclosure.
[0023] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, for ordinary technicians in this field, other drawings can be obtained based on these drawings without paying any creative labor.
[0024] Figure 1 A schematic diagram of a data structure in the prior art;
[0025] Figure 2 A schematic diagram of the structure of a hash table provided in an embodiment of the present disclosure;
[0026] Figure 3 A flow chart of a data access method provided by an embodiment of the present disclosure;
[0027] Figure 4 A schematic diagram of a data structure provided in an embodiment of the present disclosure;
[0028] Figure 5A schematic diagram of the structure of a data access device provided by an embodiment of the present disclosure;
[0029] Figure 6 A schematic diagram of the structure of a data access device provided in an embodiment of the present disclosure. DETAILED DESCRIPTION
[0030] In order to more clearly understand the above-mentioned objectives, features and advantages of the present disclosure, the scheme of the present disclosure will be further described below. It should be noted that the embodiments of the present disclosure and the features in the embodiments can be combined with each other without conflict.
[0031] In the following description, many specific details are set forth to facilitate a full understanding of the present disclosure, but the present disclosure may also be implemented in other ways different from those described herein; it is obvious that the embodiments in the specification are only part of the embodiments of the present disclosure, rather than all of the embodiments.
[0032] Generally, a large amount of data generated by people in work and life can be stored in a database. There are many types of databases. This embodiment takes a relational database (MySQL) as an example. The relational database stores data in different tables instead of putting all data in a large warehouse. That is to say, the relational database includes multiple tables. In addition, the data in each table is organized by a certain data structure. The data structure can be a tree-like data structure. This embodiment uses the data structure of B+Tree as an example for schematic illustration. A table in the relational database corresponds to a B+Tree data structure. Among them, a table in the relational database can also be called a file. It can be understood that in other embodiments, the data structure is not limited to B+Tree, for example, it can also be a binary search tree, a balanced binary tree (AVLTree), a balanced multi-way search tree (B-Tree), etc. Among them, B+Tree is an optimization based on B-Tree. Among them, B+Tree can also be called a balanced multi-way search tree, that is, a balanced multi-way search tree includes two structures, B-Tree and B+Tree.
[0033] Normally, the database is stored in the external storage device of the computer, which can be referred to as external memory. The external storage device can be, for example, a disk, a hard disk, etc. Relative to the external storage device of the computer, the internal storage device of the computer can be referred to as memory, which can be, for example, the memory inside the computer. The memory is used to store the intermediate data and calculation results of the central processing unit (CPU). In addition, the data in the database that is frequently accessed can also be stored in the memory. When the computer system reads data from the disk to the memory, it uses the disk block as the basic unit, and the data located in the same disk block will be read out at one time.
[0034] like Figure 1 FIG. 1 is a schematic diagram of a B+Tree data structure in the prior art. Figure 1 As shown, the B+Tree data structure includes a root node, non-leaf nodes, and leaf nodes. Each node occupies a disk block of disk space. Specifically, node 1 is the root node, node 2 and node 3 are non-leaf nodes, and node 4-node 9 are leaf nodes. Node 1 occupies disk block 1, node 2 occupies disk block 2, and so on. Node 9 occupies disk block 9.
[0035] In addition, a table in a relational database may include multiple records, each record may occupy a row of the table, so a record may also be called a row of records. Specifically, each record may be recorded as a tuple [key, data]. Among them, key represents the key value of the record, that is, the primary key value of the record in the corresponding table. Data represents the data in a record except the primary key value. For different records, the key value is different. For example, Figure 1 As shown, the leaf node includes a primary key value and data. Other nodes other than the leaf node, for example, the root node and the non-leaf node include a primary key value and a pointer, and the pointer can specifically be the address of the disk block occupied by the child node. The address of the disk block can specifically be the address of the disk block in the disk.
[0036] For example, Figure 1 Taking the root node shown as an example, node 2 and node 3 are child nodes of the root node respectively. The root node includes pointers p2 and p3, where p2 represents the address of the disk block occupied by node 2 and p3 represents the address of the disk block occupied by node 3.
[0037] In addition, if Figure 1 As shown, for each node, the number of primary key values in the node may not be specifically limited. However, the primary key values in each node are sorted in ascending order. Taking node 2 as an example for schematic illustration, the two primary key values in node 2 are 10 and 17, the primary key value of the child node pointed to by pointer p4 is less than 10, the primary key value of the child node pointed to by pointer p5 is between 10-17, and the primary key value of the child node pointed to by pointer p6 is greater than or equal to 17.
[0038] Let's take the search for the primary key value 29 as an example. Figure 1 Introduce the process of traversing B+Tree:
[0039] 1. Find disk block 1 based on the root node and read the content of disk block 1 into memory, that is, perform the first disk input / output (I / O) operation.
[0040] 2. Compare the primary key value 29 with the primary key value 28 in the root node. Since the primary key value 29 is greater than the primary key value 28, select the pointer p3 to the right of the primary key value 28.
[0041] 3. Find disk block 3 according to pointer p3 and read the content of disk block 3 into memory, that is, perform the second disk I / O operation.
[0042] 4. Compare the primary key value 29 and the interval (36,79) to find the pointer p7 in disk block 3.
[0043] 6. Find disk block 7 according to pointer p7 and read the content of disk block 7 into memory, that is, perform the third disk I / O operation.
[0044] 7. Find keyword 29 in disk block 7.
[0045] Normally, data that is frequently accessed in a database can be stored in memory. In the disclosed embodiment, the memory can cache data in units of data pages. For example, a data page in the memory can cache all the contents of a node in a B+Tree. In some other embodiments, a data page can also cache the contents of multiple nodes with consecutive addresses in a B+Tree. That is to say, in the present embodiment, the contents of some nodes of the B+Tree can be cached in the memory in the form of data pages. A node in the B+Tree can correspond to a data page in the memory. These data pages cached in the memory can be data pages that are being accessed, or they can be hot data pages, i.e., data pages that are accessed more frequently. In addition, in the disclosed embodiment, the memory can specifically be a memory pool (BufferPool).
[0046] Currently, the memory is managed by using a hash table. For example, the memory cache is as follows Figure 1The data page corresponding to node 1, the data page corresponding to node 3, and the data page corresponding to node 7 are shown. The identifier of node 1 can be recorded as spaceid+pageno1, where spaceid represents the identifier of the file corresponding to the B+Tree, for example, the file name. Pageno1 represents the offset of node 1 in the file. Since node 1, node 3, and node 7 are in the same B+Tree, the file names corresponding to node 1, node 3, and node 7 are the same. The identifier of node 3 can be recorded as spaceid+pageno3, and the identifier of node 7 can be recorded as spaceid+pageno7. It can be understood that the identifier of each node can be the address of the disk block occupied by the node. When constructing the hash table, the hash values of spaceid+pageno1, spaceid+pageno3, and spaceid+pageno7 are calculated respectively. For example, the hash value of spaceid+pageno1 is recorded as hash value 1, spaceid+pageno3 is recorded as hash value 3, and the hash value of spaceid+pageno7 is recorded as hash value 7. In the hash table, each hash value corresponds to a position in the hash table, and each position in the hash table is used to store the storage address of the data page corresponding to a node in the memory. Figure 2 As shown, the location corresponding to hash value 1 stores the storage address of the data page corresponding to node 1 in the memory, the location corresponding to hash value 3 stores the storage address of the data page corresponding to node 3 in the memory, and the location corresponding to hash value 7 stores the storage address of the data page corresponding to node 7 in the memory.
[0047] In the data access process, the B+Tree is traversed according to the primary key value of the target data to be accessed. The traversal process is as described above and will not be repeated here. For example, the primary key value of the target data is 29, and the target data is the data corresponding to the primary key value 29. When it is determined that the node where the target data is located is node 7, the hash value of the identifier is calculated according to the identifier of node 7. Further, the query is as follows: Figure 2 The hash table shown determines whether the location corresponding to the hash value of the identifier stores information. If no information is stored in the location, it means that the data page corresponding to node 7 is not cached in the memory. If the location stores the corresponding address information, it means that the data page corresponding to node 7 is in the memory. At this time, the data page corresponding to the node 7 cached in the memory can be accessed, for example, read, write, modify, delete, etc. Specifically, the data page corresponding to node 7 may include multiple records. Therefore, when accessing the data page corresponding to the node 7 cached in the memory, operations such as reading, writing, modifying, and deleting can be performed on some or all of the multiple records.
[0048] In order to avoid conflicts in access to the same data page in memory by different threads in a computer system, each thread can lock the hash table before accessing a data page, and release the lock on the hash table after the data access is completed.
[0049] For example, taking the delete operation as an example, the specific implementation process is as follows:
[0050] 1) Add a mutex lock to the hash table.
[0051] 2) Delete the data page corresponding to node 7 cached in the memory, or delete some records in the data page.
[0052] 3) Release the mutex on the hash table.
[0053] For example, taking the read operation as an example, the specific implementation process is as follows:
[0054] 1) Add a shared lock to the hash table.
[0055] 2) Read the data page corresponding to node 7 from the memory, or read part of the records in the data page from the memory.
[0056] 3) Release the shared lock on the hash table.
[0057] For example, taking the write operation as an example, the specific implementation process is as follows:
[0058] 1) Add a mutex lock to the hash table.
[0059] 2) Write a new record to the data page corresponding to node 7 cached in memory.
[0060] 3) Release the mutex on the hash table.
[0061] For example, taking the modification operation as an example, the specific implementation process is as follows:
[0062] 1) Add a mutex lock to the hash table.
[0063] 2) Modify the data page corresponding to node 7 cached in the memory, or modify some records in the data page.
[0064] 3) Release the mutex on the hash table.
[0065] In addition, if the target data to be accessed is not in the memory, it is also necessary to write the content in the disk block to which the target data belongs into the memory, and record the storage address of the data page corresponding to the content in the memory in the hash table.
[0066] For example, the primary key value of the target data is 75. By traversing the B+Tree, it is determined that the target data is on node 8. However, after calculating the hash value of the identifier of node 8, by querying the hash table, it is determined that the hash table does not store the storage address of the data page corresponding to node 8 in the memory. In other words, the data page corresponding to node 8 is not in the memory. At this time, it is necessary to read the content of disk block 8 from the disk and write the content of disk block 8 to a newly created data page in the memory. In other words, it is necessary to insert a data page in the memory. The implementation process of the insertion operation is as follows:
[0067] 1) Add a mutex lock to the hash table.
[0068] 2) According to the hash value corresponding to the identifier of node 8, the position corresponding to the hash value is determined from the hash table, and the storage address of the data page corresponding to node 8 in the memory is stored at the position.
[0069] 3) Release the mutex on the hash table.
[0070] It is understandable that when accessing data in the memory or inserting a new data page in the memory, the hash table needs to be locked, that is, mutual exclusion needs to be performed at the granularity of the hash table. Since the hash table manages the entire memory, locking the hash table is equivalent to locking the entire memory. In addition, the CPU retrieves data from the memory, not directly from the disk. If the data accessed by the CPU is not in the memory, the corresponding data in the disk needs to be cached in the memory. Therefore, when a thread adds a mutex lock to the hash table, other threads will not be able to access the memory, and it will be impossible to determine whether the data accessed by the thread is in the memory, and thus it will be impossible to retrieve data from the disk. In other words, locking the hash table is equivalent to locking the entire database. Therefore, memory is the basis of database logical calculations, and the concurrency of memory directly affects the throughput of the database and system performance. Therefore, in a high-concurrency scenario, locking the hash table will cause memory access to become a performance bottleneck, resulting in low system performance. Even if the hash table is split into multiple parts and part of the hash table is locked, the problem of low computer system performance cannot be fundamentally solved.
[0071] To address this problem, an embodiment of the present disclosure provides a data access method, which is described below in conjunction with a specific embodiment.
[0072] Figure 3 The data access method flow chart provided by the embodiment of the present disclosure. The specific steps of the method are as follows:
[0073] S301: Obtain a data access request, where the data access request includes identification information of target data to be accessed.
[0074] Specifically, the identification information of the target data to be accessed may be a primary key value of the target data to be accessed.
[0075] S302. According to the identification information of the target data to be accessed, traverse the data structure corresponding to the database to determine a first node and a second node in the data structure, wherein the first node is a child node of the second node, and the first node includes the identification information of the target data.
[0076] In this embodiment, for a data page that has been cached in the memory, the storage address of the data page in the memory may be added to the node corresponding to the data page.
[0077] In order to distinguish the address of the disk block occupied by the node as described above from the storage address of the data page in the memory mentioned in the embodiment of the present disclosure, the address of the disk block occupied by the node as described above can be recorded as the first pointer, and the storage address of the data page in the memory can be recorded as the second pointer. In other words, if the data page corresponding to a node in the B+Tree is cached in the memory, the node includes both the first pointer and the second pointer. If the data page corresponding to a node in the B+Tree is not in the memory, the node includes the first pointer but not the second pointer.
[0078] For example, an in-memory cache like Figure 4 The data pages corresponding to nodes 1, 2, 3, 7, 8, and 9 are shown. The storage address of the data page corresponding to node 1 in the memory is recorded as Father_pointer_page1, the storage address of the data page corresponding to node 2 in the memory is recorded as Child_pointer_page2, the storage address of the data page corresponding to node 3 in the memory is recorded as Child_pointer_page3, the storage address of the data page corresponding to node 7 in the memory is recorded as Child_pointer_page7, the storage address of the data page corresponding to node 8 in the memory is recorded as Child_pointer_page8, and the storage address of the data page corresponding to node 9 in the memory is recorded as Child_pointer_page9.
[0079] Taking p2 and Child_pointer_page2 as an example, in node 1, there may be a corresponding relationship between p2 and Child_pointer_page2. Specifically, p2 is the first pointer, and Child_pointer_page2 is the second pointer. p2 indicates the location of disk block 2 occupied by node 2, and Child_pointer_page2 indicates the storage address of the data page corresponding to node 2 in the memory.
[0080] Since node 1 is the parent node of node 2 and node 3, Child_pointer_page2 and Child_pointer_page3 may be recorded in node 1.
[0081] Since node 3 is the parent node of node 7, node 8, and node 9, Child_pointer_page7, Child_pointer_page8, and Child_pointer_page9 can be recorded in node 3. In other words, the parent node can record the storage address of the data page corresponding to the child node in the memory. In addition, the child node can also record the storage address of the data page corresponding to its parent node in the memory.
[0082] For example, Father_pointer_page3 is recorded in nodes 7, 8, and 9 respectively, and Father_pointer_page3 indicates the storage address in memory of the data page corresponding to node 3. That is, Father_pointer_page3 in nodes 7, 8, and 9 is the same as Child_pointer_page3 recorded in node 1.
[0083] Similarly, nodes 2 and 3 respectively record the storage address of the data page corresponding to node 1 in the memory, namely, Father_pointer_page1.
[0084] That is to say, Figure 4 The B+Tree shown is not only a data structure corresponding to the database, but also can be used to manage memory. That is, in this embodiment, there is no need to manage the memory in the form of a hash table.
[0085] Furthermore, according to the primary key value of the target data to be accessed, Figure 4 Traverse the B+Tree shown.
[0086] Optionally, the data structure is a tree-like data structure; and traversing the data structure corresponding to the database includes: traversing the data structure corresponding to the database starting from a root node.
[0087] For example, node 1 is the root node, and the primary key value of the target data is 29. Figure 4 As shown, the B+Tree is traversed from the root node. Specifically, the traversal starts from the root node and continues to the leaf nodes. The specific traversal process includes the following steps:
[0088] 1) Add a shared lock to the root node.
[0089] 2) Read the content in the root node, compare the primary key value 29 with the primary key value 28, select pointer p3, and read Child_pointer_page3.
[0090] 3) Release the shared lock of the root node.
[0091] 4) Add a shared lock to node 3.
[0092] 5) According to Child_pointer_page3, read the data page corresponding to node 3 from the memory, compare the primary key value 29 and the interval (36,79), select pointer p7, and read Child_pointer_page7.
[0093] 6) Release the shared lock of node 3.
[0094] 7) Add a shared lock to node 7.
[0095] 8) According to Child_pointer_page7, read the data page corresponding to node 7 from the memory, and find the keyword 29 in the data page.
[0096] 9) Release the shared lock of node 7.
[0097] Through Figure 4 By traversing the B+Tree shown, node 7 and node 3 can be determined, wherein node 7 is a child node of node 3, and node 3 is a parent node of node 7. Node 7 includes the primary key value 29 of the target data. Here, node 7 including the primary key value 29 of the target data can be recorded as the first node, and node 3 can be recorded as the second node.
[0098] S303. If the second node includes the storage address of the first data page corresponding to the first node in the memory, the second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released.
[0099] like Figure 4As shown, since node 3 includes the storage address of the data page corresponding to node 7 in the memory, namely Child_pointer_page7, it means that the data page corresponding to node 7 is in the memory. Therefore, the data page corresponding to node 7 stored in the memory can be directly accessed. Specifically, when a thread in the computer system needs to access the data page corresponding to node 7 stored in the memory, the thread can first lock node 3, and further, according to the storage address of the data page corresponding to node 7 in the memory, namely Child_pointer_page7, find the data page corresponding to node 7 stored in the memory, and access the data page corresponding to node 7, and release the lock on node 3 after the access. When accessing the data page corresponding to node 7, all or part of the records in the data page can be deleted, modified, written, read, etc.
[0100] Optionally, if the second node includes the storage address of the first data page corresponding to the first node in the memory, the first node also includes the storage address of the second data page corresponding to the second node in the memory.
[0101] like Figure 4 As shown, node 7 is the child node of node 3, and node 3 is the parent node of node 7. Node 7 is recorded as the first node, and node 3 is recorded as the second node. When node 3 includes the storage address of the data page corresponding to node 7 in the memory, namely Child_pointer_page7, node 7 may also include the storage address of the data page corresponding to node 3 in the memory, namely Father_pointer_page3. Specifically, the data page corresponding to the first node may be recorded as the first data page, and the data page corresponding to the second node may be recorded as the second data page.
[0102] The disclosed embodiment traverses the data structure corresponding to the database through the identification information of the target data to be accessed, and determines the first node and the second node with a parent-child relationship from the data structure, wherein the first node is the child node of the second node, and the first node includes the identification information of the target data. Since the data structure is not only the organizational form of the data in the database, but also the data structure is used to manage the memory, that is to say, in the present embodiment, there is no need to specially set up a hash table to manage the memory. In addition, in the present embodiment, when the data page corresponding to the child node has been cached in the memory, the storage address of the data page corresponding to the child node in the memory can be recorded in the parent node of the child node. In this way, when a thread needs to access the data page corresponding to a child node in the memory, it only needs to lock the parent node of the child node. Since the number of child nodes under a parent node is limited, locking a parent node only prevents other threads from accessing the data pages corresponding to the limited number of child nodes in the memory, and will not cause other threads to be unable to access all or most of the data pages in the memory, thereby achieving lock-free at the memory granularity. Since only the lock is added at the granularity of the data page, the granularity of the locked data is greatly reduced, thereby preventing memory access from becoming a performance bottleneck in high concurrency scenarios, ensuring the throughput of the database, and effectively improving the performance of the computer system.
[0103] On the basis of the above embodiment, locking the second node includes: locking the storage address of the first data page included in the second node in the memory.
[0104] like Figure 4 As shown, when a thread needs to access the data page corresponding to node 7, the thread can only lock part of the content in node 3.
[0105] For example, node 3 includes Child_pointer_page7, Child_pointer_page8, and Child_pointer_page9. Therefore, when node 3 is locked, only Child_pointer_page7, Child_pointer_page8, and Child_pointer_page9 may be locked, or only Child_pointer_page7 may be locked.
[0106] This embodiment can further reduce the locked data granularity by locking the storage address of the first data page included in the second node in the memory, thereby further improving the computer performance.
[0107] Optionally, locking the second node and releasing the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory includes the following possible situations:
[0108] One possible situation is: locking the second node, and releasing the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory, including: adding a mutex lock to the second node, and releasing the mutex lock on the second node after deleting the first data page stored in the memory or deleting some records in the first data page according to the storage address of the first data page in the memory.
[0109] For example, when a thread needs to delete the data page corresponding to node 7 in the memory or needs to delete some records in the data page corresponding to node 7 in the memory, the implementation process of the deletion operation is as follows:
[0110] 1) Add a mutex lock to node 3.
[0111] 2) According to the storage address of the data page corresponding to node 7 in the memory, find the data page corresponding to node 7 in the memory, delete the data page corresponding to node 7 or delete some records in the data page corresponding to node 7. If the data page corresponding to node 7 is deleted, Child_pointer_page7 in node 3 needs to be deleted, or Child_pointer_page7 in node 3 needs to be assigned NULL.
[0112] 3) Release the mutex on node 3.
[0113] It can be understood that node 3 is a non-leaf node and node 7 is a leaf node. Since node 3 and node 7 are interrelated, node 7 and node 3 are interrelated leaf nodes and non-leaf nodes. Since leaf nodes can be queried from non-leaf nodes, the access frequency of data pages corresponding to non-leaf nodes in memory is higher than that of data pages corresponding to leaf nodes. Therefore, when deleting data pages in memory, data pages corresponding to leaf nodes can be deleted first.
[0114] Optionally, if the first data page stored in the memory is deleted, then after releasing the mutex lock on the second node, the method further includes: deleting the storage address in the memory of the second data page corresponding to the second node included in the first node.
[0115] For example, after a thread deletes the data page corresponding to node 7 in the memory, the thread can further delete the storage address of the data page corresponding to node 3 included in node 7, namely Father_pointer_page3 in the memory, or assign Father_pointer_page3 to NULL. The process is described as follows:
[0116] 1) Add a mutex lock to node 7.
[0117] 2) Delete the storage address of the data page corresponding to node 3 included in node 7, namely Father_pointer_page3 in the memory.
[0118] 3) Release the mutex lock on node 7.
[0119] It is understandable that, since the data page corresponding to the node is cached in the memory, when the data page corresponding to a node is deleted, it does not mean that the node is deleted from the data structure, but only the storage address of the data page corresponding to the node in the memory is deleted from the parent node of the node, and the storage address of the data page corresponding to the parent node in the memory included in the node is deleted. The parent-child relationship between the node and its parent node still exists in the data structure.
[0120] Another possible situation is: locking the second node, and releasing the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory, including: adding a shared lock to the second node, and releasing the shared lock on the second node after reading the first data page from the memory or reading part of the records in the first data page according to the storage address of the first data page in the memory.
[0121] For example, a thread needs to read the data page corresponding to node 7 in the memory or some records in the data page. The implementation process of the read operation is as follows:
[0122] 1) Add a shared lock to node 3.
[0123] 2) According to Child_pointer_page7 in node 3, the data page corresponding to node 7 is read from the memory or part of the records in the data page is read from the memory.
[0124] 3) Release the shared lock on node 3.
[0125] Another possible situation is: locking the second node, and releasing the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory, including: adding a mutex lock to the second node, and releasing the mutex lock on the second node after modifying some or all records in the first data page stored in the memory according to the storage address of the first data page in the memory.
[0126] For example, a thread needs to modify the data page corresponding to node 7 in the memory or some records in the data page. The implementation process of the modification operation is as follows:
[0127] 1) Add a mutex lock to node 3.
[0128] 2) According to Child_pointer_page7 in node 3, the data page corresponding to node 7 is found in the memory, and all or part of the records in the data page corresponding to node 7 are modified.
[0129] 3) Release the mutex on node 3.
[0130] Another possible situation is: locking the second node, and releasing the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory, including: adding a mutex lock to the second node, and releasing the mutex lock on the second node after writing a new record in the first data page stored in the memory according to the storage address of the first data page in the memory.
[0131] For example, a thread needs to write a new record to the data page corresponding to node 7 in the memory. The implementation process of the new record writing operation is as follows:
[0132] 1) Add a mutex lock to node 3.
[0133] 2) According to Child_pointer_page7 in node 3, find the data page corresponding to node 7 in the memory, and write a new record into the data page corresponding to node 7.
[0134] 3) Release the mutex on node 3.
[0135] This embodiment locks the parent node and accesses the data page according to the storage address of the data page of the child node recorded in the parent node in the memory, for example, reading all or part of the records in the data page, deleting all or part of the records in the data page, modifying all or part of the records in the data page, writing new records in the data page, etc. After the corresponding access operation is completed, the lock on the parent node is released. Compared with the method of locking the entire memory by locking the hash table, the method of locking the parent node in this embodiment greatly reduces the granularity of the locked data, thereby avoiding memory access from becoming a performance bottleneck in high concurrency scenarios, ensuring the throughput of the database, and effectively improving the performance of the computer system.
[0136] Optionally, the method also includes: if the second node does not include the storage address of the first data page corresponding to the first node in the memory, adding a mutex lock to the second node, caching the first data page corresponding to the first node into the memory, writing the storage address of the first data page in the memory to the second node, and releasing the mutex lock on the second node.
[0137] like Figure 4 As shown, the primary key value of the target data is 29. After traversing the B+Tree from the root node of the B+Tree according to the primary key value 29 of the target data, nodes 7 and 3 are determined, and node 7 is recorded as the first node, and node 3 is recorded as the second node. However, node 3 includes Child_pointer_page8 and Child_pointer_page9, but does not include Child_pointer_page7, which means that the data page corresponding to node 7 is not in the memory. At this time, it is necessary to read the content of disk block 7 occupied by node 7 from the disk and put the content into a newly created data page in the memory, that is, it is necessary to insert a new data page in the memory, and the content in the data page is the content of disk block 7 occupied by node 7. The implementation process of the insertion operation is as follows:
[0138] 1) Add a mutex lock to node 3.
[0139] 2) Insert a new data page into the memory. The content of this data page is the content of disk block 7 occupied by node 7. The newly inserted data page into the memory is the data page corresponding to node 7. Add the storage address of the data page corresponding to node 7 in the memory to node 3, that is, add Child_pointer_page7 to node 3.
[0140] 3) Release the mutex on node 3.
[0141] Optionally, after releasing the mutual exclusion lock on the second node, the method further includes: writing into the first node a storage address in the memory of a second data page corresponding to the second node.
[0142] For example, after adding Child_pointer_page7 in node 3, you can further add Father_pointer_page3 in node 7. The specific implementation process is as follows:
[0143] 1) Add a mutex lock to node 3.
[0144] 2) Insert a new data page into the memory. The content of this data page is the content of disk block 7 occupied by node 7. The newly inserted data page into the memory is the data page corresponding to node 7. Add the storage address of the data page corresponding to node 7 in the memory to node 3, that is, add Child_pointer_page7 to node 3.
[0145] 3) Release the mutex on node 3.
[0146] 4) Add a mutex lock to node 7.
[0147] 5) Add Father_pointer_page3 in node 7.
[0148] 6) Release the mutex lock on node 7.
[0149] This embodiment can achieve fast traversal of the B+Tree by adding the storage address of the data page corresponding to the child node in the parent node and the storage address of the data page corresponding to the parent node in the child node, thereby achieving fast access to the data page in the memory.
[0150] Figure 5 The data access device provided by the embodiment of the present disclosure can execute the processing flow provided by the data access method embodiment, such as Figure 5 As shown, the data access device 50 includes:
[0151] An acquisition module 51 is used to acquire a data access request, wherein the data access request includes identification information of target data to be accessed;
[0152] A traversal module 52, used to traverse the data structure corresponding to the database according to the identification information of the target data to be accessed;
[0153] A determination module 53, configured to determine a first node and a second node in the data structure, wherein the first node is a child node of the second node, and the first node includes identification information of the target data;
[0154] The access module 54 is used to lock the second node if the second node includes the storage address of the first data page corresponding to the first node in the memory, and release the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory.
[0155] Optionally, if the second node includes the storage address of the first data page corresponding to the first node in the memory, the first node also includes the storage address of the second data page corresponding to the second node in the memory.
[0156] Optionally, the data structure is a tree-like data structure; the traversal module 52 is specifically used to traverse the data structure corresponding to the database starting from the root node.
[0157] Optionally, the access module 54 is specifically used to: add a mutex lock to the second node, and release the mutex lock on the second node after deleting the first data page stored in the memory or deleting some records in the first data page according to the storage address of the first data page in the memory.
[0158] Optionally, after deleting the first data page stored in the memory and releasing the mutex lock on the second node, the access module 54 is further configured to: delete the storage address in the memory of the second data page corresponding to the second node included in the first node.
[0159] Optionally, the access module 54 is specifically used for:
[0160] A shared lock is added to the second node, and after the first data page is read from the memory or part of the records in the first data page is read according to the storage address of the first data page in the memory, the shared lock on the second node is released.
[0161] Optionally, the access module 54 is specifically used for:
[0162] A mutual exclusion lock is added to the second node, and after modifying part or all of the records in the first data page stored in the memory according to the storage address of the first data page in the memory, the mutual exclusion lock on the second node is released.
[0163] Optionally, the access module 54 is specifically used for:
[0164] A mutual exclusion lock is added to the second node, and after a new record is written into the first data page stored in the memory according to the storage address of the first data page in the memory, the mutual exclusion lock on the second node is released.
[0165] Optionally, when the access module 54 locks the second node, it is specifically used to: lock the storage address of the first data page included in the second node in the memory.
[0166] Optionally, the access module 54 is also used to: if the second node does not include the storage address of the first data page corresponding to the first node in the memory, then add a mutex lock to the second node, cache the first data page corresponding to the first node to the memory, write the storage address of the first data page in the memory to the second node, and release the mutex lock on the second node.
[0167] Optionally, after releasing the mutex on the second node, the access module 54 is further configured to: write into the first node a storage address in the memory of a second data page corresponding to the second node.
[0168] Figure 5 The data access device of the illustrated embodiment can be used to execute the technical solution of the above-mentioned method embodiment, and its implementation principle and technical effect are similar and will not be repeated here.
[0169] Figure 6 The data access device provided by the embodiment of the present disclosure can execute the processing flow provided by the data access method embodiment, such as Figure 6 As shown, the data access device 60 includes: a memory 61, a processor 62, a computer program and a communication interface 63; wherein the computer program is stored in the memory 61 and is configured so that the processor 62 executes the data access method as described above.
[0170] In addition, an embodiment of the present disclosure further provides a computer-readable storage medium on which a computer program is stored. The computer program is executed by a processor to implement the data access method described in the above embodiment.
[0171] It should be noted that, in this article, relational terms such as "first" and "second" are only used to distinguish one entity or operation from another entity or operation, and do not necessarily require or imply any such actual relationship or order between these entities or operations. Moreover, the terms "include", "comprise" or any other variants thereof are intended to cover non-exclusive inclusion, so that a process, method, article or device including a series of elements includes not only those elements, but also other elements not explicitly listed, or also includes elements inherent to such process, method, article or device. In the absence of further restrictions, the elements defined by the sentence "comprise a ..." do not exclude the existence of other identical elements in the process, method, article or device including the elements.
[0172] The above description is only a specific embodiment of the present disclosure, so that those skilled in the art can understand or implement the present disclosure. Various modifications to these embodiments will be apparent to those skilled in the art, and the general principles defined herein may be implemented in other embodiments without departing from the spirit or scope of the present disclosure. Therefore, the present disclosure will not be limited to the embodiments described herein, but will conform to the widest scope consistent with the principles and novel features disclosed herein.
Claims
1. A data access method, characterized in that: The method comprises: Obtaining a data access request, wherein the data access request includes identification information of target data to be accessed; According to the identification information of the target data to be accessed, traverse the data structure corresponding to the database to determine a first node and a second node in the data structure, wherein the first node is a child node of the second node, and the first node includes the identification information of the target data; If the second node includes the storage address of the first data page corresponding to the first node in the memory, the second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released; Locking the second node includes: The storage address of the first data page included in the second node in the memory is locked.
2. The method according to claim 1, characterized in that: If the second node includes the storage address of the first data page corresponding to the first node in the memory, then the first node also includes the storage address of the second data page corresponding to the second node in the memory.
3. The method according to claim 1 or 2, characterized in that: The data structure is a tree-like data structure; The traversing of the data structure corresponding to the database includes: Traverse the data structure corresponding to the database starting from the root node.
4. The method according to claim 1 or 2, characterized in that: The second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released, including: A mutual exclusion lock is added to the second node, and after deleting the first data page stored in the memory or deleting some records in the first data page according to the storage address of the first data page in the memory, the mutual exclusion lock on the second node is released.
5. The method according to claim 4, characterized in that If the first data page stored in the memory is deleted, after releasing the mutex lock on the second node, the method further includes: Delete the storage address in the memory of the second data page corresponding to the second node included in the first node.
6. The method according to claim 1 or 2, characterized in that: The second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released, including: A shared lock is added to the second node, and after the first data page is read from the memory or part of the records in the first data page is read according to the storage address of the first data page in the memory, the shared lock on the second node is released.
7. The method according to claim 1 or 2, characterized in that: The second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released, including: A mutual exclusion lock is added to the second node, and after modifying part or all of the records in the first data page stored in the memory according to the storage address of the first data page in the memory, the mutual exclusion lock on the second node is released.
8. The method according to claim 1 or 2, characterized in that: The second node is locked, and after the first data page stored in the memory is accessed according to the storage address of the first data page in the memory, the lock on the second node is released, including: A mutual exclusion lock is added to the second node, and after a new record is written into the first data page stored in the memory according to the storage address of the first data page in the memory, the mutual exclusion lock on the second node is released.
9. The method according to claim 1, characterized in that: The method further comprises: If the second node does not include the storage address of the first data page corresponding to the first node in the memory, a mutex lock is added to the second node, and after caching the first data page corresponding to the first node into the memory, the storage address of the first data page in the memory is written into the second node, and the mutex lock on the second node is released.
10. The method according to claim 9, characterized in that After releasing the mutex lock on the second node, the method further includes: The storage address of the second data page corresponding to the second node in the memory is written into the first node.
11. A data access device, characterized in that: include: An acquisition module, used to acquire a data access request, wherein the data access request includes identification information of target data to be accessed; A traversal module, used to traverse the data structure corresponding to the database according to the identification information of the target data to be accessed; A determination module, configured to determine a first node and a second node in the data structure, wherein the first node is a child node of the second node, and the first node includes identification information of the target data; an access module, configured to lock the second node if the second node includes the storage address of the first data page corresponding to the first node in the memory, and release the lock on the second node after accessing the first data page stored in the memory according to the storage address of the first data page in the memory; When the access module locks the second node, it is specifically used to: lock the storage address of the first data page included in the second node in the memory.
12. A data access device, characterized in that: include: Memory; processor; as well as Computer programs; The computer program is stored in the memory and is configured to be executed by the processor to implement the method according to any one of claims 1 to 10.
13. A computer-readable storage medium having a computer program stored thereon, characterized in that: When the computer program is executed by a processor, the method according to any one of claims 1 to 10 is implemented.