当前位置:网站首页>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 .
边栏推荐
- @Scenario of transactional annotation invalidation
- 2022 low voltage electrician examination and low voltage electrician simulation examination question bank
- Compilation Principle -- syntax analysis
- [Tang Laoshi] C -- encapsulation: member variables and access modifiers
- Instructions for common methods of regular expressions
- Custom view incomplete to be continued
- Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
- MySQL dump - exclude some table data - MySQL dump - exclude some table data
- 一台服务器最大并发 tcp 连接数多少?65535?
- Viewing Chinese science and technology from the Winter Olympics (II): when snowmaking breakthrough is in progress
猜你喜欢
[secretly kill little buddy pytorch20 days -day02- example of image data modeling process]
2022 low voltage electrician examination and low voltage electrician simulation examination question bank
It is discussed that the success of Vit lies not in attention. Shiftvit uses the precision of swing transformer to outperform the speed of RESNET
一台服务器最大并发 tcp 连接数多少?65535?
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
强化學習-學習筆記1 | 基礎概念
MDM mass data synchronization test verification
Operate BOM objects (key)
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
Single page application architecture
随机推荐
请教大家一个问题,用人用过flink sql的异步io关联MySQL中的维表吗?我按照官网设置了各种
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
[secretly kill little buddy pytorch20 days -day02- example of image data modeling process]
Pytorch sets the weight and bias of the model to zero
Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
Apprentissage intensif - notes d'apprentissage 1 | concepts de base
Study diary: February 14th, 2022
APEC industry +: father of the king of the ox mill, industrial Internet "king of the ox mill anti-wear faction" Valentine's Day greetings | Asia Pacific Economic media | ChinaBrand
Service discovery and load balancing mechanism -service
Qt6 QML Book/Qt Quick 3D/基础知识
一台服务器最大并发 tcp 连接数多少?65535?
MySQL——索引
[gd32l233c-start] 5. FLASH read / write - use internal flash to store data
Xai+ network security? Brandon University and others' latest "interpretable artificial intelligence in network security applications" overview, 33 page PDF describes its current situation, challenges,
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
电子科技大学|强化学习中有效利用的聚类经验回放
From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
Cannot load driver class: com. mysql. cj. jdbc. Driver
设计电商秒杀系统