当前位置:网站首页>Mysql database explanation (IV)
Mysql database explanation (IV)
2022-06-26 13:18:00 【C chord~】
Catalog
Two . Classification and creation method of index
3、 ... and . Look at the index
2. How to modify the table to drop the index
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 :
- unique index
- primary key
- Composite index
- Text index
- General index
2. How indexes are created
- Create index directly ( The primary key index cannot be used in this way )
- Create an index when creating a table
- 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 :
- General index
- unique index
- primary key
- Composite index
- Text index
How indexes are created
- Create directly
- Create when modifying tables
- When creating a new table
边栏推荐
- Dark horse notes - Common APIs
- mysql讲解(一)
- C language: Exercise 2
- scrapy——爬取漫画自定义存储路径下载到本地
- 8. [STM32] timer (TIM) -- interrupt, PWM, input capture experiment (proficient in timer)
- Design of four kinds of linear phase FIR filters -- complete set of Matlab source code
- Electron official docs series: Contributing
- 中国剩余定理模板题 互质与非互质
- D - skiing
- Word document export (using fixed template)
猜你喜欢

IDC报告:百度智能云AI Cloud市场份额连续六次第一

组合模式(Composite )

Arcpy -- use of insertlayer() function: adding layers to map documents

mysql讲解(一)
![P5733 [deep foundation 6. example 1] automatic correction](/img/34/081dbd805593a92a86c3081d6772e3.png)
P5733 [deep foundation 6. example 1] automatic correction

Mode pont

May product upgrade observation station

10秒内完成火灾预警,百度智能云助力昆明官渡打造智慧城市新标杆

Beifu twincat3 can read and write CSV and txt files

外观模式(Facade)
随机推荐
Typescript
What are the common categories of software testing?
桥接模式(Bridge)
Copy multiple Excel files and name them different
Bifu divides EtherCAT module into multiple synchronization units for operation -- use of sync units
8、【STM32】定时器(TIM)——中断、PWM、输入捕获实验(一文精通定时器)
zoopeeper设置acl权限控制(只允许特定ip访问,加强安全)
MySQL数据库常见故障——遗忘数据库密码
倍福EtherCAT Xml描述文件更新和下载
HDU 3555 Bomb
Go structure method
偶言佳句,孤芳自赏
Arcpy——InsertLayer()函數的使用:摻入圖層到地圖文檔裏
map 取值
Electron official docs series: Distribution
I - Dollar Dayz
MySQL数据库讲解(六)
E - Apple Catching
OPLG: 新一代云原生可观测最佳实践
Enjoy element mode (flyweight)