当前位置:网站首页>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 .
边栏推荐
- How to handle wechat circle of friends marketing activities and share production and release skills
- [Yugong series] go teaching course 002 go language environment installation in July 2022
- Offset related concepts + drag modal box case
- Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
- The "boss management manual" that is wildly spread all over the network (turn)
- MySQL dump - exclude some table data - MySQL dump - exclude some table data
- Getting started with postman -- built-in dynamic parameters, custom parameters and assertions
- Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
- Ask and answer: dispel your doubts about the virtual function mechanism
- Discussion Net legacy application transformation
猜你喜欢
Transformer structure analysis and the principle of blocks in it
Baohong industry | good habits that Internet finance needs to develop
不同业务场景该如何选择缓存的读写策略?
JVM JNI and PVM pybind11 mass data transmission and optimization
Experience summary of database storage selection
【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装
How to choose cache read / write strategies in different business scenarios?
MySQL master-slave synchronization principle
Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
你真的知道自己多大了吗?
随机推荐
JVM JNI and PVM pybind11 mass data transmission and optimization
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
浅析 Ref-NeRF
技术管理进阶——如何在面试中考察候选人并增大入职概率
Visiontransformer (I) -- embedded patched and word embedded
Discussion Net legacy application transformation
Basic number theory -- Chinese remainder theorem
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
Summary of common operation and maintenance commands
Scientific research document management Zotero
MySQL 8.0 data backup and recovery
thrift go
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
QT6 QML book/qt quick 3d/ Basics
Kubernetes 通信异常网络故障 解决思路
Inventory 2021 | yunyuansheng embracing the road
How to choose cache read / write strategies in different business scenarios?
Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
大神们,我想发两个广播流1 从mysql加载基础数据,广播出去2 从kafka加载基础数据的变更
浅议.NET遗留应用改造