当前位置:网站首页>MySQL index creation, optimization analysis and index optimization
MySQL index creation, optimization analysis and index optimization
2022-06-22 03:29:00 【Fenghua Langlang】
One 、 Indexes
Indexes : A sort data structure (Btree Indexes 、Hash Indexes 、full-text Full-text index 、R-Tree Indexes )
effect : Assist in quick query 、 Update data of data table
Single value index : An index contains only a single column , A table can have multiple single indexes
unique index : The value of the index column must be unique , But you can have an empty value
Composite index : An index contains multiple columns
Two 、 Index Syntax
CREATE [UNIQUE] INDEX index_name ON table_name (columname(length))
ALTER TABLE tbl_ name ADD PRIMARY KEY (column_list): This statement adds a primary key , This means that the index value must be unique , And cannot be NULL.
ALTER TABLE tbl_ name ADD UNIQUE index_name (column_list): The value to create the index must be unique ( except NULL Outside ,NULL There may be many times )
ALTER TABLE tbl_ name ADD INDEX index_name (column_list): Add a normal index , Index values can appear multiple times .
ALTER TABLE tbL_ name ADD FULLTEXT index_name (column_list) The index specified is FULLTEXT, For full-text indexing .
3、 ... and 、 The index case
- Fields that are frequently used as query criteria should be indexed
- Frequent new fields are not suitable for index creation ( Because not only are fields updated , And the new index )
- where You do not need to create an index for fields that are not used by the condition
- In the case of high concurrency, it tends to combine indexes
- Sort field in query , Will greatly improve the sorting speed
- Statistics or grouping fields in a query
- Table record too small
- Often add or delete tables ( Improve query speed , Reduced update speed Not only save data , And the new index )
- Duplicate content of data ( It doesn 't work )
Four 、 Index optimization
- see SQL Statement execution plan , View the SQL Whether the statement uses an index , Did you do a full scan , Get the details of the access policy that may be considered by the optimizer ,

ID
- id identical , The execution sequence is from bottom to top (t2–>t3–>t1)

- id identical , The execution sequence is from bottom to top (t2–>t3–>t1)
id Different , If it is a subquery ,id The number will increase ;id The higher the value is, the higher the priority is, and the earlier the execution is (t3–>t1–>t2)

- select_type- SIMPLE ordinary select Inquire about , The query does not contain subqueries or UNION
- PRIMARY If the query contains any complex sub parts , The outermost query is marked PRIMARY
- SUBQUERY stay SELECT or WHERE The list contains subqueries
- DERIVED stay FROM The subqueries contained in the list are marked with DERIVED( derivative ),MySQL These subqueries will be executed recursively , Put the results in the provisional table
- UNION If the second SELECT Appear in the UNION after , Is marked as UNION: if UNION Included in FROM Clause , Outer layer SELECT Will be marked as :DERIVED
- UNION RESULT from UNION Table to get the result SELECT
type

The best to worst order is :system > const > eq_ref > ref > range > index > allall Full table scanVery violent and primitive search methods , It's time consuming and inefficient , There is a lot of room for optimization .- S There are 20000 people in the school , I'll tell you you can find Xiao Ming for me , Then what do you do ! Of course, you are looking for 20000 people in the whole school one by one , Even if you are very lucky, the first person will find Xiao Ming , But you still can't stop , Because you can't confirm whether there is another Xiao Ming , Until you find 20000 people
index Press In index order Full table scanrange Index scan with range, be relative to index Full index scan of , It has limits , So it's better than indexref_eq There is only one result set , Use primary key or unique index to search Precise query , No need to scan too much- For example, according to the student number to find a student in a school , Before we look it up, we know that there must be only one result , So when we first found this student number , The query stopped immediately
const Put a primary key in where The following is used as a conditional query,mysql The optimizer turns query optimization into a constant . As for how and when to transform , It depends on the optimizerpossible_keys Available indexes , Not necessarily used when querying
key Columns show MySQL Actual index used ; No index key is selected NULL
key_len Length of index (key_len The displayed value is the maximum possible length of the index field , It's not the actual length )
Without loss of accuracy , The shorter the length, the betterrows According to table statistics and index selection , Roughly estimate the number of rows to read to find the required record
边栏推荐
- [nvme2.0b 10] controller shutdown and NVM subsystem shutdown
- On the auto increment of int type primary key in MySQL
- golang并发编程之原子操作详解
- 聊聊flink水位线
- 【NVMe2.0b 5】NVM Subsystem
- Usage of tail
- Implementation of synchronization and atomic operation by mutex mutex in golang concurrent programming
- uv_ Run process
- zombie进程与orphan进程
- 剑指 Offer 68 - I. 二叉搜索树的最近公共祖先
猜你喜欢

Why is setinterval so easy to get stuck in the high and low level

opencv安装(x86/tx2 cuda/共享库)

FastDFS-6.0.6

Why is the first program a programmer writes "Hello world!"
![[nvme2.0b 6] nvme queue model](/img/e9/d29001cebeebe9677b02ffb7c25726.png)
[nvme2.0b 6] nvme queue model

【论文】低光图像增强的零参考深度曲线估计

【NVMe2.0b 6】NVMe 队列模型

Modèle stratégique

【 thesis 】 zero reference depth curve estimation for low light image enhancement

3de 机器人吸盘抓box
随机推荐
tail的用法
Policy mode
NXP imx8mp learning records
【 thesis 】 zero reference depth curve estimation for low light image enhancement
FastDFS-6.0.6
[QNX Hypervisor 2.2用户手册]5.5 启动和使用Guest
golang标准库time
A component required a bean of type 'com.example.demo3.service.UserServiceImp' that could not be fou
泛微 E-cology V9 信息泄露漏洞
NXP imx8mp学习记录
调度功能:splunk-operator-controller-manager
Redis configuration and optimization
golang并发编程之runtime包
基于Pytorch的图像分类总结:Swin Transformer
torch. Max() explanation
rabbmitMQ 发布订阅模式<二>
[nvme2.0b 9] controller initialization process
AtCoder Regular Contest 142
PHP uses composer
[microservices | Nacos] Nacos implements the advanced version of distributed configuration center