当前位置:网站首页>Interviewer: why does database connection consume resources? I can't even answer.. I was stunned!
Interviewer: why does database connection consume resources? I can't even answer.. I was stunned!
2022-06-30 00:25:00 【Chengxuape】
come from :CSDN, author :lmy86263 link :https://blog.csdn.net/lmy86263/article/details/76165714
Catalog
background
analysis
summary
background
Developing applications for a long time , Always want to get to the bottom , Especially for some questions with public answers . Everyone can explain , But in the final analysis , Can't explain clearly . Everything has a reason , And it is the most intuitive to explain the problem with numbers .
This article mainly wants to explore the details of connecting to the database , Especially in Web In the application, the database should be used to connect to the pool , Avoid re establishing a connection every time you send a request .
For this question , The answers are consistent , Establishing a database connection is time-consuming , But how much time does it take , What are the time-consuming aspects ?
analysis
This paper is connected with MySQL Database, for example , because MySQL The database is open source , Its communication protocol is open , So we can analyze the whole process of establishing connection in detail .
In this paper , The analysis of resource consumption mainly focuses on the network , Of course , Resources also include memory 、CPU Equal computing resources , The programming language used is Java, However, it is not ruled out that programming languages will also have a certain impact .
Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
// Then the program terminates , The connection was forced to close

In the connection process shown in the figure above , It can be seen that MySQL The communication protocol is based on TCP Transmission protocol , And the protocol is binary , It's not like HTTP Text protocol for .
The process of establishing connection is as follows :
The first 1 Step : establish TCP Connect , Through three handshakes .
The first 2 Step : The server sends it to the client 「 Handshake messages 」, The client responds to the handshake message .
The first 3 Step : client 「 Send authentication package 」, For user authentication , After successful verification , Server return OK Respond to , Then start executing the command .
After successful user authentication , Some connection variables will be set , For example, the character set 、 Whether to automatically commit transactions, etc , There will be multiple data interactions . After completing these steps , Will perform real data query, update and other operations .
In the test of this paper , It only took 5 Line of code to establish a connection , But no operation is performed through this connection , So after the program is executed , The connection is not through Connection.close() closed , But because the program is finished , Cause the process to terminate , Cause the connection with the database to close abnormally , So in the end TCP Of RST message .
In this simplest code , No additional connection properties have been set , Therefore, the time spent on setting properties can be considered to be the least ( Actually , We didn't set any properties , But the driver still sets the character set 、 Automatic transaction submission, etc , This depends on the specific driver implementation ), So the whole connection takes the least time .
But statistics show that , Excluding the last TCP Of RST When the message ( Because the message does not require the server to return any response ), However, there is still a round trip between the client and the server 「7」 Time ,「 That is, complete a connection , It can be said that , Data needs at least a round trip between the client and the server 7 Time 」.
10.416042 - 10.190799 = 0.225243s = 225.243ms
It means , Establishing a database connection requires 225ms, And this can still be regarded as the least , Of course 「 The time spent may be affected by network conditions 、 Database server performance and application code efficiency 」, But here is just the simplest example , It's enough to say !
Because the above program terminated abnormally , But in a normal application , The connection is usually closed through Connection.close() Accomplished .
Class.forName("com.mysql.jdbc.Driver");
String name = "shine_user";
String password = "123";
String url = "jdbc:mysql://172.16.100.131:3306/clever_mg_test";
Connection conn = DriverManager.getConnection(url, name, password);
conn.close();

Network packet capture
In this case , Things have changed , It is mainly reflected in the disconnection from the database , Pictured above :
The first 1 Step : At this time MySQL Communication protocol phase , The client sends a request to close the connection , And you don't have to wait for the response from the server .
The first 2 Step :TCP disconnect ,4 Complete the connection and disconnection with one wave .
747.284311 - 747.100954 = 0.183357s = 183.357ms
There may also be the impact of network conditions , More than the above 225ms Less , But it has almost reached 200ms The level of .
So here comes the question , Imagine the scene , For a day job 2 For WAN's website , Assume that each user will only send 5 A request , So one day is 10 Million requests .
100000 * 150ms = 15000000ms = 15000s = 250min = 4.17h
In other words, the time spent on establishing database connection every day has reached 「4 Hours 」, So database connection pool is a must .
And when the day's activity increases , Using database connection pool alone can't completely guarantee the normal operation of your service , Other solutions need to be considered .
for example :
cache
SQL Precompilation
Load balancing
……
summary
Of course, this is not the main content of this article , The core idea of this paper is only one , Database connection is really time-consuming , So don't connect frequently .
边栏推荐
猜你喜欢
![Copy linked list with random pointer [space for time --hash record]](/img/d9/d81e0e4f81174c61275e4affe0777a.png)
Copy linked list with random pointer [space for time --hash record]

01背包问题

Quick pow: how to quickly find power

传统微服务框架如何无缝过渡到服务网格 ASM
![Majority element ii[molar voting method for finding modes]](/img/8f/5925f97c0f5f8c50c19a9ef6d7723c.png)
Majority element ii[molar voting method for finding modes]

SOFARegistry 源码|数据同步模块解析

俞敏洪:我的退与进;架构师必须了解的5种最佳软件架构模式;Redis夺命52连问|码农周刊VIP会员专属邮件周报 Vol.096

《性能之巅第2版》阅读笔记(四)--Memory监测
![[QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信](/img/a4/a84f916d3aa2cc59f5b686cd32797a.png)
[QNX Hypervisor 2.2用户手册]6.2.2 Guest与Host之间通信

MySQL高级篇1
随机推荐
Getting started with qpainter: drawing the chess interface
Solr基础操作11
@ConfigurationProperties使用不当引发的bug
IDEA工具快捷键的使用
Cloud native enthusiast weekly: cool collection of grafana monitoring panels
【编程题】迷宫问题
MySQL foundation 3
[advanced C language] dynamic memory management
gyctf_2020_document
MySQL高级篇2
Several simple queries of SQL Server database
Root cause of glideexception: failed decodepath{directbytebuffer- > gifdrawable- > drawable}
Solr basic operation 11
【UITableView】坑一:tableView:heightForHeaderInSection:方法不执行
Sword finger offer II 037 Asteroid collision
Connection query of SQL Server database
传统微服务框架如何无缝过渡到服务网格 ASM
MySQL advanced 2
Finding a job in 2022 is the "last lesson" for graduates
leetcode-1. 两数之和