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>