[Database] Blocked concurrency control under tree data organization structure, B-tree index concurrent access control, tree protocol principle and case analysis

Database concurrent access tree protocol

专栏content

  • Handwritten database toadb
    This column mainly introduces how to develop from scratch, the steps of development, the principles involved in the development process, problems encountered, etc., so that everyone can keep up. And can be developed together so that everyone who needs it can become a participant.
    This column will be updated regularly, and the corresponding code will also be updated regularly. The code at each stage will be tagged to facilitate learning at each stage.

Open source contribution

Personal homepage:My homepage
Manage community: Open source database
Motto: When the sky is strong, a gentleman will strive to strive for self-improvement; when the terrain is weak, a gentleman will carry his wealth with kindness.

Preface

With the rapid development of information technology, data has penetrated into various fields and become one of the most important assets of modern society. In this era of big data, database theory plays a vital role in data management, storage and processing. However, many readers may be confused about database theory and do not know how to choose a suitable database, how to design an effective database structure, and how to process and manage large amounts of data. Therefore, this column aims to provide readers with a comprehensive and in-depth guide to database theory to help them better understand and apply database technology.

Database theory is the study of how to effectively manage, store and retrieve data. In the modern information society, the amount of data is growing exponentially, and how to efficiently process and manage this data has become an important issue. At the same time, with the continuous development of emerging technologies such as cloud computing, the Internet of Things, and big data, the importance of database theory has become increasingly prominent.

Therefore, the sharing of this column hopes to improve everyone's knowledge and understanding of database theory and help interested friends.

Overview

There is a type of database element whose organizational structure is in the form of a tree. There is no containment relationship between elements in the tree, such as the organizational form of B-tree index data. Access to such data must be done through search and access from the tree, which is consistent with The hierarchical structure of database elements introduced before is different in the locking method.

This article will focus on the access rules, blocking principles, and some optimization discussions for data organized in a tree structure.

Differences in blocking of tree structures

  • Access requires locking; when accessing tree data, if it is a B-tree index, in order to maintain the serializability of read and write operations, blocked access is required. The lock mode is the read lock, write lock, and update lock mentioned before. ;

  • The locking granularity is a node; each node of the tree is also a data block, and the locking granularity is also a node. A smaller granularity of tuples will bring more disadvantages, and a larger granularity of the entire tree will have almost no concurrency;

  • The locking method is different from 2PL; tree access starts from the root, and then traverses layer by layer to find the corresponding node location; then locking also starts from the root, and according to the two-stage locking rule introduced before, it cannot be released before use. Lock, that means there is no concurrency on the root node, which does not take advantage of the efficiency of index usage. If it is determined not to modify the tree node, then the lock of the root node can be released in advance, which is contrary to 2PL;

Based on the above differences, for tree-organized data, the tree protocol locking method is specifically used instead of the 2PL locking method. When it is determined that the current node still has space, the root node will not be modified, and the root node can be released in advance at this time. The lock also applies to intermediate nodes. Of course, the serialization guarantee relies on the order of searching from the root downwards.

tree protocol rules

The tree protocol consists of the following rules. It is assumed that a lock is used to access the tree structure, and L(X) is used to represent the locked X node;

  • When accessing a tree structure, the first lock can be added to any node in the tree;

  • Only when the lock of the parent node is held, its subsequent nodes can be locked;

  • Transactions can be unlocked at any time;

  • Unlocked nodes cannot be re-locked, even if the lock of the parent node is held at this time;

Example

Insert image description here

Three concurrent transactions, T1 starts from node A and moves through B, C, and D; transaction T2 starts from B and targets node E; transaction T3 starts from E and moves to F and G; the lock is L(X) ,Unlocking is represented by U(X).

T1 T2 T3
L1(A);R1(A);
L1(B);R1(B);
L1©; R1©;
W1(A);U1(A);
L1(D);R1(D);
W1(B);U1(B);
L2(B);R2(B);
L3(E);R3(E);
W1(D);U1(D);
W1©;U1©;
L2(E) rejected
L3(F);R3(F);
W3(F);U3(F);
L3(G);R3(G);
W3(E);U3(E);
L2(E);R2(E);
W3(G);U3(G);
W2(B);U2(B);
W2(E);U2(E);

In this example, transactions T1, T2, and T3 are scheduled concurrently according to the tree protocol. When T2 locks the E node, it conflicts with the T3 node, causing it to be delayed. After T3 releases the E node lock, it can continue to execute.

Analysis of tree protocol principle

In the tree protocol scheduling, the transactions involved in the lock must contain a serial action sequence, because they are all access sequences from top to bottom. This can be proved by using the priority graph. If there is no cycle in the priority graph, it means that It is equivalent to a serializable schedule.

Through the above protocol rules and the access sequence of the tree, when two transactions are concurrent in a tree, the following judgment can be drawn;

  • If there are several nodes and both transactions need to be locked, then the locking order on these nodes is the same;

Because when there are two or more common elements accessed by two transactions, the nodes locked by each transaction can form a subtree. The intersection of the two subtrees is also a subtree. When accessing, it also starts from the highest node and proceeds in sequence. The locking order is the same for all common elements.

Summarize

For the control of concurrent access in a tree organization, the two-stage lock mode cannot be used. In order to improve the efficiency of concurrent access, the lock on the current node path can be released in advance through the tree protocol.

end

Thank you very much for your support. Don’t forget to leave your valuable comments while browsing. If you think it is worthy of encouragement, please like and save it. I will work harder!

Author’s email: [email protected]
If there are any errors or omissions, please point them out and learn from each other.

おすすめ

転載: blog.csdn.net/senllang/article/details/134863773
おすすめ