当前位置:网站首页>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
边栏推荐
- Change tabledata column name
- (12) Interactive component selectable
- IDEA新版UI申请方法+无测试资格使用方法及相关介绍
- Description of string
- Notes on data mining in Tsinghua University (1)
- Background attribute compound writing
- Basic exercise decomposing prime factors
- 目标识别、检测和 6D 姿态估算源码与方案(最先进的方法和数据集)
- (十四)InputField逻辑分析
- Implementing architecture caching in MySQL under redis server environment
猜你喜欢

Filters and listeners

ERROR 1630 (42000): FUNCTION a.avg does not exist. Check the ‘Function Name Parsing and Resolution‘

【无标题】Task3 多路召回

MFS explanation (IV) -- MFS management server installation and configuration

node示例后台搭建

【字符集八】char8_t、char16_t、char32_t、wchar、char

2022 safety officer-c certificate special operation certificate examination question bank and simulation examination

IP, DNS, domain name, URL, hosts

Binlog in mysql:

数据库不知道哪里出问题
随机推荐
Box model border
(15) Tweenrunner
MFS详解(四)——MFS管理服务器安装与配置
第五章-[bx]和Loop指令
机器学习笔记 - 循环神经网络备忘清单
长安链节点证书、角色、权限管理介绍
Counting haybales (difference + discretization)
Flink传入自定义的参数或配置文件
(node:22344) [DEP0123] DeprecationWarning: Setting the TLS ServerName to an IP address is not permit
重启Kubernetes Pod的几种方式
2024. 考试的最大困扰度-滑动窗口
128. longest continuous sequence hash table
MySQL learning record - II. MySQL create table command
软件测试报告中常见的疏漏,给自己提个醒
2022 low voltage electrician retraining question bank and online simulation examination
Chapter V -[bx] and loop instructions
Analysis of 43 cases of MATLAB neural network: Chapter 8 prediction of GRNN Network - Freight Volume Prediction Based on generalized regression neural network
Latex common symbols summary
[character set 7] what are the wide character codes and multi byte codes of Chinese characters
top命令含义