当前位置:网站首页>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 .
边栏推荐
- Operate BOM objects (key)
- Capturing and sorting out external articles -- autoresponder, composer, statistics [III]
- Node MySQL serialize cannot rollback transactions
- Summary of common operation and maintenance commands
- Qtablewidget control of QT
- Etcd raft Based Consistency assurance
- University of Electronic Science and technology | playback of clustering experience effectively used in reinforcement learning
- Qualcomm platform WiFi -- P2P issue
- 19、 MySQL -- SQL statements and queries
- 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%
猜你喜欢
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
JS three families
QT6 QML book/qt quick 3d/ Basics
Mysql database ----- common commands of database (based on database)
强化学习-学习笔记1 | 基础概念
浅议.NET遗留应用改造
JVM JNI and PVM pybind11 mass data transmission and optimization
Introduction to golang garbage collection
"Designer universe" APEC safety and health +: environmental protection Panda "xiaobaobao" Happy Valentine's Day 2022 | ChinaBrand | Asia Pacific Economic media
Qt6 QML Book/Qt Quick 3D/基础知识
随机推荐
Gauss elimination solves linear equations (floating-point Gauss elimination template)
MySQL——SQL注入问题
抓包整理外篇——————autoResponder、composer 、statistics [ 三]
Is it OK for fresh students to change careers to do software testing? The senior answered with his own experience
How to choose cache read / write strategies in different business scenarios?
[postgresql]postgresql custom function returns an instance of table type
上周内容回顾
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
[Yugong series] February 2022 Net architecture class 004 ABP vNext used in WPF project
请教大家一个问题,用人用过flink sql的异步io关联MySQL中的维表吗?我按照官网设置了各种
MySQL master-slave synchronization principle
Gee calculated area
Sort out several network request methods of JS -- get rid of callback hell
MDM mass data synchronization test verification
JS three families
Talk about daily newspaper design - how to write a daily newspaper and what is the use of a daily newspaper?
MySQL——索引
How to modify the network IP addresses of mobile phones and computers?
Transformation between yaml, Jason and Dict
Compilation Principle -- syntax analysis