当前位置:网站首页>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

Preface

One 、 Definition of index

Two 、 Classification of indexes

  2.1 Single index

2.1.1 General index

2.1.2 unique index (unique)

2.1.3 primary key (primary key)

  2.2 Multi column index

2.2.1  Full-text index (fulltext)

3、 ... and 、 The advantages and disadvantages of index

  3.1 Advantages of index

  3.2  The disadvantages of indexing

Four 、 Application scenario of index

  4.1 Suitable for indexing

  4.2  Index failure

① The query criteria include or, Even if some conditions are indexed, they will fail .

② like Query to % start .

③ If the column type is string , Quotation marks are not used to reference data , Leading to no index .

④ 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 icon-default.png?t=M4ADhttps://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

原网站

版权声明
本文为[Floating~]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060918449089.html