当前位置:网站首页>MySQL index

MySQL index

2022-07-06 01:58:00 Crazy Stuka

1. The concept of index

 An index is a sorted list , In this list are stored the value of the index and the physical address of the row containing the data of this value ( Be similar to c The linked list of language points to the memory address of data record through pointer ).

 After using the index, you can locate the data of a row without scanning the whole table , Instead, we first find the corresponding physical address of the row data through the index table, and then access the corresponding data , Therefore, it can speed up the query speed of the database .

 An index is like a catalogue of a book , You can quickly find the required content according to the page number in the table of contents .

 Index is the top method to sort the values of one or several columns in a table .

 The purpose of indexing is to speed up the search or sorting of records in a table .

2. The function of index

 After setting the appropriate index , The database uses a variety of fast positioning technologies , Can greatly speed up the query speed , This is the main reason to create indexes .

 When the table is large or the query involves multiple tables , Using indexes can improve query speed thousands of times .

 It can reduce the cost of the database IO cost , And the index can also reduce the sorting cost of the database .

 By creating a unique index , It can ensure the uniqueness of each row of data in the data table .

 You can speed up the connection between tables .

 When using grouping and sorting , Can greatly reduce the grouping and sorting time .

 Indexing can significantly improve performance when searching and recovering data in a database 

Side effects of indexing

 Index takes up extra disk space .

 about MyISAM In terms of engines , Index files and data files are separate , The address where the index file is used to hold the data record .
 and InnoDB The table data file of the engine itself is the index file .

 It takes more time to insert and modify data , Because the index has to change with it .

3. The principle of index creation is based on

 Although index can improve the speed of database query , But it's not always appropriate to create an index . Because the index itself consumes system resources , With an index , The database will perform index query first , Then navigate to the specific data row , If the index is not used properly , On the contrary, it will increase the burden of the database .

 Primary Key 、 Foreign key must have index . Because the primary key is unique , The foreign key is associated with the primary key of the primary table , You can quickly locate when querying .
 Number of records exceeds 300 The table of rows should have an index . If there is no index , Every query needs to traverse the table , Will seriously affect the performance of the database .

 A table that is often connected to other tables , Index should be established on the connection field .
 Fields with poor uniqueness are not suitable for indexing .
 Fields that are updated too frequently are not suitable for creating indexes .
 It often appears in  where Fields in clause , In particular, the fields of large tables , It should be indexed .
 On a regular basis GROUP BY、ORDER BY Index the fields of .
 Indexes should be built on fields with high selectivity .
 Indexes should be built on small fields , For large text fields or even extra long fields , Don't index .

4. Classification and creation of index

(1) General index : The most basic index type , There's no such thing as uniqueness .

Create index directly

create index  Index name  on  Table name ( Field )

 Insert picture description here

Modify tables to create

ALTER TABLE  Table name  ADD INDEX  Index name  ( Name );

 Insert picture description here

Specify index when creating table

CREATE TABLE  Table name  ( Field 1  data type , Field 2  data type [,...],INDEX  Index name  ( Name ));

 Insert picture description here

(2) unique index : Similar to a normal index , But the difference is that each value of a unique index column is unique . Null values are allowed for unique indexes ( Note that it's different from the primary key ). If it's a composite index , The combination of column values must be unique . Adding a unique key automatically creates a unique index .

Create a unique index directly

 Insert picture description here

Modify tables to create

ALTER TABLE  Table name  ADD UNIQUE  Index name  ( Name );

 Insert picture description here

When creating a table, specify

CREATE TABLE  Table name  ( Field 1  data type , Field 2  data type [,...],UNIQUE  Index name ( Name ));

(3) primary key : Is a special unique index , Must be specified as "PRIMARY KEY”. A table can only have one primary key , No null values are allowed . Adding a primary key will automatically create a primary key index .

Modify tables to create

ALTER TABLE  Table name  ADD PRIMARY KEY( Name )

 Insert picture description here

(4) Composite index :( Single column index and multi column index ): It can be an index created on a single column , It can also be indexes created on multiple columns . Need to meet The principle of left , because select Of the statement where The conditions are executed from left to right in turn , So it's using select Statement query where
The order of the fields used by the condition must be the same as the sort in the composite index , Otherwise the index will not take effect .

Modify the table structure to create

mysql> alter table member add index phone_addr_index (phone,address);

 Insert picture description here

(5) Full-text index

(FULLTEXT): It is suitable for fuzzy query , Can be used to retrieve text information in an article . stay MySQL5.6 Version before FULLTEXT The index can only be used for MyISAM engine , stay 5.6 After the version innodb The engine also supports FOLLTEXT Indexes . Full text indexing can be done in CHAR、VARCHAR、 perhaps TEXT Create on column of type . Only one full-text index per table is allowed .

Create index directly

mysql> create fulltext index remark_index on member(remark);

 Insert picture description here

Modify table structure and create index

 Insert picture description here

Fuzzy query instance

 Insert picture description here

Look at the index

 Insert picture description here

 Insert picture description here

The meaning of each field is as follows :

Table: The name of the table .

Non_unique: If the index can't include repetition , Then for 0; If possible , Then for 1.

Key_name:  Name of index .

Seq_in_index:  Column ordinal in index , from 1 Start .

column_name:  Column name .

collation: How columns are stored in indexes . stay  MySQL in , Valuable 'A′( Ascending ) or NULL( No classification ).

Cardinality:  An estimate of the number of unique values in an index .

Sub_part:  If the column is only partially indexed , Is the number of characters indexed . If the entire column is indexed , Then for  NuLL.

Packed: Indicates how keywords are compressed . If it's not compressed , Then for NULL.

Null: If the column contains NULL, It contains YES. without , The column contains NO.

Index_type: Used indexing methods (BTREE,FULLTEXT,HASH,RTREE).

Comment: remarks .

Delete index

Delete index directly

DROP INDEX  Index name  ON  Table name ;

 Insert picture description here

Modify the table to delete the index

ALTER TABLE  Table name  DROP INDEX  Index name ;

 Insert picture description here

Delete primary key index

alter table  Table name  drop primary key;

`

原网站

版权声明
本文为[Crazy Stuka]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140053181635.html