当前位置:网站首页>Review of MySQL (IX): index
Review of MySQL (IX): index
2022-06-12 18:20:00 【BKSW.】
MySql Review ( Nine ): Indexes
What is index ?
Indexes are used to quickly find rows with a specific value on a column . No index ,MySQL Have to start with the first record , Then read the entire table until it finds the relevant rows . The bigger the watch , The more time it takes .
-- Although index can improve retrieval efficiency , But you can't add indexes at will , Because the index is also an object in the database , It also requires constant database maintenance . There are maintenance costs .
-- such as : The data in the table is often modified , This is not suitable for adding indexes , Because once the data is modified , The index needs to be reordered , For maintenance .
-- Adding an index is to a field , Or add an index to some fields .
select ename,sal from emp where ename = 'SMITH';
-- When ename When the field is not indexed , above sql Statement will perform a full table scan , scanning ename All values in the field .
-- When ename When adding an index to a field , above sql Statement will scan... According to the index , Rapid positioning .
Create index
If the index is not used , Inquire about Wages are greater than 1500 Will perform a full table scan
An index is equivalent to the contents of a Book
The primary key will be automatically added to the index , Therefore, it is more efficient to query according to the primary key .
If often based on sal The query , And encountered a performance bottleneck , First, check whether the program has algorithm problems , Think again about sal Index , Index as follows :
Create index object
create unique index Index name on Table name ( Name );
create unique index u_ename on emp(ename);
Modify index name
alter table Table name add unique index Index name ( Name );
Look at the index
show index from emp;
Use index
Be careful : It must not be used select * … You can see type!=all 了 , Indicates that the index is used
explain select sal from emp where sal > 1500;
Delete index
DROP INDEX index_name ON talbe_name
ALTER TABLE table_name DROP INDEX index_name
ALTER TABLE table_name DROP PRIMARY KEY
-- among , The first two statements are equivalent , Delete the table_name Index in index_name.
-- The first 3 Only delete PRIMARY KEY Use... When indexing , Because a watch can only have one PRIMARY KEY Indexes ,
mysql> ALTER TABLE EMP DROP INDEX test_index;
-- The index is no longer used after deletion , The query will perform a full table scan .
Other knowledge of the index
The underlying data structure of the index is :B + Tree
The realization principle of index ?
adopt B Tree Narrow the scanning range , The underlying index is sorted , Partition , The index will carry the data in the table " Physical address ", After the data is finally retrieved through the index , Get the associated physical address ,
After the data is retrieved through the physical index , Get the associated physical address , Locate the data in the table by physical address , Efficiency is the highest .
select ename from emp where ename = ‘SMITH’;
Convert to by index :
select ename from emp where Physical address = 0x123;Classification of indexes ?
Single index : Add an index to a single field
Composite index : Add an index to the union of multiple fields
primary key : An index is automatically added to the primary key
unique index : Yes unique The constrained fields are automatically indexed
…When does the index expire ?
select ename from emp where ename like ’ %A% ';
Suo Yin
…When does the index expire ?
select ename from emp where ename like ’ %A% ';
When fuzzy query , The first wildcard uses %, At this time, the index is invalid .
边栏推荐
- A story on the cloud of the Centennial Olympic Games belonging to Alibaba cloud video cloud
- Arrays in JS (including leetcode examples) < continuous update ~>
- Interior design style type, rendering 100 invitation code [1a12]
- Variable of C #
- TypeScript高级类型(二)
- GD32F4xx控制DGUS 变量显示
- JS for Fibonacci sequence
- Comparison of disk mapping tools for network disk and object cloud storage management
- Gossip about the 88 of redis source code
- PHP:Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocat
猜你喜欢
Machine learning series (5): Naive Bayes
Typescript common types (I)
Window版本pytorch入门深度学习环境安装与配置
Gd32f4xx controls dgus touch keys
High-Speed Layout Guidelines 未完...
C#简单介绍
VirtualLab基礎實驗教程-4.單縫衍射
Schéma de cristallisation différentielle active et différence entre LV - PECL, LVDS et hcsl
Overall flow chart of kernel interrupt
Vant3 +ts packaged simple step advancer component
随机推荐
干货 | 一文搞定 pytest 自动化测试框架(二)
leetcode 300. Longest increasing subsequence
从源码解析 MobX 响应式刷新机制
PHP:Fatal error: Allowed memory size of 262144 bytes exhausted (tried to allocat
JS中的数组(含leetcode例题)<持续更新~>
Eve-ng installation (network device simulator)
js判断回文数
TypeScript类型声明文件(三)
Leetcode 674 longest incrementing substring
C language learning -- data storage in memory
Vant3+ts dropdownmenu drop-down menu, multi data can be scrolled
Random talk about redis source code 90
VirtualLab基礎實驗教程-4.單縫衍射
静态内存分配和动态内存分配小结
es7不使用父子和嵌套关系来实现一对多功能
Gospel of audio and video developers, rapid integration of AI dubbing capability
Section qemu+gdb
GD32F4xx控制DGUS触控按键
USB转串口那些事儿—最大峰值串口波特率VS连续通信最高波特率
ESP-IDF 添加自己的组件