0

I am currently having an issue measuring the time an sql query takes in JAVA code. In the code below, stmt.executeQuery(query.getQuery()); will do a SQL query and I am using System.currentTimeMillis() to see how long the query takes. The issues is that in the below jsp page where I view the results of queryTime , I am getting results such as The query took 18:00:00:055 time. for an extremely simple database (2 very small tables) and an extremely simple query. 18 hours obviously isn't a realistic time as the query runs on my computer in a matter of milliseconds. What am I doing wrong?

EDIT: In response to the submitted answers, I am outputting queryDuration to console along with the sql query that was submitted for testing and getting results such as (where the milliseconds are output after the SQL query that was entered):

query (from controller): select * from service_request
milliseconds: 1
query (from controller): select * from data_file where service_request_id =     '2'
milliseconds: 0
path: www.foobar.com/custom/bin/lib/
path: www.foobar.com/images/resources/
query (from controller): select FN_Contact from service_request where Notes   IS NOT NULL
milliseconds: 10
query (from controller): select FN_Contact from service_request where Notes IS NULL
milliseconds: 0
query (from controller): select Notes, LN_Contact from service_request where   FN_Contact = 'Amanda'
milliseconds: 0 

how does this make sense when some of the queries that are seemingly more complicated are completed in less time? (these tables are very small, 4-5 columns in each and 4-5 entries in each)

java calculating query time:

long queryStart = System.currentTimeMillis();
ResultSet rs = stmt.executeQuery(query.getQuery());
long queryEnd = System.currentTimeMillis();
long queryDuration = queryEnd-queryStart;
Date queryDurationTime = new Date(queryDuration);
DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
String dateFormatted = formatter.format(queryDurationTime);
queryTime = dateFormatted;

displaying queryTime on jsp page:

<c:choose>
    <c:when test="${empty queryTime}">
    </c:when>
    <c:otherwise>
        <h3>Results:</h3>
        The query took ${queryTime} time.
    </c:otherwise>
</c:choose>
GregH
  • 5,125
  • 8
  • 55
  • 109
  • Possible duplicate of [How to format a duration in java? (e.g format H:MM:SS)](http://stackoverflow.com/questions/266825/how-to-format-a-duration-in-java-e-g-format-hmmss) – Joe Oct 05 '15 at 03:04

2 Answers2

0

You can simply do it without the time conversions.

Example with simulation of 1000 msec of operation with Thread.sleep();

public static void main(String[] args) throws InterruptedException {
        long queryStart = System.currentTimeMillis();
        Thread.sleep(1000);
        long queryEnd = System.currentTimeMillis();
        long queryDuration = queryEnd - queryStart;
        // Date queryDurationTime = new Date(queryDuration);
        // DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
        // String dateFormatted = formatter.format(queryDurationTime);
        // queryTime = dateFormatted;
        System.out.println(queryDuration);
    }
chenchuk
  • 5,324
  • 4
  • 34
  • 41
0

The problem is that you are converting the time difference in milliseconds to a Date. Either return the time difference in milliseconds queryDuration, or wrap queryStart and queryEnd in Date objects and subtract the two dates.

Oli
  • 1,112
  • 1
  • 10
  • 25
  • That doesn't look odd to me. It's not surprising that queries take between <1 and 10 milliseconds to complete. You may think that some queries are "more complicated" than others but that doesn't mean they will take more time to complete. You would have to know how the queries are implemented and what optimisations have been made to make that kind of judgement – Oli Oct 02 '15 at 00:47