Saturday 15 October 2016

java - How to get exec/fetch time of a JDBC query?



Tools like the DbVisualizer typically display an exec/fetch time after executing a database query. Is it possible to access such performance metrics after executing a query through JDBC?




ResultSet rs = stmt.executeQuery("SELECT a, b, c FROM TABLE2");
ResultSetMetaData rsmd = rs.getMetaData();

// missing functionality:
float executionTime = rsmd.getExecTime();
float fetchTime = rsmd.getFetchTime();


I did not find any such performance measure functionality in either the ResultSet or ResultSetMetaData classes. Is there no better way than 'manually' measuring the elapsed time using the system clock as discussed for example in How do I calculate the elapsed time of an event in java?


Answer




You have to measure the timing. Usually you would time things in milli-seconds with System.currentTimeMillis() or use System.nanoTime() for greater resolution/accuracy.



It would be a waste for it to time this automatically when it is rarely used.


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...