Database index is a sort of data structure in the database management system to help quickly query and update data in database tables. The implementation of the index usually uses B-tree and its variant plus + tree.
In addition to data, the database system maintains a data structure that satisfies a particular lookup algorithm, which refers to data in some way, so that advanced lookup algorithms can be implemented on these data structures. This data structure is the index.
There is a cost to setting up the index for a table: the first is to increase the storage space of the database, and the second is to spend more time inserting and modifying the data (because the index will also change).
The figure above shows a possible way of indexing. On the left is the datasheet, a total of two columns and seven records, and the leftmost is the physical address of the data record (note that logically adjacent records are not necessarily physically contiguous on disk). To speed up the Col2 lookup, you can maintain a two-fork lookup tree shown on the right, each node containing the index key value and a pointer to the corresponding data record physical address, so that the binary lookup can be used to obtain the corresponding data within the complexity of O (log2n).
Creating an index can greatly improve the performance of your system.
First, you can guarantee the uniqueness of each row of data in a database table by creating a unique index.
Second, you can greatly speed up the retrieval of data, which is the main reason to create indexes.
Third, you can speed up the connection between tables and tables, especially in terms of realizing the referential integrity of the data.
Finally, when you use grouping and sorting clauses for data retrieval, you can also significantly reduce the time to group and sort in a query.
In the process of querying, the optimization of the hidden device can be used to improve the performance of the system by using the index.
Someone might ask: Why do you not create an index for each column in the table because there are so many advantages to adding an index? Because there are many disadvantages to adding indexes.
First, it takes time to create indexes and maintain indexes, which increase as the amount of data increases.
Second, the index needs to occupy the physical space, in addition to the data table occupies the data space, each index also occupies a certain physical space, if you want to establish a clustered index, then need more space.
Third, when the data in the table is added, deleted and modified, the index will be maintained dynamically, thus reducing the data maintenance speed.
Indexes are built on top of some columns in a database table. When you create an index, you should consider which columns you can create indexes on, and on which columns you cannot create indexes. In general, you should create indexes on these columns: on the columns that you frequently need to search, you can speed up your search; On columns that are primary keys, enforce the uniqueness of the column and the arrangement of the data in the organization table; These columns are mostly foreign keys and can speed up the connection, often on connected columns; Create an index on a column that often needs to be searched based on scope, because indexes are already sorted, their specified ranges are contiguous, and indexes are created on columns that often need to be sorted, because the indexes are sorted so that the query can take advantage of the sort of the index, speeding up sorting queries, and creating indexes on frequently used columns in the WHERE clause To speed up the judgment of conditions.
Similarly, indexes should not be created for some columns. In general, these columns that should not create indexes have the following characteristics:
First, you should not create indexes for columns that are rarely used or referenced in queries. This is because, since these columns are rarely used, they are indexed or indexed and do not increase the query speed. On the contrary, because of the addition of indexes, it reduces the maintenance speed of the system and increases the space requirement.
Second, you should not add indexes to columns that have very few data values. This is because, because of the low values of these columns, such as the gender column of the personnel table, the result set's data rows account for a large proportion of the data rows in the table, that is, the data rows that need to be searched in the table are large. Adding indexes does not significantly speed up the retrieval.
Third, columns that are defined as text, image, and bit data types should not be indexed. This is because these columns have either a large amount of data or a very small number of values.
Four, you should not create an index when the modification performance is far greater than the retrieval performance. This is because the modification performance and retrieval performance are contradictory. When indexing is added, retrieval performance is improved, but modification performance is reduced. When the index is reduced, the modification performance is improved and the retrieval performance is reduced. Therefore, indexes should not be created when the modification performance is far greater than the retrieval performance.
Depending on the functionality of your database, you can create three indexes in the Database Designer: A unique index, a primary key index, and a clustered index.
Unique index
A unique index is one that does not allow any two rows to have the same index value.
Most databases do not allow a newly created unique index to be saved with a table when duplicate key values exist in existing data. The database may also prevent the addition of new data that will create duplicate key values in the table. For example, if a unique index is created on the employee's last name (lname) in the employee table, no two employees will have the same surname. Primary key indexed database tables often have a column or combination of columns whose values uniquely identify each row in the table. This column is called the primary key of the table. Defining a primary key for a table in a database diagram automatically creates a primary key index, which is a specific type of unique index. The index requires that each value in the primary key is unique. When a primary key index is used in a query, it also allows for fast access to the data. Clustered index in a clustered index, the physical order of the rows in the table is the same as the logical (indexed) Order of the key values. A table can contain only one clustered index.
If an index is not a clustered index, the physical order of the rows in the table does not match the logical order of the key values. Clustered indexes typically provide faster data access than nonclustered indexes.
The principle of locality and disk pre-read
Because of the nature of the storage media, the disk itself is much slower to access than main memory, coupled with the cost of mechanical exercise, disk access speed is often one of the hundreds of of main memory, so in order to improve efficiency, to minimize disk I/O. To do this, the disk is often not read strictly on demand, but read each time, even if only a single byte, the disk will start from this location, sequentially reading a certain length of data into memory. The rationale for this is the famous local principle in computer science: When a data is used, the data near it is usually used immediately. The data that is required during program execution is usually more centralized.
Because of the high efficiency of disk sequential reads (no seek time is required, only a small amount of rotation time), prefetching can improve I/O efficiency for locally-accessible programs.
The length of the prefetch is typically the integer multiple of the page. Pages are logical blocks of Computer Management memory, and hardware and operating systems often split main memory and disk storage into contiguous blocks of equal size, each of which is called a page (in many operating systems, the page size is typically 4k), and main memory and disk Exchange data in page units. When the program to read the data is not in main memory, will trigger a page fault exception, the system will be sent to the disk read signal, the disk will find the starting position of data and read one or several pages back to load in memory, and then abnormal return, the program continues to run.
Performance analysis of B-/+tree index
Here we can finally analyze the performance of the B-/+tree index.
As mentioned above, the index structure is generally evaluated using disk I/O times. First from the B-tree analysis, according to the definition of b-tree, we can retrieve the maximum access to H nodes. The designer of the database system cleverly utilizes the disk prefetching principle to set the size of a node to be equal to a page so that each node needs only one I/O to be fully loaded. In order to achieve this goal, the following techniques need to be used to actually implement B-tree:
Each time you create a new node, directly request a page space, so that a node is physically also stored in a page, plus the computer storage allocation is page-aligned, the implementation of a node only once I/O.
A maximum of h-1 I/O (the root node resident memory) is required at one time in B-tree, and the asymptotic complexity is O (h) =o (LOGDN). In general practical applications, the out degree d is a very large number, usually more than 100, so H is very small (usually no more than 3).
The structure of the red-black tree, H is obviously much deeper. Because logically close nodes (parent-child) are physically far away from being able to exploit locality, the red-black tree has an O (h) I/O asymptotic complexity, which is significantly worse than B-tree.
To sum up, using B-tree as index structure efficiency is very high.
Should spend time learning B-Tree and B + Tree data structures
================================================================================================
1) B-Tree
Each node in the B-tree contains the address pointer to the data object for which the key values and key values are stored, so it is possible to search for an object successfully without reaching the leaf node of the tree.
A successful search includes a search within a node and a search along a path, depending on the level at which the key is located and the number of key codes in the node.
The way to find a given keyword in a B-tree is to take the root node first, the keyword contained in the root node K1,..., kj Find the given keyword (available order lookup or binary lookup method), if found to be equal to the given value of the keyword, then find success; otherwise, you can be sure to check the keyword in some ki or ki+ 1, so take the next layer of index node that Pi refers to to continue to find, until found, or the pointer pi is empty to find failure.
2) B + Tree
The key code stored in a B + tree non-leaf node does not indicate the address pointer of the data object, and the non-node is only the index part. All the leaf nodes are on the same layer, which contains all the key codes and the corresponding data objects, and the leaf nodes are linked by the key code from small to large order. If the actual data objects are stored in the order in which they are added, rather than by the number of key codes, the index of the leaf node must be a dense index, and if the actual data is stored in the order of the key code, the leaf node is indexed in a sparse index.
The B + Tree has 2 head pointers, one is the root node of the tree, and one is the leaf node of the minimum key code.
So there are two ways to search for B + trees:
One is the sequential search of the linked list, which is pulled by the leaf node itself.
One is to start the search from the root node, similar to the B-tree, but if the Non-leaf node's key is equal to the given value, the search does not stop, but continues along the right pointer, until the key code on the leaf node is traced. So whether or not the search is successful, you will walk through all the layers of the tree.
In a B + tree, insertions and deletions of data objects are performed only on leaf nodes.
The differences between the two data structures that process the index:
The same key value in the A,b tree does not appear more than once, and it may appear at the leaf node or in a Non-leaf node. The key of the B + tree will certainly appear in the leaf nodes, and it is possible to repeat in the NON-LEAF nodes to maintain the balance of the B + tree.
b, because the B-tree key position is indefinite, and only once in the whole tree structure, although can save the storage space, but make in insert, delete operation complexity increase obviously. B + trees are a better compromise than that.
The query efficiency of the c,b tree is related to the position of the key in the tree, the maximum time complexity is the same as the B + tree (at the point of leaf node), the minimum time complexity is 1 (at the root node). The time complexity of a B + tree is fixed on a tree that has been built.