Explain the principles behind the relational database queries through the index to enhance the efficiency of

  1. Without an index, the database engine needs to find data by a full table scan, which will produce large amounts of disk IO.

  2. Relational database using B + tree indexing to speed up queries. B + tree is a binary search tree (each node must be the key: Any keys left subtree are greater than saved, to be smaller than any key value stored in the right sub-tree), so look for random when a key can be accelerated by performing a binary search query from the root node, the number of layers depends on the cost of the query tree.

  3. For range queries and sorting optimization: save pointers to the next leaf node at each leaf node, so that when the scope of the query specified range, starting with the root node to find its leaf nodes from the left value range, after traversing through the leaves back the right node to find the corresponding range value, which can accelerate range queries, sorting, grouping, and other database query operation.

  4. For optimizing disk read and write speed: other nodes except the leaf nodes only save the key, so a single disk can read and write to get as much data. To MySQL, for example, corresponding to the table a 10 million line of B + tree to find theoretically according to the master key only needs three times the disk IO, which brought relative to the full table scan is more on the order of disk IO performance.

  5. MySQL and other database engine at the time of the actual implementation of B + tree index, disk read and write optimized for: non-leaf nodes store only key values, in addition to the key value of the leaf node will store the data, in accordance with the main index to distinguish between different storage data index (clustered index) and secondary indexes:

    Leaf node a) a main storage a complete record of the index value corresponding to the key, use the primary index to find, can be directly output records; a table can create a primary index.

    Leaf nodes b) the general index is stored corresponding to the primary key value, and therefore when a secondary index to find, it is necessary to find the primary key, and then to the main index lookup; a plurality of secondary index table can be created.

  6. In addition to B + tree, relational databases generally support hash indexes, hash indexes can be very efficient random search, but for range queries, sorting and grouping are not supported.

Released eight original articles · won praise 2 · Views 210

Guess you like

Origin blog.csdn.net/qq_37492314/article/details/105291529