Can using indexes definitely improve efficiency?

An index is a data structure that is sorted and helps us find it quickly. Simply put, an index is a data structure that stores records in a database in a special form. Through indexing, the efficiency of data query can be significantly improved, thereby improving the performance of the server.

Advantages and disadvantages of indexes

Advantages:
(1) Improve the efficiency of data retrieval and reduce the IO cost of the database
(2) Sorting data through indexes reduces the cost of data sorting and CPU consumption

Disadvantages:
(1) It takes time to create and maintain indexes, and this time increases with the increase of data volume
(2) Indexes require physical space. In addition to the data space occupied by data tables, each index also occupies a certain amount of space. Physical space
(3) When adding, deleting and modifying the data in the table, the index also needs dynamic maintenance, which reduces the speed of data maintenance.

Principles for creating indexes

(1) Create an index on the column that needs to be searched frequently, which can speed up the search
(2) Create an index on the column that is used as the primary key to enforce the uniqueness of the column and the arrangement structure of the data in the table
(3) It is often used in On the connected columns, these columns are mainly some foreign keys, which can speed up the connection
(4) Create indexes on the columns that often need to be searched according to the range, because the index has been sorted, and the specified range is continuous
(5) in Create indexes on the columns that often need to be sorted, because the indexes are already sorted, so that queries can use the sorting of the indexes to speed up the sorting query time
(6) Create indexes on the columns that are often used in the WHERE clause to speed up condition judgment.

What are the types of indexes?

(1) Ordinary index
This is the most basic index type, an index created based on ordinary fields, without any restrictions.
(2) Unique index
Similar to ordinary index, the difference is that the value of the index field must be unique, but null values ​​are allowed
(3) Primary key index
It is a special unique index that does not allow null values. Add primary key constraints when creating or modifying tables, and each table can only have one primary key.
(4) Composite index
Users can create an index on multiple columns. This type of index is called a composite index (combined index). Composite indexes can replace multiple single indexes, and require less overhead than multiple single-index composite indexes.

Guess you like

Origin blog.csdn.net/weixin_49131718/article/details/131647772