当前位置:网站首页>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
边栏推荐
- 《统计学》第八版贾俊平第十四章指数知识点总结及课后习题答案
- Statistics 8th Edition Jia Junping Chapter 7 Summary of knowledge points and answers to exercises after class
- Markdown font color editing teaching
- Record an edu, SQL injection practice
- 关于交换a和b的值的四种方法
- 函数:求方程的根
- 数字电路基础(一)数制与码制
- Statistics 8th Edition Jia Junping Chapter 10 summary of knowledge points of analysis of variance and answers to exercises after class
- Matplotlib绘图快速入门
- c语言学习总结(上)(更新中)
猜你喜欢
《统计学》第八版贾俊平第十章方差分析知识点总结及课后习题答案
Apache APIs IX has the risk of rewriting the x-real-ip header (cve-2022-24112)
图书管理系统
Attack and defense world misc practice area (simplerar, base64stego, no matter how high your Kung Fu is, you are afraid of kitchen knives)
Attack and defense world misc practice area (GIF lift table ext3)
《统计学》第八版贾俊平第八章假设检验知识点总结及课后习题答案
Based on authorized access, cross host, and permission allocation under sqlserver
《统计学》第八版贾俊平第四章总结及课后习题答案
Database monitoring SQL execution
《统计学》第八版贾俊平第二章课后习题及答案总结
随机推荐
数字电路基础(二)逻辑代数
Lintcode logo queries the two nearest saplings
JDBC看这篇就够了
Get started with Matplotlib drawing
指针:最大值、最小值和平均值
On the idea of vulnerability discovery
指针--剔除字符串中的所有数字
Intranet information collection of Intranet penetration (2)
C language file operation
JDBC read this article is enough
内网渗透之内网信息收集(二)
【指针】删除字符串s中的所有空格
How does SQLite count the data that meets another condition under the data that has been classified once
内网渗透之内网信息收集(四)
安全面试之XSS(跨站脚本攻击)
函数:求1-1/2+1/3-1/4+1/5-1/6+1/7-…+1/n
captcha-killer验证码识别插件
[pointer] solve the last person left
《統計學》第八版賈俊平第七章知識點總結及課後習題答案
Function: find the root of the equation by Newton iterative method