0

While making query in MySQL by shell it shows time spent after result.


mysql> SELECT * FROM `SOME_TABLE`;
+----+---------+
| ID |   NAME  |
+----+---------+
|  1 |  `JOHN` |
+----+---------+
1 row in set (0.01 sec)

If you see spent time for this query is 0.01 sec. I want to know how to get this spent time by Java code using JDBC.

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Vanguard
  • 1,336
  • 1
  • 15
  • 30
  • Capture [`System.nanoTime()`](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#nanoTime--) before and after execution of the SQL statement, then subtract them from each other. – Andreas Jul 18 '16 at 04:53
  • Every resultset have duration you can use that resultset.getDouble('Duration'); – Prashant Srivastav Jul 18 '16 at 04:54
  • @Andreas System.nanotime returns spent time by java. I need to get to get mysql spent time. – Vanguard Jul 18 '16 at 04:56
  • @PrashantSrivastav resultset.getDouble('Duration'); -> Column 'Duration' not found. – Vanguard Jul 18 '16 at 04:57
  • @AsadGaniev It would measure the time spent sending the SQL to the server, plus time spent on the server running the SQL, plus time spent sending the result back to the client (your Java program). Do you have any reason to believe that the time shown in the `mysql` prompt is any different? – Andreas Jul 18 '16 at 04:59
  • @Andreas This is what I want to know. – Vanguard Jul 18 '16 at 05:19
  • What *exactly* is it you want? A way to measure the exact time spent by the RDBMS server executing the SQL statement, excluding transmissions times, SQL statement parsing, and Query Plan analysis? A way to measure the time including parsing and analysis? A way to measure the time also including transmission times? A way to measure the time spent from the perspective of Java? The exact same value that the prompt would give? If so, why do you want that particular number vs. any of the others? What *precision* do you want? Why isn't the suggestions already provided good enough? – Andreas Jul 18 '16 at 06:27
  • @Andreas sorry your answer was good enough. the other answers made me sure that it was really good. I just wanted to know mysql query execution time without java transmission time, and get the actual time that the mysql console gives when a query is executed – Vanguard Jul 18 '16 at 13:47
  • I don't have the answer for this, but how do you know that the time given by mysql console does not include transmission time? Is that documented, or is that just your guess/assumption? – Andreas Jul 18 '16 at 14:14
  • @Andreas I don't know if it is documented or not. So is there any way to clarify it? The question is becoming wider. :-) – Vanguard Jul 18 '16 at 14:29

0 Answers0