oceanbase 性能監控04-性能分析sql

1 . 查詢是否存在sql 等待

select sql_id, elapsed_time, queue_time, get_plan_time, execute_time, application_wait_time, concurrency_wait_time, user_io_wait_time, schedule_time, event, wait_class, wait_time_micro, total_wait_time_micro 
from v$sql_audit 
 where trace_id = 'YB420B84FE35-0005648A67211DC9'\G


 
2.找到所有SQL中平均執行時間排在TOP N的SQL

select/*+ parallel(15)*/ avg_exe_usec, svr_ip, svr_port, sql_id, plan_id 
 from oceanbase.gv$plan_cache_plan_stat 
 where tenant_id = 1001 
 order by avg_exe_usec desc
 limit 3\G;


 
3.如何查看集羣SQL請求流量是否均衡?

select/*+ parallel(15)*/t2.zone, t1.svr_ip,  count(*) as QPS, avg(t1.elapsed_time), avg(t1.queue_time)  
 from oceanbase.gv$sql_audit t1, __all_server t2  
 where t1.svr_ip = t2.svr_ip and IS_EXECUTOR_RPC = 0    
       and request_time > (time_to_usec(now()) - 1000000)    
       and request_time < time_to_usec(now())
 group by t1.svr_ip  
 order by t2.zone;
 


4. 如何查看分佈式計劃rpc執行數是否均衡?

select/*+ parallel(15)*/t2.zone, t1.svr_ip,  count(*) as RPC_COUNT, avg(t1.elapsed_time), avg(t1.queue_time)  
 from oceanbase.gv$sql_audit t1, __all_server t2  
 where t1.svr_ip = t2.svr_ip and IS_EXECUTOR_RPC = 1    
       and tenant_id = 1001
       and request_time > (time_to_usec(now()) - 1000000)    
      and request_time < time_to_usec(now()) 
 group by t1.svr_ip  
 order by t2.zone;


 
 5.查詢SQL流量分佈情況及QPS

 select/*+ parallel(15)*/t2.zone, t1.svr_ip,  count(*) as RPC_COUNT, avg(t1.elapsed_time), avg(t1.queue_time)  
from oceanbase.gv$sql_audit t1, __all_server t2  
where t1.svr_ip = t2.svr_ip 
      and tenant_id = 1001
      and SQL_ID = 'BF7AA13A28DF50BA5C33FF19F1DBD8A9'
      and IS_EXECUTOR_RPC = 0    
      and request_time > (time_to_usec(now()) - 1000000)    
     and request_time < time_to_usec(now())
group by t1.svr_ip


6. 查詢某段時間內請求次數排在TOP N的SQL

select/*+ parallel(15)*/ SQL_ID,   count(*) as QPS, avg(t1.elapsed_time) RT   from oceanbase.gv$sql_audit t1   where   tenant_id = 1001       and IS_EXECUTOR_RPC = 0           and request_time > (time_to_usec(now()) - 10000000)           and request_time < time_to_usec(now()) group by t1.sql_id order by QPS desc limit 10;

7. 查詢某段時間內平均RT排在TOP N的SQL

select/*+ parallel(15)*/ SQL_ID,   count(*) as QPS, avg(t1.elapsed_time) RT   
 from oceanbase.gv$sql_audit t1   
 where   tenant_id = 1001       and IS_EXECUTOR_RPC = 0    
        and request_time > (time_to_usec(now()) - 10000000)       
         and request_time < time_to_usec(now())
 group by t1.sql_id 
 order by RT desc 
 limit 10;
 


8.查詢某段時間內執行時間排TOP N的請求

select/*+ parallel(15)*/ sql_id, elapsed_time , trace_id    
 from oceanbase.gv$sql_audit     
 where   tenant_id = 1001 
         and IS_EXECUTOR_RPC = 0 
        and request_time > (time_to_usec(now()) - 10000000) 
        and request_time < time_to_usec(now()) 
 order by elapsed_time desc  
 limit 10;


 
9.判斷系統或某個SQL的執行是否出現大量請求不合理的使用了遠程執行

select count(*), plan_type    
from oceanbase.gv$sql_audit     
where tenant_id = 1001          
      and IS_EXECUTOR_RPC = 0          
      and request_time > (time_to_usec(now()) - 10000000)         
      and request_time < time_to_usec(now()) 
group by plan_type ;

10.找出某個租戶中全表掃描的SQL

select query_sql 
from oceanbase.gv$sql_audit 
where table_scan = 1 and tenant_id = 1001 
group by sql_id;

11.
分佈式計劃如何分析查詢問題
1)先確定是否爲分佈式計劃, (g)v$plan_cache_plan_stat, (g)v$sql_audit中均對執行計劃類型有記錄;
2)分析該執行計劃是否正確;
3)通過trace_id關聯查詢gv$sql_audit, 查看所有執行的子計劃耗時情況,每個子計劃的RPC執行均對應一條sql_audit記錄,分析該sql_audit記錄來定位問題, 如下舉例:is_executor_rpc = 1表示子計劃執行在sql_audit中記錄,主要記錄執行相關信息。is_executor_rpc = 0表示接受sql請求的線程在sql_audit中的記錄, 該記錄含有SQL執行過程的信息,包括SQL信息,獲取執行計劃信息等。

select/*+ parallel(15)*/ sql_id, is_executor_rpc, elapsed_time 
 2from oceanbase.gv$sql_audit
 3where trace_id = 'YB420AB74FC6-00056349D323483A';

12.統計信息查詢
目前,sys租戶可以通過前述的內部表來查詢表和列的統計信息,下面的SQL給出了一個使用內部表查詢表、列的統計信息的示例。使用該SQL可以通過傳入數據庫名、表名和列名這三個信息的組合獲取到包括表的行數、列的NDV、列的null值數量以及列的最大最小值信息。

-- 內部表使用16進制格式存儲最大值最小值信息,使用des_hex_str()函數可以將它轉化爲可讀的格式。

select
  r_c as row_count,
  s.num_distinct as NDV, 
  s.num_null as num_null,
  des_hex_str(s.min_value) as min,
  des_hex_str(s.max_value) as max
from
  __all_column_statistic s,
  __all_database d,
  __all_table t,
  __all_column c,
  (select max(row_count)as r_c, table_id from __all_meta_table group by table_id) m
where
  s.table_id= t.table_id
  and t.database_id= d.database_id
  and d.database_name= 'tpch_1g'  -- 數據庫名
  and t.table_name= 'orders'      -- 表名
  and c.table_id= t.table_id
  and c.column_name= 'o_orderkey' -- 列名
  and s.column_id= c.column_id
  and s.column_id= c.column_id
  and s.table_id= m.table_id;

-- 查詢結果示例
+-----------+---------+----------+------+---------+
| row_count | NDV     | num_null | min  | max     |
+-----------+---------+----------+------+---------+
|   1500000 | 1479709 |        0 | 1    | 6000000 |
+-----------+---------+----------+------+---------+
1 row in set (0.14 sec)
 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章