当前位置:网站首页>MySQL index
MySQL index
2022-06-23 06:53:00 【Small code 2016】
Reference resources :https://www.cnblogs.com/luyucheng/p/6289714.html
Concept
Index is help mysql Data structure for efficient data acquisition
That is, the essence of index is data structure
It can also be understood as a well ordered quick lookup data structure
Data structures that satisfy specific search algorithms , These data structures are referenced in some way ( Point to ) data , In this way, advanced search algorithms can be implemented on these data . This data structure is the index
The usual index , If not specified , All refer to B Trees ( Multiple search trees , It's not necessarily a binary tree ), Where the clustered index , Secondary index , Overlay index , Composite index , Prefix index , By default, the only indexes are B+ Tree index , Collectively, index , except B+ Trees , And the Hashi index
Advantages and disadvantages
advantage : Can reduce the IO Cost and CPU Consumption of
Inferiority : Reduced update speed , Because the index must be updated when the data is updated
classification
- General index
- unique index
- primary key
- Composite index
- Full-text index
grammar
# establish
CREATE TABLE table_name[col_name data type]
[unique|fulltext][index|key][index_name](col_name[length])[asc|desc]
1.unique|fulltext Is an optional parameter , Each represents a unique index 、 Full-text index
2.index and key For synonyms , Both have the same effect , Used to specify index creation
3.col_name For the field columns that need to be indexed , The column must be selected from multiple columns of the definition in the data table
4.index_name Specify the name of the index , Is an optional parameter , If you don't specify , Default col_name Is the index value
5.length Is an optional parameter , Indicates the length of the index , Only fields of string type can specify index length
6.asc or desc Specifies whether index values in ascending or descending order are stored
# Delete
drop index [indexName] on tableName;
# see
show index from tableName;
General index
Is the most basic index , It has no restrictions . There are several ways to create it :
# 1.
CREATE INDEX index_name ON table(column(length))
# 2.
ALTER TABLE table_name ADD INDEX index_name ON (column(length))
# 3.
CREATE TABLE `table` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`title` char(255) CHARACTER NOT NULL ,
`content` text CHARACTER NULL ,
`time` int(10) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
INDEX index_name (title(length))
)
unique index
Similar to the previous normal index , The difference is that : The value of the index column must be unique , But you can have an empty value . If it's a composite index , The combination of column values must be unique . There are several ways to create it :
# 1.
CREATE UNIQUE INDEX indexName ON table(column(length))
# 2.
ALTER TABLE table_name ADD UNIQUE indexName ON (column(length))
# 3.
CREATE TABLE `table` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`title` char(255) CHARACTER NOT NULL ,
`content` text CHARACTER NULL ,
`time` int(10) NULL DEFAULT NULL ,
UNIQUE indexName (title(length))
);.
primary key
Is a special unique index , A table can only have one primary key , No null values are allowed . It is common to create a primary key index while building a table :
CREATE TABLE `table` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`title` char(255) NOT NULL ,
PRIMARY KEY (`id`)
);
Composite index
An index created on multiple fields , Only the first field that created the index is used in the query criteria , Indexes are used . When using a composite index, follow the leftmost prefix set
ALTER TABLE `table` ADD INDEX name_city_age (name,city,age);
Full-text index
Mainly used to find keywords in text , Rather than directly comparing the values in the index .fulltext Index is very different from other indexes , It's more like a search engine , Not simply where The parameters of the statement match .fulltext Index match match against Operation and use , Not the average where Statement plus like. It can be create table,alter table ,create index Use , But at present, there is only char、varchar,text Full-text indexes can be created on columns . It is worth mentioning that , When the amount of data is large , Now put the data into a table without a global index , And then use CREATE index establish fulltext Indexes , It's better than building a table first fulltext And then write data much faster .
# 1.
CREATE FULLTEXT INDEX index_content ON article(content)
# 2.
ALTER TABLE article ADD FULLTEXT index_content(content)
# 2.
CREATE TABLE `table` (
`id` int(11) NOT NULL AUTO_INCREMENT ,
`title` char(255) CHARACTER NOT NULL ,
`content` text CHARACTER NULL ,
`time` int(10) NULL DEFAULT NULL ,
PRIMARY KEY (`id`),
FULLTEXT (content)
);
matters needing attention
When using indexes , Here are some tips and precautions :
- The index will not contain null Columns of values
As long as the column contains null Values will not be included in the index , Only one column in a composite index contains null value , So this column is invalid for this composite index . So we should not let the default value of the field be null. - Use short index
Index series Columns , If possible, you should specify a prefix length . for example , If there is a char(255) The column of , If in front of 10 Or 20 Within a character , Multiple values are unique , So don't index the entire column . Short index can not only improve query speed but also save disk space and I/O operation . - Index column sort
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 , If you need to create a composite index for these columns . - like Statement operation
In general, it is not recommended to use like operation , If necessary , How to use it is also a problem .like “%aaa%” Index will not be used and like “aaa%” You can use index . - Don't operate on Columns
This will cause the index to fail and perform a full table scan , for example
SELECT * FROM table_name WHERE YEAR(column_name)<2017;
- Don't use not in and <> operation
边栏推荐
猜你喜欢
随机推荐
haas506 2.0开发教程-高级组件库-modem.voiceCall(仅支持2.2以上版本)
Topic35——34. 在排序数组中查找元素的第一个和最后一个位置
C language stepping on the pit: document coding error, resulting in Base64 Chinese coding error
C language obtains second, millisecond, subtle and nanosecond timestamps
从 WAN 到 SD-WAN 边缘设备的网络架构
mars3d点线面的绘制和重置
Machine learning artifact scikit learn minimalist tutorial
core.js是什么---kalrry
Network architecture from Wan to sd-wan edge devices
图解三次握手四次挥手,小白都能看懂
Leetcode notes: Weekly contest 298
XShell7 下载
English grammar_ Adjective comparative - Level 3 change
C language removes line breaks (or other characters) at the end of strings
mysql 优化
English语法_副词 - ever / once
746. climbing stairs with minimum cost - Dynamic Planning
leetcode - 572. 另一棵树的子树
Synchronous switching power supply reduces EMI layout dv/dt di/dt
Sword finger offer 42 Maximum sum of successive subarrays









