当前位置:网站首页>What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
What is an index in MySQL? What kinds of indexes are commonly used? Under what circumstances will the index fail?
2022-07-06 14:37:00 【Floating~】
Tips : interview baba One of the necessary questions !!!
Catalog
Two 、 Classification of indexes
2.1.3 primary key (primary key)
2.2.1 Full-text index (fulltext)
3、 ... and 、 The advantages and disadvantages of index
3.2 The disadvantages of indexing
Four 、 Application scenario of index
① The query criteria include or, Even if some conditions are indexed, they will fail .
④ Participate in the calculation on the index column .
⑤ Violate the leftmost matching principle .
⑥ When MySQL When full table scanning is faster than indexing .
Preface
In this age of big data , The huge amount of data requires efficient database operation technology , Index is an important object to improve the efficiency of database operation . We can understand it as “ Quickly find data structures in order ”.
This article will start from the definition of index , Classification of indexes , The advantages and disadvantages of index , Share your understanding of the index in four aspects, such as the application scenario of the index , For your reference only .
Student surface :
One 、 Definition of index
MySQL The official definition of : Indexes (index) Help MySQL Data structure for efficient data acquisition .
The essence : An index is a data structure . It is a sequential table that describes the one-to-one correspondence between the column values of index columns and the record rows in the meta table .
Conclusion : In addition to the data itself , The database also maintains a data structure that satisfies a specific search algorithm , These data structures point to data in some way , In this way, the advanced search algorithm can be realized on the basis of these data structures , This data structure is the index .
Be careful :
① Index affects where Later sorting and finding (oder by)
② Indexes are often stored on disk in the form of files .
Two 、 Classification of indexes
classification :
Single index : General index 、 unique index 、 primary key .
Multi column index : Full-text index .
2.1 Single index
An index contains only a single column , But you can have multiple single-column indexes in a table .
2.1.1 General index
Definition : The most basic index type , There is no limit to uniqueness .
-- Create a normal index
CREATE INDEX The index name ON Table name ( Field name );
CREATE INDEX stu_name on student(sname);
-- Modify the general index
ALTER TABLE Table name ADD INDEX The index name ( Field name );
ALTER TABLE student ADD INDEX sname(sname);
2.1.2 unique index (unique)
Definition : Rows with the same index value are not allowed , So as to represent duplicate indexes and key values .
-- Add unique index
CREATE UNIQUE INDEX The index name ON Table name ( Field name );
CREATE UNIQUE INDEX stu_id ON student(sid);
-- Modify the unique index
ALTER TABLE Table name ADD UNIQUE INDEX The index name ( Field name );
ALTER TABLE student ADD UNIQUE INDEX sid(sid);
2.1.3 primary key (primary key)
Definition : In the database diagram , Define a primary key , The primary key index will be created automatically .
-- Add primary key index
-- When creating a table
-- Modify the primary key index
ALTER TABLE Table name ADD PRIMARY KEY ( Field name );
ALTER TABLE student ADD PRIMARY KEY (sid);
2.2 Multi column index
Definition : An index created on a combination of fields in a table , Only when the left field of these fields is used in the query criteria , Indexes are used , Use a composite index to follow the leftmost prefix set .()
2.2.1 Full-text index (fulltext)
Definition : The key technology of index , Used to retrieve text information , In some words , Through the keywords or words in it , Find the record line to which it belongs .
-- Create full text index
CREATE FULLTEXT INDEX Full text index name ON Table name ( You want to set the field of full-text index ) WITH PARSER ngram;
CREATE FULLTEXT INDEX content ON student(ssex,sname) WITH PARSER ngram;
-- Modify the full-text index (with The latter representatives support Chinese )
ALTER TABLE Table name ADD FULLTEXT Full text index name ( You want to set the field of full-text index ) WITH PARSER ngram;
ALTER TABLE student ADD FULLTEXT studentcontent(ssex,sname) WITH PARSER ngram;
Full text index cannot be set for primary key !!!
3、 ... and 、 The advantages and disadvantages of index
3.1 Advantages of index
① Improve the efficiency of data retrieval , Reduce the IO cost .( University Libraries index )
② Sort data by index , Reduce sorting costs , Reduce CPU trumpet .
nature :
(1) Efficiency : Using index can improve the efficiency of database query .
(2) integrity : Users can accelerate the connection between meters , Achieve referential integrity between tables .
(3) Uniqueness : The index can ensure the uniqueness of the checked data .
(4) Special abilities : By using index , You can query in the process , Using the optimize hide tool , Improve system performance .
* Find and sort faster .
3.2 The disadvantages of indexing
① Creating and maintaining index groups takes time , And as the amount of data increases, so does the time spent .
② Index needs disk space , In addition to the data table taking up the data space , Each index takes up a certain amount of physical space . If there are a lot of indexes , Index files can reach the maximum file size faster than data files .
③ When adding data in the table 、 When deleting and modifying , Indexes should also be maintained dynamically , This reduces the speed of data maintenance .
* Insert (insert)、 modify (update)、 Delete (delete) slower .
Four 、 Application scenario of index
4.1 Suitable for indexing
First of all : On columns that often need to be searched , Can speed up the search ;
second : On the column as the primary key , Force the uniqueness of the column and organize the data in the table ;
Third : On columns that are often used in joins , These columns are mainly some foreign keys , Can speed up the connection ;
Fourth : Create indexes on columns that often need to be searched by range , Because the index is sorted , Its specified range is continuous ;
The fifth : Create indexes on columns that often need to be sorted , Because the index is sorted , In this way, the query can take advantage of index sorting , Speed up sorting query time ;
The sixth : It is often used in WHERE Create an index on the column in clause , Speed up the judgment of conditions .
To make a long story short , The fields often used for searching are added with indexes .
4.2 Index failure
Use student form ,sid Set as primary key index ,sname Set to normal index .Navicat in type For query type ,ALL Retrieve for full table .
① The query criteria include or, Even if some conditions are indexed, they will fail .
for example :
SELECT * FROM student WHERE Sid=1;
There are or When
SELECT * FROM student WHERE Sid=1 OR birthday="1900-01-01";
② like Query to % start .
for example :
SELECT * FROM student WHERE Sname=" Zhang San ";
In the query statement like With % start
SELECT * FROM student WHERE Sname LIKE"% 3、 ... and ";
③ If the column type is string , Quotation marks are not used to reference data , Leading to no index .
for example :
Did not hit the normal index , because 222 The string is not quoted .
SELECT * FROM student WHERE Sname=222;
④ Participate in the calculation on the index column .
for example :
Missed sid primary key , Because of the calculation .
SELECT * FROM student WHERE Sid-1=1;
⑤ Violate the leftmost matching principle .
for example :
because ssex The index is based on (ssex,sname) How to index the above , So for hit index .
SELECT * FROM student WHERE Ssex=" male ";
⑥ When MySQL When full table scanning is faster than indexing .
⑦ Other and upper index failure references Index failure is super detailed https://blog.csdn.net/sy_white/article/details/122112440?utm_source=app&app_version=5.3.0&code=app_1562916241&uLinkId=usr1mkqgl919blen
@ Finally, I wish everyone who asks , Everything is right !!!yyds
—— A Piao full of vitality
边栏推荐
- 循环队列(C语言)
- 数字电路基础(一)数制与码制
- Statistics, 8th Edition, Jia Junping, Chapter VIII, summary of knowledge points of hypothesis test and answers to exercises after class
- Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
- JDBC看这篇就够了
- SQL injection
- Statistics 8th Edition Jia Junping Chapter 12 summary of knowledge points of multiple linear regression and answers to exercises after class
- Pointers: maximum, minimum, and average
- 数据库多表链接的查询方式
- Function: string storage in reverse order
猜你喜欢
Statistics 8th Edition Jia Junping Chapter 5 probability and probability distribution
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
New version of postman flows [introductory teaching chapter 01 send request]
An unhandled exception occurred when C connected to SQL Server: system Argumentexception: "keyword not supported:" integrated
数据库多表链接的查询方式
JDBC看这篇就够了
Mysql的事务是什么?什么是脏读,什么是幻读?不可重复读?
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Web vulnerability - File Inclusion Vulnerability of file operation
《统计学》第八版贾俊平第七章知识点总结及课后习题答案
随机推荐
How to test whether an object is a proxy- How to test if an object is a Proxy?
captcha-killer验证码识别插件
指针 --按字符串相反次序输出其中的所有字符
servlet中 servlet context与 session与 request三个对象的常用方法和存放数据的作用域。
【指针】统计一字符串在另一个字符串中出现的次数
Fire! One day transferred to go engineer, not fire handstand sing Conquest (in serial)
MySQL中什么是索引?常用的索引有哪些种类?索引在什么情况下会失效?
浙大版《C语言程序设计实验与习题指导(第3版)》题目集
5 minutes to master machine learning iris logical regression classification
JDBC transactions, batch processing, and connection pooling (super detailed)
[pointer] find the length of the string
Internet Management (Information Collection)
The difference between layer 3 switch and router
数字电路基础(一)数制与码制
[pointer] solve the last person left
Interview Essentials: what is the mysterious framework asking?
Function: find the root of the equation by Newton iterative method
Hcip -- MPLS experiment
函数:计算字符串中大写字母的个数
Intranet information collection of Intranet penetration (4)