当前位置:网站首页>MySQL index
MySQL index
2022-07-23 11:54:00 【Summer invasion bug planning】
MySQL Indexes
The index is equivalent to the catalogue of a Book , Through the directory, you can quickly find the corresponding resources .
In terms of databases , There are two retrieval methods when querying a table :
The first way : Full table scan
The second way : Search by index ( It's very efficient )
The fundamental principle is to narrow the scanning range .
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, so it is not suitable to add 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 no index is added to the field , 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 .
When to use index
The amount of data is huge ( It depends on the demand and online conditions );
This field is rarely DML operation ( Field modification , Corresponding index maintenance is also required );
This field often appears in where clause ( Which field do you often query based on );
Primary key and have unique The constrained fields are automatically indexed ;
Therefore, the primary key query is more efficient , Try to retrieve according to the primary key
see sql Statement execution plan
explain sql sentence ;
Create index object
create index The index name on Table name ( Field name );
Delete index object
drop index The index name on Table name ;
The realization principle of index
The underlying data structure of the index is :B+Tree

Any row record in the database table has a physical address
Fields are sorted and partitioned by index , Then search according to the query conditions , Last return physical address , The actual query statement becomes as follows :
select ename from emp where Physical address ;
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 , Locate the data in the table by physical address , efficiency
The highest. .
select ename from emp where ename = ‘SMITH’;
Convert to by index :
select ename from emp where Physical address = 0x3;
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 : The primary key automatically adds an index ;
unique index : Yes unique The constrained fields are automatically indexed ;
…
When the index expires
When fuzzy query , The first wildcard uses %, At this time, the index is invalid .
边栏推荐
- Window runs gradle build ----- stacktrace. Exception occurs when the file framework-4.3.0.build-snapshot-schema.zip is not found
- 数仓4.0笔记——业务数据采集
- 二叉树
- Development of digital collection system: what are the main features of NFT?
- Machine learning algorithm for large factory interview (5) recommendation system algorithm
- 软件测试1
- Using dynamic programming to solve the longest growing subsequence problem
- 2、MySQL数据管理--DML(添加、修改、删除数据)
- 编译原理-语法分析详解
- Internet communication
猜你喜欢

九、实用类

Development of digital collection system / introduction of digital collection scheme

数仓4.0笔记——用户行为数据采集三

3、DQL(数据查询语句)

Mosaic the face part of the picture

查看真机APP里面沙盒文件

数仓4.0笔记——用户行为数据采集一

Phxpaxos installation and compilation process

Development of digital collection system: what are the main features of NFT?

數倉4.0筆記——業務數據采集
随机推荐
Yarn capacity scheduler settings
数仓4.0笔记——用户行为数据采集三
1.认识数据库
數倉4.0筆記——用戶行為數據采集四
IP地址是什么
ninja启动过程
Image fuzzy processing batch production fuzzy data set
强迫症的硬盘分区
Typescript introduction
3.1、对DQL简化补充
NFT digital collection platform development and construction, source code development digital collection
MySQL tree structure recursive query
数仓4.0笔记——数仓建模
1. Initial experience of MySQL
MySQL存储引擎
MySQL用户管理
NFT digital collection development: Jingdong "Qida bear takes you to the capital" tourism package
UItextview的textViewDidChange的使用技巧
Pywinauto+ an application (learn to lesson 9) -- blocked
The user logs in continuously (interruption is allowed) to query SQL