The role, advantages and disadvantages of database indexes

Why create an index? This is because, creating indexes can greatly improve the performance of the system. 
First, by creating a unique index, the uniqueness of each row of data in the database table can be guaranteed. 
Second, it can greatly speed up data retrieval, which is the main reason for creating indexes. 
Third, it can speed up table-to-table joins, especially in terms of achieving referential integrity of data. 
Fourth, when using the grouping and sorting clauses for data retrieval, the time for grouping and sorting in the query can also be significantly reduced. 
Fifth, by using the index, the optimization hider can be used in the query process to improve the performance of the system.

Some people may ask: there are so many advantages to adding indexes, why not create an index for every column in the table? Although this idea has its rationality, it also has its one-sidedness. Although indexes have many advantages, it is very unwise to add indexes to every column in the table. This is because increasing the index also has a number of downsides.

First, creating and maintaining indexes takes time, which increases with the amount of data. 
Second, the index needs to occupy physical space. In addition to the data table occupying the data space, each index also occupies a certain amount of physical space. If a clustered index is to be established, the required space will be larger. 
Third, when adding, deleting and modifying the data in the table, the index should also be dynamically maintained, which reduces the speed of data maintenance.

Indexes are built on certain columns in a database table. Therefore, when creating an index, you should carefully consider which columns can be indexed and which columns cannot be created. In general, indexes should be created on these columns, for example:

On the column that often needs to be searched, the speed of the search can be accelerated; 
on the column as the primary key, the uniqueness of the column is enforced and the arrangement structure of the data in the organization table; 
on the column that is often used in the connection, these columns are mainly some Foreign keys, which can speed up joins; 
create an index on a column that often needs to be searched according to a range, because the index is already sorted, and its specified range is continuous; 
create an index on a column that often needs to be sorted, because the index is already sorted, In this way, the query can use the sorting of the index to speed up the sorting query time; 
create an index on the column that is often used in the WHERE clause to speed up the judgment of the condition.


Also, some columns should not be indexed. In general, these columns that should not be indexed have the following characteristics:

First, indexes should not be created on columns that are rarely used or referenced in queries. This is because, since these columns are rarely used, indexing or no indexing does not improve query speed. On the contrary, due to the addition of indexes, the maintenance speed of the system is reduced and the space requirement is increased. 
Second, indexes should not be added to columns with few data values. This is because because these columns have very few values, such as the gender column of the personnel table, in the query results, the data rows of the result set account for a large proportion of the data rows in the table, that is, the data that needs to be searched in the table The proportion of rows is large. Increasing the index does not significantly speed up the retrieval speed. 
Third, no indexes should be added to columns defined as text, image and bit data types. This is because the amount of data in these columns is either quite large or has very few values. 
Fourth, indexes should not be created when the modification performance is much greater than the retrieval performance. This is because modification performance and retrieval performance are contradictory. When increasing the index, the retrieval performance will be improved, but the modification performance will be reduced. When reducing the index, it will improve the modification performance and reduce the retrieval performance. Therefore, indexes should not be created when the modification performance is much greater than the retrieval performance.

Methods of Creating Indexes and Characteristics 
of Indexes Methods of 
Creating Indexes There are various methods of creating indexes, including methods of creating indexes directly and methods of creating indexes indirectly. Create an index directly, such as using the CREATE INDEX statement or using the Create Index Wizard, and create an index indirectly, such as when defining a primary key constraint or a unique key constraint in a table, an index is also created. Although both methods can create indexes, the specific content of creating indexes is different. 
Use the CREATE INDEX statement or use the Create Index Wizard to create an index. This is the most basic index creation method, and this method is the most flexible. You can customize the index to meet your needs. When creating an index in this way, you can use many options, such as specifying the fullness of data pages, sorting, sorting statistics, etc., which can optimize the index. Using this method, you can specify the type, uniqueness, and composition of the index, that is, you can create both a clustered index and a nonclustered index, you can create an index on one column, or you can create an index on both. Or create an index on more than two columns.

Indexes can also be created indirectly by defining primary key constraints or unique key constraints. A primary key constraint is a logic to maintain data integrity that restricts records in a table to have the same primary key record. When creating a primary key constraint, the system automatically creates a unique clustered index. Although, logically, the primary key constraint is an important structure, but in the physical structure, the structure corresponding to the primary key constraint is a unique clustered index. In other words, in the physical implementation, there is no primary key constraint, but only a unique clustered index. Similarly, when creating a unique key constraint, an index is also created, which is a unique non-clustered index. Therefore, when creating an index using constraints, the type and characteristics of the index are basically determined, and there is little room for customization by the user.

When defining a primary key or unique key constraint on a table, if the table already has a standard index created using the CREATE INDEX statement, the index created by the primary key constraint or the unique key constraint overwrites the previously created standard index. That is, indexes created with primary key constraints or unique key constraints take precedence over indexes created with the CREATE INDEX statement.

The feature index of the 
index has two features, namely the unique index and the compound index. 
A unique index guarantees that all data in the indexed column is unique and does not contain redundant data. If the table already has a primary key constraint or unique key constraint, then when the table is created or modified, SQL Server automatically creates a unique index. However, if uniqueness must be guaranteed, a primary key constraint or a unique key constraint should be created instead of a unique index. When creating a unique index, these rules should be carefully considered: when creating a primary key constraint or a unique key constraint on a table, SQL Server automatically creates a unique index; if the table already contains data, then when creating the index, SQL Server checks the redundancy of existing data in the table; whenever data is inserted with an insert statement or modified with a modify statement, SQL Server checks the redundancy of the data: if there are redundant values, then SQL Server cancels the statement's Execute, and return an error message; ensure that each row of data in the table has a unique value, which ensures that each entity can be uniquely identified; only create unique indexes on columns that can guarantee the integrity of the entity, for example, You cannot create a unique index on the Name column in the Persons table because people can have the same name.

A compound index is an index created on two or more columns. When searching, when two or more columns are used as a key value, it is best to create a compound index on these columns. When creating a composite index, these rules should be considered: Up to 16 columns can be combined into a single composite index, and the total length of the columns that make up the composite index cannot exceed 900 bytes, that is, the length of the composite column cannot be too long; In a compound index, all columns must come from the same table, and compound columns cannot be established across tables; in a compound index, the order of columns is very important, so the order of columns should be carefully arranged. In principle, it should be defined first The most unique column, such as an index on (COL1,COL2) is not the same as an index on (COL2,COL1) because the order of the columns of the two indexes is different; in order for the query optimizer to use a compound index, the query The WHERE clause in the statement must refer to the first column in the compound index; compound indexes are very useful when there are multiple key columns in the table; using compound indexes can improve query performance and reduce the number of indexes created in a table quantity.

Original text: https://blog.csdn.net/pang040328/article/details/4164874

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325691128&siteId=291194637