B-tree vs hash
Compared with the ordinary Ordered Tree, B-tree has two advantages. One is that its complexity is naturally strict O (logn). Unlike ordinary Binary Trees, B-tree may degrade to O (n) is not as balanced as AVL is. Second, B-tree is ordered based on the characteristics of the storage system, therefore, its complexity cannot be lower than that of O (logn), so the number of comparisons is no less than that of other trees, however, it utilizes the features that the storage system reads a large volume of data at a time faster than reading a small amount of data at a time. One node has m sons, which can be stored continuously, it can also be read at a time. Therefore, it is important to select m for B-tree Optimization Based on the hardware features.
But the B-tree is still too complicated. The general coder, not to mention writing, is hard to understand. In general, hash is used for search, and the average hash is one hundred rows.Code. In general, it will work with the memory pool, and the memory pool code should be within one hundred lines. In terms of performance, hash is O (1. Simple and reliable.
But there will still be someone using B-TREE, this is because B-TREE has other advantages,
One is the B-TREE is ordered, this can do a lot of things, such as sorting, such as looking for the nearest number, such as search interval;
Second, the expansion of the B-TREE is more relaxed, it is easy to add nodes in the B-TREE, the complexity is also controllable, and in the hash, the hash table load is too high, the performance will drop sharply, if you want to expand the hash table, this is a very painful process, it will bring about memory fragmentation. In multi-thread scenarios, read and write security should be considered, which will result in a large increase time and is not suitable for real-time services, of course there are also manyAlgorithmThis problem can be solved, but with these algorithms, hash is no longer simple.
Third, the time complexity of hash O (1) is just a mathematical expectation. The worst case may be O (N), so it is not applicable in many cases, such as real-time control.
For details, refer to the taocp written by knuth. After the hash is introduced in volume 3, there is a comparison between the hash and the tree structure.
Another advantage of B-TREE is that it is easier to create snapshots. This should be an advantage of the tree structure. During each addition or deletion, the node on the added or deleted path is split to a snapshot, and the time space complexity is very low. Hash can also be used as a snapshot, so the method is not so elegant. For example, add a timestamp or a primary table or a schedule.
The general key-value storage system uses hash lines, which is too troublesome to use the B tree.