当前位置:网站首页>MySQL index
MySQL index
2022-06-30 05:53:00 【Sxiny Xin】
Catalog
One 、MySQL Indexes
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 a way to sort the values of one or more columns in a table .
The purpose of indexing is to speed up the search or sorting of records in a table .
2、 Functions and disadvantages of index
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 .
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 .
3、 The principle of index creation is based on
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 .
4、 Classification and creation of index
General index
The most basic index type , There's no such thing as uniqueness .
create index Index name on Table name ( Name [(length)]);

show create table xy01; # View table structure

Modify table mode to create index
alter table Table name add index Index name ( Name );
alter table xy01 add index cardid_index (cardid);


Set index when creating table
create table Table name ( Field 1 data type ,... Field n, data type ,index Index name ( Name ));
create table xy03 (id int(5) not null,name char(10),num varchar(20),index id_index (id));

unique index
Similar to a normal index , But the difference is that each value of a column with a unique index 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 , Add unique
Key will automatically create a unique index .
create unique index Index name on Table name ( Name );
create unique index name_index on xy03 (name);

Modify tables to create
alter table Table name add unique Index name ( Name );
alter table xy03 add unique num_index (num);

Specify a unique index when creating
create table Table name ( Field 1 data type ,... Field n data type ,unique Index name ( Name ));
create table xy04 (id int not null primary key,name char(10),hobby varchar(20),unique name_index (name));

primary key
Primary key index 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
When creating a table, specify
create table Table name ( Field 1 data type ,... Field n data type ,primary key ( Field name ));
create table xy05 (fruits char(10) not null primary key,price float(4));
or
create table xy05 (fruits char(10) not null,price float(4),primary key (fruits));

Create a primary key index by modifying a table
alter table Table name add primary key ( Name );

Composite index
A composite index can be an index created on a single column , It can also be indexes created on multiple columns . 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 .
Specify the index group when creating the table
create table Table name ( Name 1 data type ,... Name n data type ,index Index name ( Name 1,..., Name n));
create table xy06 (id int,name char,tel int,hobby char,sex char,index xy_index (name,sex));

Create index groups by modifying tables
alter table xy04 add index xy_index (name,hobby);

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 .
Create index directly
create fulltext index Index name on Table name ( Name );

Create a full-text index by modifying the table method
alter table Table name add fulltext index Index name ( Name );

Use full text index queries
select * from Table name where match( Name ) against(' Query content ');
Look at the index
show index from Table name ;
show keys from Table name ;

Parameter meaning :
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
lndex_type: Used indexing methods (BTREE,FULLTEXT,HASH,RTREE)
comment: remarks
5 Delete index
Delete index directly
drop index Index name on Table name 
Modify the table to delete the index 
Delete primary key index
alter table xy02 drop primary key;
边栏推荐
- Rotating frame target detection mmrotate v0.3.1 learning configuration
- Xctf attack and defense world crypto advanced area
- Shopping list--
- Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
- 炒股用指南针开户交易安全吗?
- Database SQL language 04 subquery and grouping function
- InputStream转InputStreamSource
- luoguP2756 飞行员配对方案问题(最大流)
- Summary of redis learning notes (I)
- 86. separate linked list
猜你喜欢

About modifying dual system default startup item settings

1380. lucky numbers in matrices

At the age of 32, I fell into a middle-aged crisis and finally quit naked...

What are membrane stress and membrane strain

动态规划--怪盗基德的滑翔翼

STM32F103 series controlled OLED IIC 4-pin

Do you know how to show the health code in only 2 steps

Switch to software testing and report to the training class for 3 months. It's a high paying job. Is it reliable?

MySQL存储系统

强烈推荐十几款IDEA开发必备的插件
随机推荐
Database SQL language 06 single line function
二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
Leader: who can use redis expired monitoring to close orders and get out of here!
09- [istio] istio service entry
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
D. Big Brush
Database SQL language 04 subquery and grouping function
抓取手机端变体组合思路设想
Online assignment of C language program design in the 22nd spring of Western Polytechnic University
Related applications of priority queue
Huxiaochun came to fengshu electronics to sign a strategic cooperation agreement with Zoomlion
Who is promoting the new inflection point of audio and video industry in 2022?
Official win 10 image download
We strongly recommend more than a dozen necessary plug-ins for idea development
At the age of 32, I fell into a middle-aged crisis and finally quit naked...
【板栗糖GIS】global mapper—如何把栅格的高程值赋予给点
Golden code of programmer interview
How does WPS cancel automatic numbering? Four options
leetcode763. Divide letter interval
STM32F103 series controlled OLED IIC 4-pin