当前位置:网站首页>[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 %'
边栏推荐
- What does openid mean? What does "token" mean?
- OpenFOAM:底层
- tp5与tp6的区别是啥呀?
- 开户券商怎么选择?在线开户是安全么?
- What are the indicators of DEA?
- atguigu----18-组件
- City Chain technology platform, really Realizing value Internet reconstruction!
- 城链科技平台,正在实现真正意义上的价值互联网重构!
- Is it safe to open a stock account online now?
- ¥3000 | 录「TBtools」视频,交个朋友&拿现金奖!
猜你喜欢
C language "Recursion Series": recursively realizing the n-th power of X
初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
《树莓派项目实战》第五节 使用Nokia 5110液晶屏显示Hello World
How to analyze the grey prediction model?
linux中的mysql有10061错误怎么解决
How to calculate the positive and negative ideal solution and the positive and negative ideal distance in TOPSIS method?
Summary of NLP data enhancement methods
各种同步学习笔记
(翻译)采用字母间距提高全大写文本可读性的方式
C language: count the number of characters, numbers and spaces
随机推荐
【总结】1361- package.json 与 package-lock.json 的关系
How to interpret the information weight index?
Object.defineProperty也能监听数组变化?
Unknown table 'column of MySQL_ STATISTICS‘ in information_ schema (1109)
城链科技平台,正在实现真正意义上的价值互联网重构!
故障:Outlook 收发邮件时的 0x800CCC1A 错误
ICer必须知道的35个网站
Getting to know the generation confrontation network (11) -- using pytoch to build wgan to generate handwritten digits
About i/o -- the relationship between memory and CPU and disk
Rank sum ratio (RSR) index calculation
Sharepoint:sharepoint 2013 with SP1 easy installation
C language "Recursion Series": recursively realizing the n-th power of X
Easyplayer streaming media player plays HLS video. Technical optimization of slow starting speed
InfluxDB时序数据库
The city chain technology platform is realizing the real value Internet reconstruction!
Retrieval model rough hnsw
Stimulsoft ultimate presents reports and dashboards
atguigu----01-脚手架
In Section 5 of bramble pie project practice, Nokia 5110 LCD is used to display Hello World
Iframe is simple to use, iframe is obtained, iframe element value is obtained, and iframe information of parent page is obtained