当前位置:网站首页>MySQL index
MySQL index
2022-07-02 06:46:00 【fnfd】
Index is help MySQL Data structure for efficient data acquisition .
Ordered data structure for quick search
The database is outside the storage data itself , It also maintains a data structure that satisfies a specific search algorithm , These data structures point to data in some way , Based on these data structures, advanced search algorithms are implemented , This data structure is the index .
Index principle
The purpose of indexing is to improve query efficiency , The essence is to filter out the final desired results by constantly narrowing the range of data you want to obtain , And turn random events into sequential events , With this mechanism , We can always use the same lookup method to lock data . With the help of the index , When executing a query, you can quickly find the required data without scanning the entire table .
Index advantage
Improve the efficiency of data retrieval , Reduce the IO cost .
Sort data through index columns , Reduce the cost of sorting data , To reduce the CPU Consume .
Index disadvantage
Index is also a table , The table holds the primary key and index fields , And points to the record of the entity table , Therefore, the index column also takes up disk space .
Although the index greatly improves the query speed , At the same time, it will reduce the speed of updating the table , For example, make changes to the table INSERT,UPDATE and DELETE, Because when updating tables ,MySQL Not only to save data , And save the index file , Each update adds the field of the index column , Will adjust the index information after the key value changes due to the update .
When to use index
Primary key Build unique index by default
Columns as query criteria (where after )
Columns as foreign keys , Index foreign key relationship
Sorted fields
Grouped fields
When you don't need to use an index
There are too few records
Frequently added, deleted and modified tables , Query fewer tables , Add index to the required class
where Columns not used in conditions
Columns with repeated and average data ( Such as gender )
Index data structure
B+ Trees (InnoDB The storage engine just uses B+ Tree implementation index ).
A well ordered node can store multiple data structures
Non leaf nodes do not store data , Value store index , More indexes can be placed .
Data records are stored in leaf nodes
There is a chain pointer between all leaf nodes .
B+ By sorting the data, the tree can
Index classification
primary key : After setting it as the primary key, the database will automatically create an index .
ALTER TABLE Table name add PRIMARY KEY Table name ( Name );
Delete primary key index :
ALTER TABLE Table name drop PRIMARY KEY;
Single value index : That is, an index contains only a single column , A table can have multiple single-column indexes .
Create a single value index :CREATE INDEX Index name ON Table name ( Name );
DROP INDEX Index name ;
unique index : The value of the index column must be unique , Allow for null
CREATE UNIQUE INDEX Index name ON Table name ( Name );
DROP INDEX Index name ON Table name ;
Composite index ( Composite index ): That is, an index contains multiple columns , During database operations , Composite indexes require less overhead than single valued indexes ( Index multiple columns of the same ), A composite index can be used when the number of rows in a table is much larger than the number of indexed columns .
Create composite index :CREATE INDEX Index name ON Table name ( Column 1, Column 2)
DROP INDEX Name ON Table name ;
The leftmost prefix principle of Composite Index
For example, there are a,b,c The three column , by a,b Create a composite index with two columns , Then the leftmost index principle should be met when using . When using the column of the composite index as the condition , The leftmost column must appear as a condition , Otherwise, the combined index will not take effect .
Full-text index
When fuzzy query is needed , General index is invalid , At this time, you can use full-text index .
CREATE FULLTEXT INDEX Index name ON Table name ( Field name )WITH PARSER ngram;
Select structure from Table name where match( Name ) AGAINST( Search terms ).
Look at the index
SHOW INDEX FROM Table name ;
Clustered index and non clustered index
When you find the index, you find the data you need , So this index is a cluster index , So the primary key is the clustered index .
The storage of index and data is separated , In other words, the index was found but the data was not found , According to the value on the index ( Primary key ), Go back to the table again , Non clustered index is also called auxiliary index .
边栏推荐
- 如何调试微信内置浏览器应用(企业号、公众号、订阅号)
- Name six schemes to realize delayed messages at one go
- 微信小程序基础
- js数组的常用的原型方法
- Selenium+msedgedriver+edge browser installation driver pit
- Win10: add or delete boot items, and add user-defined boot files to boot items
- kali最新更新指南
- Pytest (3) parameterize
- Warp shuffle in CUDA
- Functions of tensorrt
猜你喜欢

微信小程序基础

web自动中利用win32上传附件

The table component specifies the concatenation parallel method

The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem

20201002 vs 2019 qt5.14 developed program packaging

Latex参考文献引用失败 报错 LaTeX Warning: Citation “*****” on page y undefined on input line *

由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决

AWD learning

Idea announced a new default UI, which is too refreshing (including the application link)

Win10:添加或者删除开机启动项,在开机启动项中添加在用户自定义的启动文件
随机推荐
Redis - grande question clé
Latex compiles Chinese in vscode and solves the problem of using Chinese path
Uploading attachments using Win32 in Web Automation
Nodejs - Express middleware modification header: typeerror [err_invalid_char]: invalid character in header content
Blog directory of zzq -- updated on 20210601
奇葩pip install
ctf-web之练习赛
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
unittest.TextTestRunner不生成txt测试报告
Sentry搭建和使用
由于不正常断电导致的unexpected inconsistency;RUN fsck MANUALLY问题已解决
Error "list" object is not callable in Web automatic switching window
Linux MySQL 5.6.51 community generic installation tutorial
Summary of advertisement business bug replay
table 组件指定列合并行方法
Automation - when Jenkins pipline executes the nodejs command, it prompts node: command not found
CTF three count
自学table au
QQ email cannot receive the email sent by Jenkins using email extension after construction (timestamp or auth...)
【文献阅读与想法笔记13】 Unprocessing Images for Learned Raw Denoising