当前位置:网站首页>Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
Interviewer soul torture: why does the code specification require SQL statements not to have too many joins?
2022-07-05 13:26:00 【Hollis Chuang】
Send questions
interviewer : Have been operated Linux Do you ?
I : Yes
interviewer : I want to check the memory usage. What command should I use
I :free
perhaps top
interviewer : Then you can use it free What information can be seen in the command
I : that , As shown in the figure below You can see memory and cache usage
total Total memory
used Used memory
free Free memory
buff/cache Used cache
avaiable Available memory
interviewer : Do you know how to clean up the used cache (buff/cache)
I :em... I do not know!
interviewer :sync; echo 3 > /proc/sys/vm/drop_caches
You can clean it up buff/cache
了 , Can you tell me if I can execute this command online ?
I :( Send questions , Inner joy ) The benefits are great , Clean up the cache and we have more memory available , Just follow pc above xx Like the guard's little rocket , click , To free up a lot of memory
interviewer :em...., Go back and wait for the announcement
We can talk SQL Join
interviewer : Change the subject , Talk to you about join The understanding of the
I : well ( If you make another mistake, it's over , Seize the opportunity )
review
SQL Medium join
According to some conditions, you can combine the specified table with and return the data to the client
join
There are
inner join
Internal connection
left join
Left connection
right join
The right connection
full join
Full connection
The above picture is from :cnblogs.com/reaptomorrow-flydream/p/8145610.html
interviewer :
If it needs to be used in project development join
sentence , How to optimize and improve performance ?
I : There are two cases , Small data size , Large data scale .
interviewer : then ?
I : about
Small data size It's all in memory. Whoa
Large data scale
Can be optimized by adding indexes join Statement execution speed Can be reduced by redundant information join The number of times Minimize the number of table connections , One SQL Do not connect statement tables more than 5 Time
interviewer : It can be summarized as join
Statement is relatively cost performance , Am I right? ?
I : Yes
interviewer : Why? ?
buffer
I : In execution join There must be a process of comparison
interviewer : Yes
I : Comparing two tables one by one is slow , So we can read the data from two tables into one in turn Memory block
in , With MySQL Of InnoDB Engine as an example , By using the following statements, we can find the relevant memory area show variables like '%buffer%'
As shown in the figure below join_buffer_size
The size of will affect us join
Statement execution performance
interviewer : Besides ?
A big premise
I : Any project will go online after all , Data generation is inevitable , The size of the data can't be too small
interviewer : That's true
I : Most of the data in the database will eventually be saved to Hard disk
On , And stored as a file .
With MySQL Of InnoDB Engine as an example
InnoDB With
page
(page) Basic IO Company , The size of each page is 16KBInnoDB For each table, a
.ibd
file
verification
I : This means that we need to read as many files as we have tables to connect , Although the index can be used , But it's still necessary to move the head of the hard disk frequently
interviewer : That is to say, frequent movement of magnetic head will affect performance, right
I : Yes , Don't all open source frameworks like to say that they have greatly improved performance through sequential reading and writing , such as hbase
、kafka
interviewer : That's right. , Then you think Linux
Is this optimized ? Tips , You can do it again free
Order to have a look
I : Strange how the cache is occupied 1.2G many
picture source :https://www.linuxatemyram.com/
interviewer : Have you ever thought about it
buff/cache
What's in it ,?Why?
buff/cache
Take up so much memory , Available memory isavaillable
also1.1G
?Why can you clean it up with two orders
buff/cache
Memory footprint , And want to releaseused
It can only be done by ending the process ?
product , Your delicacies
After thinking for a few minutes
I : It's so easy to let go buff/cache
Memory used , That means it doesn't matter , Clearing it will not affect the operation of the system
interviewer : Not exactly
I : Is it ? Remember 《CSAPP》( Deep understanding of computer systems ) There's a word in it
The essence of memory hierarchy is , Each tier of storage device is the cache of the lower tier devices
Adult translation , That is to say Linux Think of memory as the cache of hard disk
Related information :http://tldp.org/LDP/sag/html/buffer-cache.html
interviewer : Now you know how to answer that question
I : I ....
Join Algorithm
interviewer : Give you another chance , If you can do it Join What would you do with the algorithm ?
I : Without index , Nested loop is over . If there is an index , Index can be used to improve performance .
interviewer : Back to join_buffer
Do you think join_buffer
What is stored in it ?
I : During scanning , The database will select a table to put it Data to be returned and compared with other tables In the join_buffer
interviewer : How to handle with index ?
I : This is a little bit easier , Just read the index tree of two tables and compare them , Let me introduce the processing method without index
Nested Loop Join
Nested loop , Read only one row of data in the table at a time , That is to say if outerTable Yes 10 Ten thousand rows of data , innerTable Yes 100 Row data , Read required 10000000
Time ( Suppose the files of these two tables are not cached in memory by the operating system , We call it the cold data sheet )
Of course, no database engine uses this algorithm now ( Too slow )
Block nested loop
Block
block , That is to say, every time a piece of data is taken to memory to reduce I/O The cost of
When no index is available ,MySQL InnoDB That's how it works
Consider the following two tables t_a
and t_b
When index execution is not possible join During operation ,InnoDB Will be used automatically Block nested loop
Algorithm
summary
At school , Database teachers like database paradigm best , I didn't learn to be performance oriented until I went to work , Redundancy means redundancy , There's no redundancy join
If join
It really affects performance . Try to get your join_buffer_size
, Or replace the SSD .
author : Corsane Go
juejin.cn/post/6844904041382674440
End
Previous recommendation
Redis Distributed lock failure , I can't help being rude ...
How to prevent your jar Decompiled ?
There is Tao without skill , It can be done with skill ; No way with skill , Stop at surgery
Welcome to pay attention Java Road official account
Good article , I was watching ️
边栏推荐
- [深度学习论文笔记]UCTransNet:从transformer的通道角度重新思考U-Net中的跳跃连接
- Mmseg - Mutli view time series data inspection and visualization
- Small case of function transfer parameters
- C# 对象存储
- APICloud Studio3 API管理与调试使用教程
- 记录一下在深度学习-一些bug处理
- 53. Maximum subarray sum: give you an integer array num, please find a continuous subarray with the maximum sum (the subarray contains at least one element) and return its maximum sum.
- 爱可生SQLe审核工具顺利完成信通院‘SQL质量管理平台分级能力’评测
- Apicloud studio3 API management and debugging tutorial
- [MySQL usage Script] catch all MySQL time and date types and related operation functions (3)
猜你喜欢
量价虽降,商业银行结构性存款为何受上市公司所偏爱?
南理工在线交流群
【每日一题】1200. 最小绝对差
Small case of function transfer parameters
Win10——轻量级小工具
Shandong University Summer Training - 20220620
Flutter draws animation effects of wave movement, curves and line graphs
Hundred days to complete the open source task of the domestic database opengauss -- openguass minimalist version 3.0.0 installation tutorial
私有地址有那些
Win10 - lightweight gadget
随机推荐
My colleague didn't understand selenium for half a month, so I figured it out for him in half an hour! Easily showed a wave of operations of climbing Taobao [easy to understand]
峰会回顾|保旺达-合规和安全双驱动的数据安全整体防护体系
[deep learning paper notes] hnf-netv2 for segmentation of brain tumors using multimodal MR imaging
mysql econnreset_ Nodejs socket error handling error: read econnreset
不知道这4种缓存模式,敢说懂缓存吗?
Alibaba cloud SLB load balancing product basic concept and purchase process
Binder communication process and servicemanager creation process
Write API documents first or code first?
Get you started with Apache pseudo static configuration
Yyds dry goods inventory # solve the real problem of famous enterprises: move the round table
关于 Notion-Like 工具的反思和畅想
Binder通信过程及ServiceManager创建过程
Difference between avc1 and H264
Flutter InkWell & Ink组件
Operational research 68 | the latest impact factors in 2022 were officially released. Changes in journals in the field of rapid care
数据湖(七):Iceberg概念及回顾什么是数据湖
With 4 years of working experience, you can't tell five ways of communication between multithreads. Dare you believe it?
【Hot100】34. 在排序数组中查找元素的第一个和最后一个位置
Go array and slice
restTemplate详解