diff --git a/mysql-test/r/sleep_time.result b/mysql-test/r/sleep_time.result
new file mode 100644
index 0000000..7415840
--- /dev/null
+++ b/mysql-test/r/sleep_time.result
@@ -0,0 +1,13 @@
+# run a query that will take some time
+select sleep(5) as reset_time;
+reset_time
+0
+# Wait some more time outside of the query
+# Check that the total amount of time spent on the last command in the
+# 'main' connection (i.e. the ~5 seconds of COM_SLEEP) is a reasonable
+# number, here defined as < 7 seconds. This should be 1 since the one
+# other process should have spent less than 7 seconds in the COM_SLEEP
+# by this point.
+select SUM(TIME < 7) as should_be_1 from INFORMATION_SCHEMA.PROCESSLIST where ID!=CONNECTION_ID();
+should_be_1
+1
diff --git a/mysql-test/t/sleep_time.test b/mysql-test/t/sleep_time.test
new file mode 100644
index 0000000..4c52295
--- /dev/null
+++ b/mysql-test/t/sleep_time.test
@@ -0,0 +1,22 @@
+#
+# Test that the "Time" in processlist for threads in "Sleep" state is correct
+#
+
+connect(main,localhost,root,,);
+
+connection main;
+--echo # run a query that will take some time
+select sleep(5) as reset_time;
+--echo # Wait some more time outside of the query
+--sleep 5
+connection default;
+
+--echo # Check that the total amount of time spent on the last command in the
+--echo # 'main' connection (i.e. the ~5 seconds of COM_SLEEP) is a reasonable
+--echo # number, here defined as < 7 seconds. This should be 1 since the one
+--echo # other process should have spent less than 7 seconds in the COM_SLEEP
+--echo # by this point.
+select SUM(TIME < 7) as should_be_1 from INFORMATION_SCHEMA.PROCESSLIST where ID!=CONNECTION_ID();
+
+disconnect main;
+
diff --git a/sql/sql_class.cc b/sql/sql_class.cc
index 8d33c56..08379c2 100644
--- a/sql/sql_class.cc
+++ b/sql/sql_class.cc
@@ -4517,6 +4517,7 @@ void THD::set_command(enum enum_server_command command)
 #ifdef HAVE_PSI_THREAD_INTERFACE
   PSI_CALL(set_thread_command)(m_command);
 #endif
+  set_time();
 }
 
 /** Assign a new value to thd->query.  */
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index ef60893..13081aa 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -1124,7 +1124,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
   thd->enable_slow_log= TRUE;
   thd->query_plan_flags= QPLAN_INIT;
   thd->lex->sql_command= SQLCOM_END; /* to avoid confusing VIEW detectors */
-  thd->set_time();
   thd->set_query_id(get_query_id());
   if (!(server_command_flags[command] & CF_SKIP_QUERY_ID))
     next_query_id();
