当前位置:网站首页>[MySQL] understanding and use of indexes

[MySQL] understanding and use of indexes

2022-06-25 08:45:00 Xiao AI ~

1. What is index

​ When the amount of data and access is small ,MySQL Is very fast , Index has little effect on access . But when data and traffic soar , You will find MySQL Slow down , even to the extent that down fall , This must be optimized SQL 了 . Set up correct and reasonable index for database , yes MySQL An important means of optimization .

​ The purpose of indexing is to improve query efficiency , It can be compared to a dictionary , If you want to check "mysql" The word , We definitely need to position m Letter , Then find the letters from front to back y, And find the rest "sql". If there is no index , Then you may need to look through all the words to find what you want . Out of the dictionary , Index examples can be found everywhere in life , Like the train schedule at the railway station 、 A catalogue of books, etc . They all work the same way , By constantly narrowing the range of data you want to obtain, you can filter out the desired results , At the same time, it turns random time into sequential time , That is, we always lock the data through a search method .

​ When the index is created , You need to consider which columns will be used for SQL Inquire about , Then create one or more indexes for these columns . in fact , Index is also a kind of table , Save primary key or index field , And a pointer that can only think of each record as an actual table ( Nonclustered index and clustered index , An address where field values are stored in the index , A saved field is the value of the field ). Database users can't see the index , They're just used to speed up queries . Database search engines use indexes to locate records quickly .

​ INSERT and UPDATE Statement will take more time to execute in the table with index , and select Statements execute faster , This is because when inserting and updating , The database also needs to insert or update index values .

2. Index creation

2.1 Index type

  • UNIQUE ( unique index ): The same value cannot occur , There can be NULL value ;
  • INDEX ( General index ): Allow the same index content ;
  • PRIMARY KEY ( primary key ): The same value... Is not allowed ;
  • FULLTEXT INDEX ( Full-text index ): For a word in the value , but Efficiency is really not flattering ;
  • Composite index : In essence, it is to build multiple fields into one index , Column value combinations must be unique

2.2 Several ways to add indexes

Use ALTER Statement to add an index to a table

ALTER TABLE Apply to data table creation before adding

--ALTER TABLE  Table name  ADD  Index type   Index name ( Field name )
ALTER TABLE tab_identity ADD UNIQUE index_test (name);

Use create Statement to add an index to a table

CREATE INDEX Can be used to add General index and UNIQUE Indexes , Can be used to create indexes when creating tables .

-- create  Index type   The index name  on  Table name ( Field ) 
create INDEX iden_index on tab_identity(name)

3. The deletion of the index

Delete index can be used ALTER TABLE or DROP INDEX Statement to implement .DROP INDEX Can be in ALTER TABLE Internal processing as a statement , The format is as follows :

drop index index_name on table_name;  --  Normal and unique indexes can be deleted in this way , The primary key index cannot be deleted in this way 

alter table table_name drop index index_name; --  The primary key index cannot be deleted in this way 

alter table table_name drop primary key; --  You can delete the primary key index , The premise is that the primary key is not self incremented  auto_increment, If there is auto increment, it cannot be deleted , You need to delete the auto increment first 

4. Index failure

4.1 The index will not contain NULL value

As long as the column contains NULL value , Will not be included in the index , Only one column in the compliance index contains NULL value , So this column is invalid for this composite index .

4.2 Index column sort

MySQL The query uses only one index , So if where If index has been used in clause , that order by The columns in will not use indexes . So the database default sorting can meet the requirements of the case do not use sorting operations , Try not to include sorting of multiple columns , It's best to composite index these columns if you need to .

4.3 like Statement operation

In general, the use of like operation , If you have to use it , Pay attention to the right way of using .like '%aaa%' No index , and like ‘aaa%’ You can use index .

4.4 Don't use NOT IN、<>、!= operation , however <、<=、=、>、>=、BETWEEN、IN Index can be used

4.5 Index should be built on a regular basis select On the field of operation

This is because , If these columns are easy to use , So index does not significantly change the query speed . contrary , With the addition of index , On the contrary, it reduces the maintenance speed of the system and increases the space demand .

4.6 The index should be based on the unique field of value comparison

4.7 For those defined as text、image、bit Data types should not be indexed . Because the data in these columns is either quite large , Or the value is very small

4.8 stay where and join The columns that appear in need to be indexed

4.9 where There are unequal signs in the query conditions of (where column !=…),MySQL Index will not be available

4.10 If where The function is used in the query condition of clause ( Such as :where DAY(column)=…),MySQL Index will not be available

4.11 stay join In operation ( When you need to extract data from multiple data tables ),MySQL Only when the primary key and external inspection data types are the same can the index be used , Otherwise, even if the index is established, it will not be used

5. Look at the index


--  View all indexes in the table 
show KEYS FROM tab_identity;

--  See if the index is used 
explain SELECT * FROM tab_identity where name LIKE ' thank %'

原网站

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