Mysql index [Summary]

1. Index: A technology to improve query efficiency in the database when indexing, similar to a dictionary directory;

1. Why use indexes?

If the index is not used, the data will be scattered in each disk block. When querying the data, you need to traverse each disk block one by one to find the data. If the amount of data is very large, traversing each block of data is also a very time-consuming task. , After adding an index, the disk blocks will be saved in a tree structure, and some disk blocks will be accessed purposely when querying data. Because the number of disk blocks accessed is reduced, it can play a role in improving query efficiency;

2. Is the more indexes the better?

No, because the index will take up disk space, the index created through a certain field may never be used, then this index has no meaning at all, only need to create an index on the frequently used field in the query.

3. Is it necessary to have an index?

Not necessarily, if the amount of data is small, using indexes will reduce query efficiency.

4. Classification of index:

  • Clustered index (clustered index): The index created by the primary key is a clustered index. Tables with primary key constraints will automatically add a clustered index, and the data is stored in the tree structure of the clustered index.
  • Non-clustered index: An index added through a non-primary key field is called a non-clustered index. Only the address of the disk block where the data is stored is stored in the tree structure, and there is no data.

5. The format of creating an index:create index 索引名 on 表名(字段名[长度]);

create index i_item on item(title);

6. View the index:show index from 表名;

show index from item;

7. Delete the index:drop index 索引名 on 表名;

drop index i_item on item;

8. Composite index: An index created by multiple fields is called a composite index.

format:create index 索引名 on 表名(字段一,字段二);

When multiple fields are frequently used for data query, a composite index can be created in order to improve query efficiency;

2. Index summary:

  1. Indexes are suitable for technologies that improve query efficiency, similar to directories;
  2. The index will take up disk space, not the more the better;
  3. If the amount of data is small, the index will reduce query efficiency;
  4. Do not add indexes on tables that are frequently changed.

Guess you like

Origin blog.csdn.net/weixin_44296929/article/details/108666488