当前位置:网站首页>Mysql database explanation (IV)

Mysql database explanation (IV)

2022-06-26 13:18:00 C chord~

Catalog

introduction

One . Indexes

1. Concept

2. advantage

3. shortcoming

4. Rules for creating indexes

Two . Classification and creation method of index

1. classification

2. How indexes are created

2.1 General index

2.2 unique index

2.3 primary key

2.4 Composite index

2.5 Full-text index

3、 ... and . Look at the index

Four . Delete index

1. Delete index directly

2. How to modify the table to drop the index

3. Delete primary key index

summary


introduction

In the process of enterprise informatization , The data volume of tables in the database is getting larger and larger 、 Performance can degrade dramatically , Creating indexes is critical to maintaining good performance . Indexing is the most effective way to optimize query performance , Can easily improve query performance by several orders of magnitude .

One . Indexes

1. Concept

  • 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 ( The index is built in the case of many contents , If there is less content, indexing is a waste of resources )

2. advantage

  • 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 for creating all the .
  •   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 .

3. shortcoming

  • 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

4. Rules for creating indexes

  • Indexing can speed up database queries , 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 child 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 , You need 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 .
  •   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 .
     

Two . Classification and creation method of index

1. classification

The index can be divided into 5 class :

  1. unique index
  2. primary key
  3. Composite index
  4. Text index
  5. General index

2. How indexes are created

  1. Create index directly ( The primary key index cannot be used in this way )
  2. Create an index when creating a table
  3. Create an index when modifying a table

2.1 General index

The most basic index type , There's no such thing as uniqueness

How it was created

1. Create directly 
create index  Index name  on  Table name  ( Name [(length)]);

( Name [(length)]:length  For optional , If you ignore  length  Value , Then use the value of the entire column as the index .
                 If you specify to use the front of the column  length  Characters to create the index , This helps to reduce the size of the index file .
 The index name is suggested to be  “index”  ending .
2. Create by modifying the table 
alter table  Table name  add index  Index name  ( Name );
3. When creating a table 
create table  Table name  ( Field 1  data type , Field 2  data type ,index  Index name  ( Name ));

2.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
  •   Adding a unique key automatically creates a unique index

How it was created

1. Create directly 
create unique index  Index name  on  Table name  ( Name );
2. Create when modifying a table 
alter table  Table name  add unique  Index name  ( Name );
3. When you create a table, create 
create table  Table name  ( Field 1  data type , Field 2  data type ,unique  Index name  ( Name ));

2.3 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

How it was created

1. When creating a table, create 
create table  Table name  ( Field 1  data type , Field 2  data type ,primary key ( Name ));
2 Create when modifying tables 
alter table  Table name  add primary key ( Name );

2.4 Composite index

  • We need to meet the leftmost principle , 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 .

How it was created

1. When creating a table, create 
create table  Table name  ( Field 1  data type , Field 2  data type ,index  Index name  ( Name 1, Name 2));
select * from  Table name  where  Name 1='...' and(or)  Name 2='...'
2. Create when modifying tables 
create table  Table name  ( Field 1  data type , Field 2  data type );
alter table  Table name  add primary key ( Name );

2.5 Full-text index

  • 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 fulltext 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 .
     

How it was created

1. Directly establish 
create fulltext index  Index name  on  Table name  ( Name );
2. Create modification table 
alter table  Table name  add fulltext  Index name  ( Name );
3. When creating a table, create 
create table  Table name  ( Field 1  data type , Field 2  data type ,fulltext  Index name  ( Name ));

3、 ... and . Look at the index

show index from  Table name ;
show index from  Table name \G;  Display table index information vertically 
show keys from  Table name ;
show keys from  Table name \G;
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 .

Four . Delete index

1. Delete index directly

drop index  Index name  on  Table name ;

2. How to modify the table to drop the index

alter table  Table name  drop index  Index name ;

3. Delete primary key index

alter table  Table name  drop primary key;

summary

  • The index needs to be built when there is a large amount of data , Otherwise, it is a waste of resources
  • Fields with poor uniqueness are not suitable for indexing .
  •   Fields that are updated too frequently are not suitable for creating indexes .

Classification of indexes :

  1. General index
  2. unique index
  3. primary key
  4. Composite index
  5. Text index

How indexes are created

  1. Create directly
  2. Create when modifying tables
  3. When creating a new table

原网站

版权声明
本文为[C chord~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206261227385002.html