1

On mysql I usually see time of query

select * from rooms;
+---------+---------------+----------+
| number  | room_name     | identify |
+---------+---------------+----------+
|       1 | myroom        |        1 |
|       2 | studio 1      |        4 |
|       3 | Dancefloor    |        7 |
+---------+---------------+----------+
3 rows in set (0,00 sec)

Is also possible to get cpu usage and memory from the mysql server?

4
  • CPU usage and memory of what? The timing data comes from the client, not the server. I'm not sure MySQL tracks CPU time per-query, and it certainly doesn't track memory since a lot of that is shared between queries. Commented Apr 21, 2020 at 2:26
  • From the server, I edit question now Commented Apr 21, 2020 at 2:30
  • 1
    Short answer: Nope. Long answer: MySQL exports a lot of data metrics and there's innumerable performance measurement tools, but none as simple as this. Commented Apr 21, 2020 at 2:31
  • Add as answer please, so I can vote and close the question. Commented Apr 21, 2020 at 2:49

1 Answer 1

2

Yes. It's nothing more than the elapsed time. You can replicate it by storing the start time and subtracting it from the time when the query ends. Like this pseudo-code.

start = Time.now
do_the_query
end = Time.now

elapsed_time = end - start

Digging into the mysql source code, this is nothing more than the elapsed clock time.

Here's the relevant code in client/mysql.cc.

static ulong start_timer(void) {
#if defined(_WIN32)
  return clock();
#else
  struct tms tms_tmp;
  return times(&tms_tmp);
#endif
}

static void end_timer(ulong start_time, char *buff) {
  nice_time((double)(start_timer() - start_time) / CLOCKS_PER_SEC, buff, true);
}

static void mysql_end_timer(ulong start_time, char *buff) {
  buff[0] = ' ';
  buff[1] = '(';
  end_timer(start_time, buff + 2);
  my_stpcpy(strend(buff), ")");
}
static int com_go(String *buffer, char *line MY_ATTRIBUTE((unused))) {
  ...

  char time_buff[52 + 3 + 1]; /* time max + space&parens + NUL */

  ...

  timer = start_timer();
  executing_query = true;
  error = mysql_real_query_for_lazy(buffer->ptr(), buffer->length());

  ...

    if (verbose >= 3 || !opt_silent)
      mysql_end_timer(timer, time_buff);
    else
      time_buff[0] = '\0';

If you don't read C...

  1. timer = start_timer(); gets the current time from times.
  2. mysql_real_query_for_lazy runs the query.
  3. mysql_end_timer(timer, time_buff) subtracts the current time from the start time and displays it.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.