# Slow Query

+ Search for a user's Top N Slow Queries:
```
select query_time, query, user
from information_schema.slow_query
where is_internal = false  
  and user = "user1"        
order by query_time desc
limit 2;
+-------------------+--------------------------------------------------------------------------------+
| query_time        | query                                                                          |
+-------------------+--------------------------------------------------------------------------------+
| 12.77583857       | select * from t_slim, t_wide where t_slim.c0=t_wide.c0;                        |
|  0.734982725      | select t0.c0, t1.c1 from t_slim t0, t_wide t1 where t0.c0=t1.c0;               |
+-------------------+--------------------------------------------------------------------------------+
```
------------------------------------------------------------

Inefficient execution of SQL in daily production can affect the stability of the system. It's often the result of some poorly optimized SQL, which requires us to locate these SQL quickly and accurately in some way. The usual way to locate slow SQL is to use the TiDB Dashboard to do most of the work, but sometimes we need to filter based on specific conditions, we need to use the SLOW_QUERY and CLUSTER_SLOW_QUERY system tables to help us filter the positioning problems
CLUSTER_SLOW_QUERY is basically similar to SLOW_QUERY. Queries based on SLOW_QUERY can be replaced with the CLUSTER_SLOW_QUERY system table to query the full set of slow statements
Search for a user's Top N slow queries
```
select query_time, query, user
from information_schema.slow_query
where is_internal = false  -- 排除 TiDB 内部的慢查询 SQL
  and user = "user1"        -- 查找的用户名
order by query_time desc
limit 2;
+--------------+------------------------------------------------------------------+
| query_time   | query                                                            |
+--------------+------------------------------------------------------------------+
| 12.77583857  | select * from t_slim, t_wide where t_slim.c0=t_wide.c0;          |
|  0.734982725 | select t0.c0, t1.c1 from t_slim t0, t_wide t1 where t0.c0=t1.c0; |
+--------------+------------------------------------------------------------------+
```
