All Products
Search
Document Center

Find the TOP N SQL queries

Last Updated: Jun 18, 2021

Find the TOP N SQL queries with the most requests within a certain period of time

You can execute the following statement to find the TOP N SQL queries with the most requests within a certain period of time:

obclient>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;

+----------------------------------+------+------------+
| SQL_ID                           | QPS  | RT         |
+----------------------------------+------+------------+
| BF7AA13A28DF50BA5C33FF19F1DBD8A9 | 2523 |  4233.2085 |
| CE7208ADDE365D0AB5E68EE24E5FD730 | 1268 |  5935.8683 |
| E5C7494018989226E69AE7D08B3D0F15 | 1028 |  7275.7490 |
| D0E8D8C937E44BC3BB9A5379AE1064C5 | 1000 | 12999.1640 |
| 2D45D7BE4E459CFBEAE4803971F0C6F9 | 1000 |  8050.6360 |
| C81CE9AA555BE59B088B379CC7AE5B40 | 1000 |  6865.4940 |
| BDC4FE903B414203A04E41C7DDA6627D | 1000 | 12751.8960 |
| B1B136047D7C3B6B9125F095363A9D23 |  885 | 13293.2237 |
| 47993DD69888868E92A7CAB2FDE65380 |  880 |  7282.0557 |
| 05C6279D767C7F212619BF4B659D3BAB |  844 | 11474.5438 |
+----------------------------------+------+------------+

Find the TOP N SQL queries with the longest average response time within a certain period of time

You can run the following statement to find the TOP N SQL queries with the longest average response time within a certain period of time:

obclient>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;
              

+----------------------------------+------+------------+
| SQL_ID                           | QPS  | RT         |
+----------------------------------+------+------------+
| 0A3D3DCB3343BBBB10E4B4B9777B77FC |    1 | 53618.0000 |
| A3831961C337545AF5BD1219BE29867A |    1 | 50764.0000 |
| F3DC5EF627DA63AE52044FCE7732267C |    1 | 48497.0000 |
| 39C63F143FDDACAEC090F480789DBCA5 |    1 | 47035.0000 |
| A3BF306B02FF86E76C96C9CEFADBDB7E |    1 | 45553.0000 |
| 7942E8D29BAFBF23EF3E3D29D55F428A |    1 | 45285.0000 |
| 20989A74CC1703664BDE9D6EA7830C24 |    1 | 39143.0000 |
| 80F40791E76C79D3DCD46FEEFFAB338E |    1 | 37654.0000 |
| 07E2FE351E3DD82843E81930B84D3DDE |    1 | 37231.0000 |
| 11B19DB5A1393590ABBE08005C155B2E |    1 | 37139.0000 |
+----------------------------------+------+------------+

Find the TOP N SQL queries with the longest average execution time among all SQL queries

You can execute the following statement to find the TOP N SQL queries with the longest average execution time among all SQL queries:

obclient>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;

*************************** 1. row ***************************
avg_exe_usec: 9795912
      svr_ip: 10.183.76.140
    svr_port: 2882
      sql_id: C5D91E6C772D1B87C32BB3C9ED1435E1
     plan_id: 4668689
*************************** 2. row ***************************
avg_exe_usec: 9435052
      svr_ip: 10.103.229.107
    svr_port: 2882
      sql_id: 3B6EFEEC8332EB2A0822A3EA7B769500
     plan_id: 4692858
*************************** 3. row ***************************
avg_exe_usec: 9335002
      svr_ip: 11.180.113.7
    svr_port: 2882
      sql_id: 3B6EFEEC8332EB2A0822A3EA7B769500
     plan_id: 4683085