在 SQL Server Profiler 中显示 - 查询执行需要 1 毫秒,但在 Spring 应用程序中需要 30 毫秒,延迟在哪里?
In SQL Server Profiler shows - Query Execution takes 1ms but in Spring App takes 30ms, where is the delay?
我在 Azure SQL 数据库
中创建了一个简单的 Table
CREATE TABLE DeleteMe(
[ID] [int] NOT NULL,
[LastName] [char](255) NOT NULL,
[FirstName] [char](255) NOT NULL,
[Age] [int] NOT NULL,
[DOJ] [datetime2](0) NOT NULL,
[Role] [varchar](255) NULL
) ON [PRIMARY]
GO
CREATE CLUSTERED INDEX [clusteredindexdeleteme] ON DeleteMe
(
[ID] ASC,
[LastName] ASC,
[FirstName] ASC,
[Age] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
在 SQL Server Profiler 中显示 - 查询执行时间少于 1 毫秒
但在 Spring 应用程序需要 30 毫秒
INFO : 01.05.2022:1754 (40.419) [[]main] HikariDataSource: springHikariCP - Starting...
INFO : 01.05.2022:1754 (41.941) [[]main] HikariDataSource: springHikariCP - Start completed.
Execution Started: 2022/01/05 17:54:41.948
INFO : 01.05.2022:1754 (41.950) [[]main] HikariDataSource: springHikariCP - Starting...
INFO : 01.05.2022:1754 (42.139) [[]main] HikariDataSource: springHikariCP - Start completed.
Select Query Execution Started: 2022/01/05 17:54:42.225
Select Query Execution Completed: 2022/01/05 17:54:42.251
Java 应用:
public class JDBCSample {
public static void main(String[] args) {
...
try {
Connection conn = dataSource().getConnection();
conn.setAutoCommit(false);
Statement statement = conn.createStatement();
ResultSet rs ;
try {
LocalDateTime executionStartTime = LocalDateTime.now();
System.out.println("Select Query Execution Started: " + dtf.format(executionStartTime));
rs = statement.executeQuery("SELECT ID from Delete Where ID = 50");
executionEndTime = LocalDateTime.now();
System.out.println("Select Query Execution Completed: " + dtf.format(executionEndTime));
}
catch (SQLException ex)
{
System.out.println("Error message: " + ex.getMessage());
return; // Exit if there was an error
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Bean(destroyMethod = "close")
public static DataSource dataSource(){
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
hikariConfig.setJdbcUrl("jdbc:sqlserver://....;encrypt=true;trustServerCertificate=false;");
hikariConfig.setUsername("...");
hikariConfig.setPassword("...");
hikariConfig.setMaximumPoolSize(1);
hikariConfig.setConnectionTestQuery("SELECT 1");
hikariConfig.setPoolName("springHikariCP");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
return dataSource;
}
}
注意:当我对安装在 Dev 笔记本电脑上的本地服务器执行相同的应用程序时,只需要 6 毫秒。
注意:我正在从 Azure Kubernetes 执行此应用程序。
延迟在哪里?我该如何解决这个问题,以便 Spring 应用程序不会超过 2 毫秒。
根据你在这里分享的内容,我认为延迟是网络延迟。
Dev Java App 到 Dev Server 需要 6 秒。
Azure Kubernetes 应用到 Azure SQL 数据库需要 30 毫秒。
如果您包括应用程序和服务器之间的网络延迟,我认为这是合理的。我不认为你会在 2 毫秒内得到结果。如果你收到了,请告诉我 ;)
尝试以某些方式检查网络延迟,例如在 SELECT 1
查询和计算周转时间。
有趣的阅读:
https://azure.microsoft.com/en-in/blog/testing-client-latency-to-sql-azure/
https://docs.microsoft.com/en-us/archive/blogs/igorpag/azure-network-latency-sql-server-optimization
https://github.com/RicardoNiepel/azure-mysql-in-aks-sample
There is a way to measure latency network in java
我们向 Microsoft 支持开立了一个案例,发现 AKS 集群中的 coreDNS 存在问题,并要求 Microsoft 产品组增加 coreDNS 的资源 Pods。
我在 Azure SQL 数据库
中创建了一个简单的 TableCREATE TABLE DeleteMe(
[ID] [int] NOT NULL,
[LastName] [char](255) NOT NULL,
[FirstName] [char](255) NOT NULL,
[Age] [int] NOT NULL,
[DOJ] [datetime2](0) NOT NULL,
[Role] [varchar](255) NULL
) ON [PRIMARY]
GO
CREATE CLUSTERED INDEX [clusteredindexdeleteme] ON DeleteMe
(
[ID] ASC,
[LastName] ASC,
[FirstName] ASC,
[Age] ASC
)WITH (STATISTICS_NORECOMPUTE = OFF, DROP_EXISTING = OFF, ONLINE = OFF, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
GO
在 SQL Server Profiler 中显示 - 查询执行时间少于 1 毫秒
但在 Spring 应用程序需要 30 毫秒
INFO : 01.05.2022:1754 (40.419) [[]main] HikariDataSource: springHikariCP - Starting...
INFO : 01.05.2022:1754 (41.941) [[]main] HikariDataSource: springHikariCP - Start completed.
Execution Started: 2022/01/05 17:54:41.948
INFO : 01.05.2022:1754 (41.950) [[]main] HikariDataSource: springHikariCP - Starting...
INFO : 01.05.2022:1754 (42.139) [[]main] HikariDataSource: springHikariCP - Start completed.
Select Query Execution Started: 2022/01/05 17:54:42.225
Select Query Execution Completed: 2022/01/05 17:54:42.251
Java 应用:
public class JDBCSample {
public static void main(String[] args) {
...
try {
Connection conn = dataSource().getConnection();
conn.setAutoCommit(false);
Statement statement = conn.createStatement();
ResultSet rs ;
try {
LocalDateTime executionStartTime = LocalDateTime.now();
System.out.println("Select Query Execution Started: " + dtf.format(executionStartTime));
rs = statement.executeQuery("SELECT ID from Delete Where ID = 50");
executionEndTime = LocalDateTime.now();
System.out.println("Select Query Execution Completed: " + dtf.format(executionEndTime));
}
catch (SQLException ex)
{
System.out.println("Error message: " + ex.getMessage());
return; // Exit if there was an error
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Bean(destroyMethod = "close")
public static DataSource dataSource(){
HikariConfig hikariConfig = new HikariConfig();
hikariConfig.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
hikariConfig.setJdbcUrl("jdbc:sqlserver://....;encrypt=true;trustServerCertificate=false;");
hikariConfig.setUsername("...");
hikariConfig.setPassword("...");
hikariConfig.setMaximumPoolSize(1);
hikariConfig.setConnectionTestQuery("SELECT 1");
hikariConfig.setPoolName("springHikariCP");
HikariDataSource dataSource = new HikariDataSource(hikariConfig);
return dataSource;
}
}
注意:当我对安装在 Dev 笔记本电脑上的本地服务器执行相同的应用程序时,只需要 6 毫秒。
注意:我正在从 Azure Kubernetes 执行此应用程序。
延迟在哪里?我该如何解决这个问题,以便 Spring 应用程序不会超过 2 毫秒。
根据你在这里分享的内容,我认为延迟是网络延迟。
Dev Java App 到 Dev Server 需要 6 秒。
Azure Kubernetes 应用到 Azure SQL 数据库需要 30 毫秒。 如果您包括应用程序和服务器之间的网络延迟,我认为这是合理的。我不认为你会在 2 毫秒内得到结果。如果你收到了,请告诉我 ;)
尝试以某些方式检查网络延迟,例如在 SELECT 1
查询和计算周转时间。
有趣的阅读:
https://azure.microsoft.com/en-in/blog/testing-client-latency-to-sql-azure/
https://docs.microsoft.com/en-us/archive/blogs/igorpag/azure-network-latency-sql-server-optimization
https://github.com/RicardoNiepel/azure-mysql-in-aks-sample
There is a way to measure latency network in java
我们向 Microsoft 支持开立了一个案例,发现 AKS 集群中的 coreDNS 存在问题,并要求 Microsoft 产品组增加 coreDNS 的资源 Pods。