当前位置:网站首页>Notes on MySQL related knowledge points (startup, index)
Notes on MySQL related knowledge points (startup, index)
2022-07-03 21:03:00 【qingfengvssuibian】
linux Service startup under
service mysqld start
systemctl start mysqld.service #( The above command is actually equivalent to executing the following command 【 Will jump 】)Connect mysql The server
mysql -uroot -p Input password
Operating the database
create database Database name # Create database
drop database Database name # Delete database
use Database name # Select database Index related
mysql The indexes of all seem to be based on B+ Tree built , unique index 、 Primary key 、 General index 、 Full text index, etc .
Logical classification
General index
# Create index
create index indexName on tableName(columnName(length));
# Modify table structure to add index
alter table tableName add index indexName (columnName(length));
# Create index when creating table
create table tableName(
id int primary key,
index indexName (columnName(10)) # Create index
);
# Delete index < General index deletion method >
# Delete an index of a table
drop index indexName on tableName;
# Delete an index of a table
alter table tableName drop index indexName;unique index
# Create unique index
create unique index indexName on tableName(columnName(length));
# Modify the table to create a unique index
alter table tableName add unique index indexName (columnName(length));
# Add a unique index when creating a table
create table tableName(
id int primary key,
name varchar(10),
unique index indexName(name(length))
);Primary key
# Create primary key
create table tableName(
id int primary key auto_increment
);
# perhaps
create table tableName(
id int auto_increment,
primary key (id);
);
# Set the primary key separately
alter table tableName add primary key (id);
# Delete index
# If the primary key is self increasing , We need to get rid of self increase
alter table tableName modify id int ; # Remove autoincrement
alter table tableName drop primary key;Full-text index
View index information
show index from tableName;Physical classification
Clustered index
It is generally a primary key index , If there is no primary key index , Just choose a non empty unique index , If not , Just choose rowid As a clustered index .
Each table has only one clustered index .
A clustered index is actually the storage structure of a table , It's a tree structure . If there is no clustered index , Data is neatly arranged and stored on disk .
Nonclustered indexes
The logical order of the index is generally different from the storage order of the data , A table can have multiple nonclustered indexes . Leaf nodes in nonclustered indexes do not store actual data , Save only the reference of the clustered index ( Such as primary key id).
Each table can have multiple nonclustered indexes .
Index advantages and disadvantages
advantage
Through the index , Reduce io frequency , Improve query efficiency
shortcoming
1、 Create index , Take up storage space
2、 When adding, deleting, or modifying an index , To maintain the structure of the index , Affect efficiency
Indexing rules
Should be created
1、 Have a primary key
2、 Frequently as queries 、 Fields for grouping and sorting criteria
3、 Fields associated with other tables
Avoid creating
1、 Indexing is not recommended for those with high repetition values , The amount of data returned to the table for query is too large ( Second inquiry )
2、 Do not index frequently updated fields , Because the maintenance cost is too high
3、 Tables with a small amount of data do not need to be indexed
Index usage considerations
1、like sentence '% Zhang San ' , The index will fail
2、 Unconditional query , Index failure , Such as != 、<> 、not in 、not like wait
3、 Joint index , Pay attention to order , The conditions with strong screening should be ranked first ( Highly unique )
4、 Cast does not go through the index .
5、 Use overlay index to avoid the occurrence of back table , Improve query efficiency
Overlay index is to change a single column non primary key index into a multi field joint index , You can find the desired data in an index tree , There is no need to go to the primary key index tree , Search again This phenomenon , be called Index overlay .
6、null 、not null 、!= You may also use the index , When the amount of returned data is less than 20% I'll go .
See ---
7、 Optimize paging scenarios with sub queries
MySQL It's not about skipping offset That's ok , It's about taking offset+N That's ok , Before giving up offset That's ok , return N That's ok , That's right offset When I was very old , Very inefficient , Or control the total number of returns , Or for pages that exceed a certain threshold SQL rewrite .
8、 Business unique fields , Even a combination of multiple fields , You should also create indexes ( Composite index )
9、 When establishing Association for multi table query , Ensure that the corresponding fields have indexes and the data types on both sides are consistent
10、 If the result is clear, there is only one , Use limit = 1 Can improve efficiency
11、select Statement declaration field
12、 As far as possible with union all Instead of union.Union The collection needs to be merged before uniqueness filtering , This involves sorting , a large number of cpu operation , Increase resource consumption and delay , Of course , Use union all The precondition is that there is no duplicate data in the two result sets .
边栏推荐
- Hcie security Day10: six experiments to understand VRRP and reliability
- Q&A:Transformer, Bert, ELMO, GPT, VIT
- 你真的知道自己多大了吗?
- Shortest path problem of graph theory (acwing template)
- MySQL——索引
- Pytorch sets the weight and bias of the model to zero
- 不同业务场景该如何选择缓存的读写策略?
- 大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
- Instructions for common methods of regular expressions
- Strange way of expressing integers (expanding Chinese remainder theorem)
猜你喜欢

Gee calculated area

Baohong industry | good habits that Internet finance needs to develop

Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
![[gd32l233c-start] 5. FLASH read / write - use internal flash to store data](/img/4f/ed8ca2f43ba5a8e5da63eb419aae82.jpg)
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data

Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress

Such as the visual appeal of the live broadcast of NBA Finals, can you still see it like this?

JVM JNI and PVM pybind11 mass data transmission and optimization

Qt6 QML Book/Qt Quick 3D/基础知识

In 2021, the global revenue of syphilis rapid detection kits was about US $608.1 million, and it is expected to reach US $712.9 million in 2028

XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
随机推荐
Qualcomm platform WiFi update disconnect end open event
Single page application architecture
强化學習-學習筆記1 | 基礎概念
Kubernetes 通信异常网络故障 解决思路
Sightseeing - statistics of the number of shortest paths + state transfer + secondary small paths
Install and use Chrony, and then build your own time server
C 10 new feature [caller parameter expression] solves my confusion seven years ago
Preliminary practice of niuke.com (11)
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
2022 low voltage electrician examination and low voltage electrician simulation examination question bank
鹏城杯 WEB_WP
Basic number theory -- Chinese remainder theorem
@Transactional注解失效的场景
Visiontransformer (I) -- embedded patched and word embedded
The global industrial design revenue in 2021 was about $44360 million, and it is expected to reach $62720 million in 2028. From 2022 to 2028, the CAGR was 5.5%
XAI+网络安全?布兰登大学等最新《可解释人工智能在网络安全应用》综述,33页pdf阐述其现状、挑战、开放问题和未来方向
Sort out several network request methods of JS -- get rid of callback hell
Transformation between yaml, Jason and Dict
Getting started with postman -- environment variables and global variables
Summary of common operation and maintenance commands
https://juejin.cn/post/6844903967365791752