当前位置:网站首页>[MySQL from introduction to proficiency] [advanced chapter] (x) MyISAM's indexing scheme & advantages and disadvantages of indexing
[MySQL from introduction to proficiency] [advanced chapter] (x) MyISAM's indexing scheme & advantages and disadvantages of indexing
2022-07-28 11:14:00 【Man Nong Feige】
Hello! , I'm Manon Feige , Thank you for reading this article , Welcome to three links with one button .
1. Python Basic column , Basic knowledge in a net ,9.9 Yuan can't buy a loss , I can't buy it . Python From entry to mastery
️ 2. Python Crawler column , Systematically learn the knowledge points of reptiles .9.9 Yuan can't buy a loss , I can't buy it .python Reptile beginner level
️ 3. Ceph actual combat , Everything from principle to actual combat . Ceph actual combat
️ 4. Java Introduction to high concurrency programming , Punch in to learn Java High concurrency . Java Introduction to high concurrency programming
5. Take a stroll around the community , Weekly benefits , There are surprises every week . Manon Feige community , Leap plan
The whole network has the same name 【 Manon Feige 】 Welcome to your attention , personal VX: wei158556
List of articles
1. brief introduction
The previous articles have been introduced InnoDB Index scheme of storage engine , This article then introduces MyISAM Index scheme of storage engine .
MyISAM and InnoDB The default indexes of the storage engine are B+Tree Indexes
MyISAM Engine USES B+Tree As an index structure , Leaf node data Domain stores The address of the data record .
2. Environmental Science
| Environmental Science | edition |
|---|---|
| Red Hat | 4.8.5-39 |
| MySQL | 5.7 |
MyISAM The index principle of
We know from the previous study that InnoDB The engine is index, that is, data , That is, all complete user records have been included in the leaf node of the cluster index , The index and data are placed under the name Table name .ibd, For example, using InnoDB Engine index_demo The data and indexes of the table are stored in index_demo.ibd in ,
and MyISAM The engine stores the index and data in two separate files , For example, using MyISAM Engine engine_demo_table The index of the table is stored in engine_demo_table.MYI In file , Data is stored in engine_demo_table.MYD In the table .
MyISAM The indexing scheme of has the following characteristics :
- Record the records in the table According to the insertion order of records Stored separately in a file , be called
Data files. This file is not divided into several data pages , As many records as there are, just insert as many records into this file . Because when inserting data, there is no There is no deliberate sorting according to the size of the primary key , So we can't use dichotomy to find these data . - Use MyISAM The table of the storage engine will store the index information in another file called index file ( Table name .MYI) In another file ,MyISAM A separate index is created for the primary key of the table , However, the leaf node of the index does not store complete user records , It is Primary key value + Data record address The combination of .

There are three columns here , Suppose we use Col1 Primary key , The image above is a MyISAM Primary index of table (Primary key) schematic . It can be seen that MyISAM The index file of only holds the address of the data record . stay MyISAM in , Primary index and secondary index (Secondary key) There is no difference in structure . It's just a primary key index requirement key Is the only one. , And the secondary index key Can be repeated .
MyISAM And InnoDB Comparison of
MyISAM The indexing methods are " It's not clustering " Of , And InnoDB contain 1 Cluster indexes are different . The indexing methods of these two engines have the following differences :
- stay InnoDB In the storage engine , We just need to match... According to the primary key value Cluster index A search will find the corresponding record , And in the MyISAM But it needs to be done once Back to the table operation , signify MyISAM The indexes established in are equivalent to secondary indexes .
- InnoDB The data file itself is the index file ( Index is data ), and MyISAM Index files and data files are separate , Index file only holds the address of the data record .
- InnoDB The nonclustered index of data The domain stores the corresponding records The value of the primary key , and MyISAM The index records the address of the data .
- MyISAM The operation of returning table is very fast , Because taking the address offset directly to the file to get data , take the reverse into consideration InnoDB Back to the table is to find user records in the clustered index by obtaining the primary key value , Although it's not slow , It's better to visit the address directly than not .
- InnoDB List of requirements Must have primary key (MyISAM There can be no ), If no primary key is explicitly specified , be MySQL The system will automatically select a non empty and unique column representing data as the primary key , If there is no such column , be MySQL Automatically for InnoDB Table generates an implicit field as the primary key , The length of this field is 6 Bytes , Type is long .
PS:
6. Too long fields are not recommended as primary keys , Because all secondary indexes refer to primary key indexes , Too long primary key index will make the secondary index too large , And the size of each data page is 16KB, Too long primary key index will cause less data stored in each data page .
7. Using nonmonotonic fields as primary keys InnoDB It's not a good idea , because InnoDB The data file itself is a piece B+Tree, Non monotonic primary keys will cause new records to be inserted , Data files for maintenance B+Tree Frequent split adjustment . Very inefficient , While using Adding fields as primary keys is a good choice .
The advantages of indexing
- Indexing can greatly improve the efficiency of data retrieval , Reduce the IO cost
- By creating a unique index , It can ensure that every row in the database table The uniqueness of data .
- In terms of reference integrity of data , Can speed up the connection between tables , let me put it another way , When the dependent child table and parent table are jointly queried , Can improve the query speed .
- When using grouping and sorting clauses for data queries , Sure Significantly reduce the time of grouping and sorting in queries , Reduce CPU Consumption of
Disadvantages of indexes
The cost of space
Every time you build an index, you build a tree for it B+ Trees , Every tree B+ Each node of the tree is a data page , A data page is occupied by default 16KB Storage space , A big one B+ The tree consists of many data pages , That's a lot of storage space .
The cost of time
The data in the table is checked every time increase 、 Delete 、 Change In operation , All need to be modified B+ Tree index . And we talked about it B+ Each node of the tree is based on the value of the index column Sort from small to large And make up Double linked list . Whether it's a record in a leaf node , Or the records in the inner node form a one-way linked list according to the value of the index column from small to large . And increase 、 Delete 、 Changing operations may damage the sorting of nodes and records , So the storage engine needs extra time to do some Record shift , Page splitting , Page recycling And other operations to maintain the sorting of nodes and records . If we build many indexes , Each index corresponds to B+ Trees should be maintained accordingly , It will slow down performance .
summary
This article introduces in detail MyISAM The index scheme of ,MyISAM The engine and InnoDB The indexes used by the engine by default are B+Tree Indexes , The difference between them is MyISAM The index and data of are separated , The leaf node of the index will only store the address of the data , When searching data, you need to return to the table , and InnoDB The index of the engine is data , The leaf nodes of the clustered index store complete user records .
边栏推荐
- [JS advanced] JS functions, overloads, anonymous functions, scopes and scope chains_ 03
- 剑指 Offer 35. 复杂链表的复制
- 2021-03-24
- Clo********e: project management notes
- Learn these analysis methods and models, and no longer have no ideas when encountering problems
- Blue Bridge Cup embedded Hal library systick
- Apb2 and apb1 in stm32
- 2021-03-24
- Nodejs: detect and install the NPM module. If it is already installed, skip
- 用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
猜你喜欢

蓝桥杯嵌入式-HAL库-SYSTICK

分体式测斜探头安装要点及注意事项

Blue Bridge Cup embedded Hal library ADC

ctf技能树----文件上传

Advance.ai sailing guide helps enterprises sail to Indonesia and grasp half of the Southeast Asian market

机器学习强基计划0-5:为什么学习的本质是泛化能力?

Software designers ask 20 questions before the exam, pay attention!!

大三下学期总结

The solution of PHP sending mobile MAS SMS garbled code

Learn how to do e-commerce data analysis (with operation analysis index framework)
随机推荐
leetcode:981. 基于时间的键值存储【迭代for的陷阱:on】
国内外优秀程序员的博客全在这了,请查收
C语言使用二重指针实现简单工厂模式(多态)
const与指针的组合使用
用手机对电脑进行远程关机
Nodejs: set up the express service, set up the session and realize the exit operation
Eslint, Eslint中文文档
PHP发送移动MAS短信乱码的解决方法
CTF skill tree - file upload
Remote shutdown of computer with mobile phone
剑指 Offer 09. 用两个栈实现队列
Tree shaking and DCE
keil和IAR中lib库文件的生成和使用
Software designers ask 20 questions before the exam, pay attention!!
Question of hanging the interviewer
蓝桥杯嵌入式-HAL库-ADC
网络文件系统服务(NFS)
RHEL 6.4 安装svn和apache
Learn these analysis methods and models, and no longer have no ideas when encountering problems
Explanation of JDBC classes