当前位置:网站首页>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 .
边栏推荐
- Capture de paquets et tri du contenu externe - - autoresponder, composer, statistiques [3]
- Measurement fitting based on Halcon learning -- Practice [1]
- 设计电商秒杀系统
- Brief analysis of ref nerf
- TLS environment construction and plaintext analysis
- Experience summary of database storage selection
- Apprentissage intensif - notes d'apprentissage 1 | concepts de base
- Shortest path problem of graph theory (acwing template)
- 强基计划 数学相关书籍 推荐
- Ask and answer: dispel your doubts about the virtual function mechanism
猜你喜欢

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

17 websites for practicing automated testing. I'm sure you'll like them

Study diary: February 14th, 2022

【愚公系列】2022年7月 Go教学课程 002-Go语言环境安装

Experience summary of database storage selection

LabVIEW training

2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions

Design e-commerce seckill system

From the behind the scenes arena of the ice and snow event, see how digital builders can ensure large-scale events

Hcie security Day11: preliminarily learn the concepts of firewall dual machine hot standby and vgmp
随机推荐
Redis data migration (II)
Mysql database ----- common commands of database (based on database)
Operate BOM objects (key)
Etcd 基于Raft的一致性保证
@Scenario of transactional annotation invalidation
Nmap and masscan have their own advantages and disadvantages. The basic commands are often mixed to increase output
The 12th Blue Bridge Cup
Advanced collaboration: coroutinecontext
MySQL——数据库备份
Goodbye 2021, how do programmers go to the top of the disdain chain?
Kubernetes abnormal communication network fault solution ideas
C 10 new feature [caller parameter expression] solves my confusion seven years ago
强化學習-學習筆記1 | 基礎概念
2022 melting welding and thermal cutting examination materials and free melting welding and thermal cutting examination questions
MySQL dump - exclude some table data - MySQL dump - exclude some table data
Hcie security Day12: supplement the concept of packet filtering and security policy
thrift go
Offset related concepts + drag modal box case
Recommendation of books related to strong foundation program mathematics
What is the maximum number of concurrent TCP connections for a server? 65535?
https://juejin.cn/post/6844903967365791752