当前位置:网站首页>Creation and use of MySQL index
Creation and use of MySQL index
2022-06-25 04:56:00 【xxxibolva】
Catalog
6、 ... and 、 Efficient indexing policy
One 、 Index basis
- Index Introduction
Database index is like Pinyin when looking up a Chinese dictionary 、 Radicals
An index is a page that stores the primary key and index fields Orderly surface , And points to the record of the entity table
Indexes are divided into single column indexes and composite indexes , Single index —— An index contains one column ; Composite index —— An index contains multiple columns
Multiple single column indexes of a table ≠ Composite index
- advantage
Improve query speed
- shortcoming
Index files that take up disk space
Data operation statement ( increase INSERT、 Delete DELETE、 Change UPDATE) The processing time will be longer , That is, reduce the update speed of the table
Two 、 Scenarios for indexing
1. The amount of data exceeds 300 The table of should have an index
2. A table that is often connected to other tables , An index should be established on the connection field
3、 ... and 、 Create index
grammar :[UNIQUE|FULLTEXT] INDEX|KEY [index_Name](column_Name[(length)] [ASC|DESC]) [USING Index method ]
INDEX and KEY It's the same thing
There is only one single column index column_Name; There are multiple composite indexes column_Name, commas , Use the leftmost matching principle
The index method defaults to B+TREE
Left most matching principle :cloumn_Name by (a,b,c) when ,WHERE You can query (a) or (a,b) or (a,b,c) or (a,c)
1. General index
Is the most basic index , No restrictions , Index values can appear multiple times
- Create index on table creation
grammar :INDEX [index_Name] (column_Name)
CREATE TABLE student
(Sno char(5),
Sname varchar(20) not null,
INDEX st_name (Sname));
st_name For index name , If the user does not specify , be MySQL The index name will be automatically specified
The first 4 Lines can be replaced with
KEY st_name (Sname)
INDEX (Sname)
KEY (Sname)- Create index after creating table
grammar :CREATE INDEX index_Name ON table_Name(column_Name);
- Create index when modifying table
grammar :ALTER TABLE table_Name ADD INDEX [index_Name] (column_Name);
2. unique index
A unique index is a unique key , The value of a unique index column must be unique , It can be for NULL, And There can be multiple NULL
If it's a composite index , The combination of column values must be unique
- Create index on table creation
grammar :UNIQUE [index_Name] (column_Name)
- Create index after creating table
grammar :CREATE UNIQUE INDEX [index_Name] ON table_Name(column_Name);
- Create index when modifying table
grammar :ALTER TABLE table_Name ADD UNIQUE [index_Name] (column_Name);
3. primary key
The primary key index does not need to be added manually , The primary key index is automatically created when the primary key is created , So a table can only have one primary key index
Index values must be unique , And Not for NULL
grammar : take UNIQUE INDEX Change it to PRIMARY KEY
4. Full-text index
Use... In search engines ,MySQL Chinese full text index is not supported , adopt sphinx To do Chinese full-text index
grammar : take UNIQUE Change it to FULLTEXT
Four 、 Delete and view index
1. Delete index
grammar :DROP INDEX [index_Name] ON table_Name;
Use ALTER
grammar :ALTER TABLE table_Name DROP INDEX index_Name;
ALTER TABLE table_Name DROP PRIMARY KEY;
2. Show index information
SHOW INDEX List index information in the table
\G Format output
grammar :SHOW INDEX FROM table_Name [\G];
3. Query whether the index is used
grammar :EXPLAIN Query statement \G;
explain Meaning of output information
key: The final index evaluated by the optimizer ( The query does not use an index NULL)
Extra: Additional information on
/****Extra Four possibilities ****/
(1)using index: It means that the query requirements can be met by overwriting the index , No need to go back to the table , Therefore, the efficiency is higher
(2)using index;using where: First, the storage engine returns the search results through index retrieval ( There is still no need to go back to the table ), And then in Server The layer passes through where Statement to filter the search results . This filter does not need to be returned to the table , Therefore, the efficiency is also very high
(3)using where: Express MySQL The results extracted by the storage engine layer will be filtered , The filter condition field has no index .using where Itself has nothing to do with whether to use indexes
(4)using index condition: yes MySQL 5.6 A new feature introduced in , Applicable to secondary indexes only , It usually occurs in the scenario where the query field cannot be overwritten by the secondary index , In this scenario, you often need to go back to the table . adopt ICP, It can reduce the row records returned by the storage engine , This reduces IO operation
For more information, please take a look https://blog.csdn.net/Saintyyu/article/details/99694649
/*use index*/
explain
select Sname
from student
where Sname='Li Zhang' and Sage=19 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: ref
possible_keys: st_info
key: st_info
key_len: 82
ref: const
rows: 1
filtered: 20.00
Extra: Using where; Using index
1 row in set, 1 warning (0.00 sec)
/*don't use index*/
explain
select *
from student
where Ssex=1 and Sage=19 \G;
*************************** 1. row ***************************
id: 1
select_type: SIMPLE
table: student
partitions: NULL
type: ALL
possible_keys: NULL
key: NULL
key_len: NULL
ref: NULL
rows: 5
filtered: 20.00
Extra: Using where
1 row in set, 2 warnings (0.00 sec)
5、 ... and 、 Use index
/*create table*/
create table student
(Sno char(5),
Sname varchar(20) not null,
Ssex char(2),
Sage tinyint,
Sdept varchar(10),
primary key (Sno));
/*insert data*/
/*create index*/
create index st_name on student(Sname,Ssex,Sage);
/*show index*/
show index from student \G;
/*use index*/
select *
from student
where Sname='he Liu' and Ssex=0;
/*drop index*/
drop index st_name on student;
6、 ... and 、 Efficient indexing policy
1. Hash index and prefix index
Sometimes you need to index long character columns , This will increase the storage space of the index and reduce the efficiency of the index , You can use hash index or prefix index to make the index more efficient
- Hash index
hash Index just hash Value order , It has nothing to do with table data , Cannot be applied to order by
hash The index can only be used to compare queries = or IN, Invalid other query range , The essence is that no data is stored
- Prefix index
The prefix index is the prefix of the selected character column n Characters as index , This can greatly save index space , To improve index efficiency
MySQL Cannot use prefix index to do ORDER BY、GROUP BY And using prefix index to do coverage scanning
2. Select the appropriate index column order
3. Clustered index and nonclustered index
- Clustered index
- Nonclustered indexes
Unlike a clustered index, a nonclustered index does not determine the physical ordering of data on disk , And in B-Tree Contains indexes but no row data , Row data is simply saved in B-Tree The corresponding pointer of the index in to point to the row data , Such as : It's up there (Sname,Ssex,Sage) An index built on is a nonclustered index .
4. Overlay query
If an index contains the values of all the fields to be queried , It is called overlay index , Overwriting indexes can greatly improve access performance
Such as :select Sname,Sage from student where Sname='he Liu';
Fields to query (Sname,Sage) Are contained in the index columns of the composite index (Sname,Ssex,Sage) in
Reference material
2.mysql Novice introduction to index
3.MySQL Creation and use of index
4.MySql Detailed introduction and correct use of the index
5.MySQL in explain Field meaning and extra Detailed explanation
边栏推荐
- DMA double buffer mode of stm32
- TeeChart Pro ActiveX 2022.1
- What is Ethernet and how to connect the computer
- Rce code execution & command execution (V)
- 【图像融合】基于matlab方向离散余弦变换和主成分分析图像融合【含Matlab源码 1907期】
- Difference between asemi high power FET and triode
- Qdebug June 2022
- 【FLink】access closed classloader classloader. check-leaked-classloader
- SQL lab range explanation
- SOC验证环境的启动方式
猜你喜欢

Laravel's little knowledge

台式电脑连不上wifi怎么办

XSS (cross site script attack) summary (II)

"Daily practice, happy water" 1108 IP address invalidation

Heavy broadcast | phase shift method + mathematical principle derivation of multi frequency heterodyne + implementation

MySQL concept and operation (III)

Ctfhub eggs

成功解决:selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from
Triangle class (construction and deconstruction)

WPF 使用 MAUI 的自绘制逻辑
随机推荐
epplus复制模板后打印区域变小的问题
本轮压力测试下,DeFi协议们表现如何?
ORA-00800: soft external error
MySQL concept and operation (III)
Excel exports data to SQL and pictures to folder through macro | VBA
The SQL response is slow. What are your troubleshooting ideas?
Attack and defense world web baby Web
Penetration information collection steps (simplified version)
高效的NoSQL数据库服务Amozon DynamoDB体验分享
Apache+php uploading large files
CSRF (Cross Site Request Forgery) &ssrf (server request forgery) (IV)
【图像融合】基于matlab方向离散余弦变换和主成分分析图像融合【含Matlab源码 1907期】
Integrate CDN to create the ultimate service experience for customers!
cannot import name ‘escape’ from ‘jinja2’【成功解决】
Difference between asemi high power FET and triode
[keil] GPIO output macro definition of aducm4050 official library
Response (XI)
Web3 DAPP user experience best practices
Kotlin compose listens to the soft keyboard and clicks enter to submit the event
How PHP gets the user's City