当前位置:网站首页>Practical SQL statements and execution plan view
Practical SQL statements and execution plan view
2022-06-12 09:09:00 【Eden Garden】
see SQL Implementation plan of
When the data scale is small , Queries are all done in an instant , But when the data scale increases to more than ten million, the same sql There is no output , At this time, the importance of query optimization and indexing is reflected
How to view sql Implementation plan of :EXPLAIN select * from user where age = ‘20’;
id :select The order in which statements are executed ,id Same from top to bottom ,id The higher the value, the higher the priority , Execute first ;
select_type:
- simple: Does not contain subqueries or union
- primary: Include subqueries , The outermost query is marked as primary
- subquery: Subquery
- union:union
- derived: Appear in the from Subquery in Clause
table: Table name
partitions: The partition table shows partitions id, The non partitioned table shows null
type:
- eq_ref : Unique index or primary key
- ref: Non unique index
- ref_or_null: Similar to the previous query , But it's attached null Value judgement
possible_keys: The index involved in the query ( Full index )
key: Query the index actually used , Display without index null
key_len: Maximum length of index field
ref: Indicates which columns are used to find values on indexed columns
rows: Estimate the number of rows that the lock needs to read
Economic SQL:
1、 Insert or replace
Insert a new record (INSERT), But if the record already exists, delete it first and then insert it
REPLACE INTO DIS_CUS_LIST (ID,CUS_ID,CUS_NAME,BELONG_BUS,CERT_CODE) VALUES
(null,'0078045','xiaoxiong','02','789456123123456')
2、 Insert or update
Insert a new record (INSERT), But if the record already exists , Update this record
// Use the primary key to determine whether there is
INSERT INTO ... ON DUPLICATE KEY UPDATE
INSERT INTO DIS_CUS_LIST (ID,CUS_ID,CUS_NAME,BELONG_BUS,CERT_CODE) VALUES
(null,'0078045','xiaoxiong','02','789456123123456')
ON DUPLICATE KEY UPDATE BELONG_BUS='01',CERT_CODE='369852147'
3、 Insert or ignore
Insert a new record (INSERT), But if the record already exists , Just ignore
INSERT IGNORE INTO DIS_CUS_LIST (ID,CUS_ID,CUS_NAME,BELONG_BUS,CERT_CODE) VALUES
(null,'0078045','xiaoxiong','02','789456123123456')
4、SQL Medium if-else Judge
SQL Often used in if-else Judge
CASE WHEN ... THEN ... ELSE ... END
// scene : Game Festival feedback ,2021 All adult males can receive... For newly registered users in 10 Yuan's red envelope , Other users can receive 5 Yuan's red envelope
UPDATE user_info u SET u.balance = CASE WHEN u.sex = ' male '
and u.age > 18 THEN u.balance + 10 ELSE u.balance + 5 END WHERE u.creat_time >= '2021-01-01'
5、 Specify data snapshot or backup
If you want to assign data from one table to another new table , Can combine CREAT TABLE and SELECT
CREAT TABLE dis_cus_list1 SELECT * FROM dis_cus_list where belong_bus = '01'
6、 Write query result set
If the result set of the query needs to be written to the table , So let's create a temporary table to store data , Then use the corresponding query statement to import the result set into the table
INSERT INTO temp_1 (cus_id,belong_bus,aver_age,cert_code)
SELECT cus_id,belong_bus,AVG(score),cert_code FROM dis_cus_list GROUP BY cus_id
7、 Force the use of the specified index
When we write a query statement , We know which index we need to use as the best solution , Then you can use FORCE INDEX The specified index is used for forced query
// The specified index must exist
SELECT * FROM dis_cus_list FORCE INDEX (idx_cus_id) WHERE cus_id = '0078045' ORDER BY id DESC
Work experience :
During the work development process, there was a requirement to insert the query results into other tables , The stupidest thing is that I used the circular method to check the primary key first and then insert , As a result, if you need to insert thousands of data , You need to access the database thousands of times , Not only slow , It also increases the burden on the server ; Later, the technical manager used a query , Insert all the required data , You only need to access the database once , The efficiency is improved dozens of times in an instant .
Economic SQL Learn from :
https://blog.csdn.net/qq_39390545/article/details/106690916
边栏推荐
猜你喜欢

Use NVM to dynamically adjust the nodejs version to solve the problem that the project cannot be run and packaged because the node version is too high or too low

IDEA新版UI申请方法+无测试资格使用方法及相关介绍

ISCSI详解(五)——ISCSI客户端配置实战
![[computer use] how to change a computer disk into a mobile disk?](/img/ff/843f4220fcaefc00980a6edc29aebf.jpg)
[computer use] how to change a computer disk into a mobile disk?

第三章 寄存器 (内存访问)

Chapter IV - first procedure

《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测

网页中加载二次元3D虚拟主播源码(1:项目介绍和源码)

利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包

机器学习笔记 - 循环神经网络备忘清单
随机推荐
Problems that cannot be resolved by tar command
利用nvm动态调整nodejs版本,解决因为node版本过高或过低导致项目无法运行和打包
Priority issues
torch. logical_ And() method
[character set 9] will GBK be garbled when copied to unicode?
Change tabledata column name
Can you migrate backwards before the first migration in the south- Can you migrate backwards to before the first migration in South?
Occupied occupied occupied occupied occupied
[untitled] task3 multiple recall
第八章-数据处理的两个基本问题
Basic knowledge of Linear Algebra -- concepts and relationships of common matrices
CodeCraft-22 and Codeforces Round #795 (Div. 2) 题解
MFS详解(四)——MFS管理服务器安装与配置
《MATLAB 神经网络43个案例分析》:第8章 GRNN网络的预测----基于广义回归神经网络的货运量预测
解决当打开Unity时 提示项目已经打开,而自己之前并没有打开过(可能之前异常关闭)的问题
(15) Tweenrunner
重启Kubernetes Pod的几种方式
(十三)文本渲染Text
Node sample background setup
Inheritance of row height