当前位置:网站首页>Database optimization understanding these is enough
Database optimization understanding these is enough
2022-07-28 14:15:00 【Huzz's childhood paper plane】
Database optimization is nothing more than the following items , It's enough to understand and remember
1. sql Sentence classification
DDL: Data definition language (create, drop, alter)
DQL: Data query language (select)
DML: Data manipulation language (insert, update, delete)
DCL: Data control language (grant, revoke, commit, rollback)
2. sql constraint
Primary key constraint
Foreign key constraints
Unique constraint ( A column of data in the table does not have the same value , A table can have multiple unique constraints )
Default constraint ( Insertion time , Some fields are assigned by default )
check constraint ( Judge the validity of insertion through logical expression )
3. Link query
The left outer join
Right connection
Internal connection
Cross connect ( The cartesian product )
4. in and exists The difference between
Use in, First line sub query , The sub query result is Cartesian product with the main table , Then filter according to the conditions
Use exists, First query the main table , Traverse the external table according to the query results , Matching condition
Subquery small , The main query is large and indexed use in
Subqueries are large and indexed , Main query small use exists
(in and exists Resulting in a change in the driving sequence , This is the main factor of performance change . We take the speed of the driving table as the performance index )
not in Both internal and external tables are scanned , No index is used , not exists Sub queries of can still use indexes , therefore not exists Than not in fast
5. varchar and char But don't
varchar It means longer The inserted characters are the same length , Not enough space , Faster query
char Indicates fixed The length of inserted characters is different , Do not fill in spaces , Slow query
6. drop, delete and truncate The difference between
drop delete truncate
Speed : fast slow Faster
type : DDL DML DDL
Roll back : Cannot roll back Roll back Cannot roll back
Delete : Whole table Data in the table All data
7. union and union all The difference between
Get the union of the two results
union It's going to be de duplicated Sort , union all Do nothing , Good performance
8. A temporary table
Mysql In execution sql Statement will temporarily create some tables to store intermediate results
Temporary tables are only visible to the current connection , Space will be released after the connection is closed
Memory temporary table
Disk temp table
Use of temporary tables :
form Subqueries in
disdinct Inquire about + order by
order by and group by When the clauses of are different
Use union
9. Big data query optimization
Index optimization
sql Optimize
Horizontal split
Split Vertically
In the middle of table
Cache technology
A fixed length table
The smaller the column, the faster the access
10. The slow query
Used to record that the execution time exceeds a certain threshold SQL Statement log
Optimize :
Check whether the index hits
Optimize database structure
Optimize LINIT Pagination
11. Self increasing ID and UUID
Self increasing id
The field length is small
Store in order , Easy to retrieve
Don't worry about duplicate primary keys
Data migration , Table merging is very troublesome
In a high concurrency environment , Competing for self incrementing locks will reduce the throughput of the database
UUID
Unique identification code
Generate at the application layer id, Improve the throughput of the database
Generate id It affects the insertion speed
UUID The size comparison between them is very slow , Affect query speed
Mysql Self incrementing primary keys are recommended in , A primary key index is a clustered index .
Primary key index B+ The entered leaf node stores the primary key value and data in order , If the primary key index is self increasing ID, Just arrange them in order .
If it is UUID, It will cause a lot of data movement when inserting , Generate a lot of memory fragmentation , Performance degradation
12. The field is set to NOT NULL
NULL It is different from null value , Null values do not take up space , and NUll It takes up space , All set to NOT NULL You can continue to insert null values
NULL It will affect some function statistics , count, B Trees don't store NULL, So the index doesn't use NULL
NOT IN Subquery in NULL In this case, the returned results are null
13. Optimized query
Use index correctly , Try to cover the index as much as possible
Paging
Only return the required fields
Rational use of sorting
Reduce comparison operations
Complex operations are performed on the server
Database Parallel Processing
Large queries are split into small queries
Decompose associated query , Using cache is more efficient
LINIT When the offset is large You can record the maximum of each removal ID, Next time use ID The query
14. Optimize union Inquire about
If you don't need to de duplicate and sort , have access to union all
15. Optimize where Clause
Do not apply !=, <> Avoid full table scanning
Do not apply null Or null judgment , Try to set the field to not null
Use as much as possible union all Instead of or
stay where and order by Index the columns involved
Reduce use in or not in, A full scan will be performed
stay where Using parameters in Clause results in a full table scan
Avoid expressions or function operations , Can cause indexes to fail
16. sql Statement execution order
from, on, join, where, group by, having, select, distinct, order by
17. Big watch optimization
Limit the scope of data query
Read / write separation , The main database is responsible for writing , Read from the library
Vertical sub table , Split fields into multiple tables
Horizontal sub table , Split data into multiple tables
Optimize a single table , To field , Indexes , Optimize query statements
Add cache
18. Vertical sub table , Vertical sub database , Horizontal sub table , Horizontal sub database
Vertical sub table : Split a table into multiple tables according to fields ( Table structure )
Vertical sub database : Divide the table according to business , Deploy to different databases ( Table structure )
Horizontal sub table : Split the data in one table into multiple tables ( Table data )
Horizontal sub database : Split the data of a table into different databases according to certain rules ( Table data )
19. In sub database and sub table ID Handle
There needs to be an overall situation ID:
UUID( No repetition , Take up a lot of space , Not suitable for indexing )
Database autoincrement ID( You need a library for generating primary keys , High concurrency has bottlenecks )
Redis Generate ID( Good performance )
Twitter Of snowflake Algorithm
20. Mysql The principle of replication
Ensure data consistency between master and slave servers
After inserting data into the primary server , The inserted data will be automatically synchronized from the server
Master slave replication principle :
Three threads : binlog Threads , I/O Threads , SQL Threads
binlog Threads : Be responsible for writing the data changes on the master server into the binary log
I/O Threads : Responsible for reading binary logs from the primary server , And write it to the relay log of the slave server (Relay log) in
SQL Threads : Be responsible for retrieving relay logs , Parse the log and perform data changes on the primary server
The role of master-slave replication :
High availability and failover
Load balancing
The data backup
Upgrade test
------------- Deepen the impression again ------------
1. Select the most appropriate field properties
On the basis of meeting business needs , Set the width of the field as small as possible
2. Database index
The index should be built on where Judgement and order by Sort the fields on
3. Try to use sub query , have access to JOIN Join query instead
Because using subqueries will create temporary tables
4. union all To meet the needs of the business , Don't use union
5. where Clause avoid using !=, <> The operator
6. Generally not recommended LIKE operation
7. WHERE Clause to avoid expression operations on fields
8. Don't use * Perform a full field query
9. Paging query : Use ID Index for paging , Or use the query results on the previous page ID Query the maximum value
10. Use... In some cases exists Instead of in
边栏推荐
- Websocket chat
- 正则表达式
- QT self-made soft keyboard is the most perfect and simple, just like its own virtual keyboard
- What is a spin lock? A spin lock means that when a thread attempts to acquire a lock, if the lock has been occupied by other threads, it will always cycle to detect whether the lock has been released,
- 安全保障基于软件全生命周期-Istio的认证机制
- 走进音视频的世界——FLV视频封装格式
- 多级缓存方案
- P1797 heavy transportation problem solution
- Understand BFC features and easily realize adaptive layout
- Solve the problem that uniapp wechat applet canvas cannot introduce fonts
猜你喜欢

Diablo 4 ps4/ps5 beta has been added to the Playstation database

论文研读--Masked Generative Distillation

Target detection: speed and accuracy comparison (fater r-cnn, r-fcn, SSD, FPN, retinanet and yolov3)

第六章 支持向量机

83.(cesium之家)cesium示例如何运行

多线程与高并发(三)—— 源码解析 AQS 原理

SLAM论文合集

对“Image Denoising Using an Improved Generative Adversarial Network with Wasserstein Distance“的理解

修订版 | 目标检测:速度和准确性比较(Faster R-CNN,R-FCN,SSD,FPN,RetinaNet和YOLOv3)...

Solve the problem that uniapp wechat applet canvas cannot introduce fonts
随机推荐
Jmeter安装教程及登录增加token
TS扫盲大法-基础篇
【Utils】CookieUtil
在centos中安装mysql5.7.36
Master several common sorting - Select Sorting
IP黑白名单
Security assurance is based on software life cycle -istio authorization mechanism
a标签_文件下载(download属性)
regular expression
Poj3268 shortest path solution
Understand the principle behind the virtual list, and easily realize the virtual list
记一次COOKIE的伪造登录
jenkins
Implementation of StrCmp, strstr, memcpy, memmove
Verification code brute force cracking test [easy to understand]
[translation] salt companies come to linkerd for load balancing, and stay for efficiency, reliability and performance
解决跨越的几种方案
【Utils】JsonUtil
IntersectionObserver交叉观察器
安全保障基于软件全生命周期-Istio的认证机制