简体   繁体   English

在 Java 应用程序中测量 sql 执行时间

[英]Measure sql execution time in a Java Application

Is there a easy way to measure execution time of all sql statements that are executed by JDBC and print the result to the output?是否有一种简单的方法可以测量由 JDBC 执行的所有 sql 语句的执行时间并将结果打印到 output?

Some may advise me to use AOP to do this but I'm trying to avoid this if possible.有些人可能会建议我使用 AOP 来执行此操作,但我会尽可能避免这种情况。 Is there another way?还有其他方法吗?

Ironically, when viewing your question the advert on the right was for the Appdynamics Lite Java Performance tool .具有讽刺意味的是,在查看您的问题时,右侧的广告是针对Appdynamics Lite Java 性能工具的。

If you are not running the application in an application server that provides you a DataSource, you would find the log4jdbc project to be useful.如果您没有在为您提供数据源的应用程序服务器中运行应用程序,您会发现log4jdbc项目很有用。 The jdbc.sqltiming logger provided by the project will allow you to record the execution of the SQL statements executed.项目提供的jdbc.sqltiming记录器将允许您记录执行的 SQL 语句的执行情况。

You could use this in an application that relies on DataSources, by wrapping the connection returned from the DataSource in a ConnectionSpy object.您可以在依赖于 DataSources 的应用程序中使用它,方法是将 DataSource 返回的连接包装在 ConnectionSpy object 中。 This would require changes in your codebase.这将需要更改您的代码库。

There are of course, other options available the time of writing this:当然,在撰写本文时还有其他可用选项:

We use three different ways to show execution time.我们使用三种不同的方式来显示执行时间。

  1. We use built in Sql Server tools to show execution time/frequency/io/etc.我们使用内置的 Sql 服务器工具来显示执行时间/频率/io/等。 I don't do this myself so I don't know what the exact tool is.我自己不这样做,所以我不知道确切的工具是什么。

  2. We use AviCode to track execution time over a defined limit.我们使用 AviCode 来跟踪超过定义限制的执行时间。

  3. We run all of our sql calls through a library that automatically metrics all sql calls.我们通过一个自动度量所有 sql 调用的库来运行所有 sql 调用。

We use these different methods because they each provide a different view of the execution.我们使用这些不同的方法是因为它们各自提供了不同的执行视图。 When there is a problem we look at all of them to make sure they agree.当出现问题时,我们会查看所有问题以确保他们同意。

Do you have something like this available in your environment?你的环境中有类似的东西吗?

Check this out.看一下这个。 They mention using Sql Recorder with JDBC.他们提到使用 Sql Recorder 和 JDBC。 It might work for you.它可能对你有用。

Anything better than P6Spy? 有什么比 P6Spy 更好的吗?

If you want to check execution time take by your java application then print date then execute the statement and again print the date you can see the diffrence.如果您想检查 java 应用程序的执行时间,请打印日期,然后执行该语句并再次打印您可以看到差异的日期。 Like喜欢

System.out.println(new Date()); System.out.println(new Date()); stmt.executeUpdate(); stmt.executeUpdate(); System.out.println(new Date()); System.out.println(new Date());

If you want to see the time taken by SQL server, execute query in SQL Query analyzer, on right hand side below corner of the window you will find the time taken to execute the query.如果您想查看 SQL 服务器所用的时间,请在 SQL 查询分析器中执行查询,在 window 的右侧下角您将找到执行该查询所用的时间。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM