当前位置:网站首页>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
边栏推荐
- 杂七杂八的东东
- Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)
- 1161 Merging Linked Lists
- cmder
- phpStudy设置301重定向
- Miscellaneous things
- 2121. 相同元素的间隔之和-哈希表法
- [saison de remise des diplômes · technologie agressive er] votre choix, agenouillez - vous et partez
- leetcode - 572. A subtree of another tree
- core. What is JS ---kalrry
猜你喜欢

haas506 2.0开发教程-高级组件库-modem.sms(仅支持2.2以上版本)

Haas 506 2.0 Tutoriel de développement - bibliothèque de composants avancés - modem. SMS (ne prend en charge que les versions supérieures à 2,2)

mysql 优化

XXL-SSO 实现SSO单点登录

Explain csma/cd, token bus and token ring clearly

Sklearn classification in sklearn_ Report & accuracy / recall /f1 value

Focusing on the smart city, Huawei cooperates with China Science and technology Xingtu to jointly develop a new digital blue ocean

网页制作存在的一些难点

Miscellaneous things

小白投资理财必看:图解基金买入与卖出规则
随机推荐
Give up Visio, this drawing tool is really fragrant!
Open source to the world (Part 2): the power of open source from the evolution of database technology BDTC 2021
图解三次握手四次挥手,小白都能看懂
swagger3整合oauth2 认证token
C# wpf 附加属性实现界面上定义装饰器
了解学习 JSX 的工作方式
杂七杂八的东东
English grammar_ Adverb - ever / once
idea自动生成serialVersionUID
Qt 中 QVariant 使用总结
【畢業季·進擊的技術er】自己的選擇,跪著也要走
Easy EDA learning notes 09 esp32-wroom-32e module esp32-devkitc-v4 development board one click download circuit
Open source ecology 𞓜 super practical open source license basic knowledge literacy post (Part 2)
2022年养老理财产品有哪些?风险小的
XShell7 下载
Gridsearchcv (grid search), a model parameter adjuster in sklearn
页面嵌入iframe 点击浏览器后退问题
Media industry under the epidemic situation, small program ecology driven digital transformation exploration
Termux
C language obtains second, millisecond, subtle and nanosecond timestamps