当前位置:网站首页>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 .
边栏推荐
- Pytest (2) mark function
- Render minecraft scenes into real scenes using NVIDIA GPU
- The win10 network icon disappears, and the network icon turns gray. Open the network and set the flash back to solve the problem
- Tensorrt command line program
- 由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
- Flask-Migrate 检测不到db.string() 等长度变化
- 20210306转载如何使TextEdit有背景图片
- Browser scrolling for more implementations
- The intern left a big hole when he ran away and made two online problems, which made me miserable
- Atcoder beginer contest 253 F - operations on a matrix / / tree array
猜你喜欢
[literature reading and thought notes 13] unprocessing images for learned raw denoising
js中map和forEach的用法
默认google浏览器打不开链接(点击超链接没有反应)
FE - 微信小程序 - 蓝牙 BLE 开发调研与使用
unittest. Texttestrunner does not generate TXT test reports
Thread hierarchy in CUDA
Sentinel Alibaba open source traffic protection component
Win10桌面图标没有办法拖动(可以选中可以打开可以删除新建等操作但是不能拖动)
Utilisation de la carte et de foreach dans JS
如何调试微信内置浏览器应用(企业号、公众号、订阅号)
随机推荐
unittest.TextTestRunner不生成txt测试报告
ts和js区别
部署api_automation_test过程中遇到的问题
Eggjs -typeorm treeenity practice
AWD学习
The use of regular expressions in JS
ctf三计
There are multiple good constructors and room will problem
20201025 Visual Studio2019 QT5.14 信号和槽功能的使用
由於不正常斷電導致的unexpected inconsistency;RUN fsck MANUALLY問題已解决
Virtualenv and pipenv installation
[daily question 1] write a function to judge whether a string is the string after the rotation of another string.
Functions of tensorrt
There is no way to drag the win10 desktop icon (you can select it, open it, delete it, create it, etc., but you can't drag it)
浏览器滚动加载更多实现
查询GPU时无进程运行,但是显存却被占用了
[self cultivation of programmers] - Reflection on job hunting Part II
Common function writing method and set get writing method for calculating attributes
sprintf_s的使用方法
Fe - eggjs combined with typeorm cannot connect to the database