OceanBase初體驗之查看OceanBase的執行計劃

前置條件

  • 包含obd和obclient的中控機
  • OceanBase 測試集羣
  • 獨立的測試租戶
  • BenchmarkSQL 工具(可選)

爲了能夠方面的查看複雜SQL的執行計劃,我們先用TPCC模擬一些數據庫負載。

模擬數據庫負載

obd裏面已經集成了tpcc測試工具,需要聯網更新一下插件即可。如果機器不具備外網環境,需要提前下載BenchmarkSQL上傳到測試機中。

BenchmarkSQL下載地址:https://github.com/meiq4096/benchmarksql-5.0

這裏直接用obd裏的tpcc,操作比較簡單:

[ob@localhost ~]$ sudo yum install -y yum-utils
[ob@localhost ~]$ sudo yum-config-manager --add-repo https://mirrors.aliyun.com/oceanbase/OceanBase.repo
[ob@localhost ~]$ sudo yum install obtpcc java

在前面新建的tt租戶下跑一個10倉的負載,時間是5分鐘:

[ob@localhost ~]$ obd test tpcc obtest --tenant=tt --warehouses 10  --run-mins 5
Get local repositories and plugins ok
Open ssh connection ok
Cluster status check ok
Connect obproxy(x.x.x.222:2883) ok
Connect obproxy(x.x.x.222:2883) ok
Optimize for stage build ok
Connect to tenant tt ok
Server check ok
Merge ok
Starting BenchmarkSQL LoadData

driver=com.mysql.jdbc.Driver
conn=jdbc:mysql://x.x.x.222:2883/test?rewriteBatchedStatements=true&allowMultiQueries=true&useLocalSessionState=true&useUnicode=true&characterEncoding=utf-8&socketTimeout=30000000&useSSL=false
user=root@tt
password=***********
warehouses=10
loadWorkers=1
fileLocation (not defined)
csvNullValue (not defined - using default 'NULL')

Worker 000: Loading ITEM
Worker 000: Loading ITEM done
Worker 000: Loading Warehouse      1
Worker 000: Loading Warehouse      1 done
Worker 000: Loading Warehouse      2
Worker 000: Loading Warehouse      2 done
Worker 000: Loading Warehouse      3
Worker 000: Loading Warehouse      3 done
Worker 000: Loading Warehouse      4
Worker 000: Loading Warehouse      4 done
Worker 000: Loading Warehouse      5
Worker 000: Loading Warehouse      5 done
Worker 000: Loading Warehouse      6
Worker 000: Loading Warehouse      6 done
Worker 000: Loading Warehouse      7
Worker 000: Loading Warehouse      7 done
Worker 000: Loading Warehouse      8
Worker 000: Loading Warehouse      8 done
Worker 000: Loading Warehouse      9
Worker 000: Loading Warehouse      9 done
Worker 000: Loading Warehouse     10
Worker 000: Loading Warehouse     10 done
create index ok
finish build ok
check data ok
Optimize for stage test ok
Connect to tenant tt ok
Merge ok
14:20:27,866 [main] INFO   jTPCC : Term-00,
14:20:27,869 [main] INFO   jTPCC : Term-00, +-------------------------------------------------------------+
14:20:27,869 [main] INFO   jTPCC : Term-00,      BenchmarkSQL v5.0
14:20:27,869 [main] INFO   jTPCC : Term-00, +-------------------------------------------------------------+
14:20:27,869 [main] INFO   jTPCC : Term-00,  (c) 2003, Raul Barbosa
14:20:27,869 [main] INFO   jTPCC : Term-00,  (c) 2004-2016, Denis Lussier
14:20:27,872 [main] INFO   jTPCC : Term-00,  (c) 2016, Jan Wieck
14:20:27,872 [main] INFO   jTPCC : Term-00, +-------------------------------------------------------------+
14:20:27,872 [main] INFO   jTPCC : Term-00,
14:20:27,872 [main] INFO   jTPCC : Term-00, db=oceanbase
14:20:27,872 [main] INFO   jTPCC : Term-00, driver=com.mysql.jdbc.Driver
14:20:27,872 [main] INFO   jTPCC : Term-00, conn=jdbc:mysql://x.x.x.222:2883/test?rewriteBatchedStatements=true&allowMultiQueries=true&useLocalSessionState=true&useUnicode=true&characterEncoding=utf-8&socketTimeout=30000000&useSSL=false
14:20:27,872 [main] INFO   jTPCC : Term-00, user=root@tt
14:20:27,872 [main] INFO   jTPCC : Term-00,
14:20:27,872 [main] INFO   jTPCC : Term-00, warehouses=10
14:20:27,873 [main] INFO   jTPCC : Term-00, terminals=100
14:20:27,874 [main] INFO   jTPCC : Term-00, runMins=5
14:20:27,874 [main] INFO   jTPCC : Term-00, limitTxnsPerMin=0
14:20:27,874 [main] INFO   jTPCC : Term-00, terminalWarehouseFixed=true
14:20:27,874 [main] INFO   jTPCC : Term-00,
14:20:27,874 [main] INFO   jTPCC : Term-00, newOrderWeight=45
14:20:27,875 [main] INFO   jTPCC : Term-00, paymentWeight=43
14:20:27,875 [main] INFO   jTPCC : Term-00, orderStatusWeight=4
14:20:27,875 [main] INFO   jTPCC : Term-00, deliveryWeight=4
14:20:27,875 [main] INFO   jTPCC : Term-00, stockLevelWeight=4
14:20:27,875 [main] INFO   jTPCC : Term-00,
14:20:27,875 [main] INFO   jTPCC : Term-00, resultDirectory=my_result_%tY-%tm-%td_%tH%tM%tS
14:20:27,875 [main] INFO   jTPCC : Term-00, osCollectorScript=./misc/os_collector_linux.py
14:20:27,875 [main] INFO   jTPCC : Term-00,
14:20:27,891 [main] INFO   jTPCC : Term-00, copied /home/ob/tmp/props.oceanbase to my_result_2024-03-17_142027/run.properties
14:20:27,891 [main] INFO   jTPCC : Term-00, created my_result_2024-03-17_142027/data/runInfo.csv for runID 1
14:20:27,891 [main] INFO   jTPCC : Term-00, writing per transaction results to my_result_2024-03-17_142027/data/result.csv
14:20:27,892 [main] INFO   jTPCC : Term-00, osCollectorScript=./misc/os_collector_linux.py
14:20:27,892 [main] INFO   jTPCC : Term-00, osCollectorInterval=1
14:20:27,892 [main] INFO   jTPCC : Term-00, osCollectorSSHAddr=null
14:20:27,892 [main] INFO   jTPCC : Term-00, osCollectorDevices=null
14:20:27,953 [main] INFO   jTPCC : Term-00,
14:20:28,239 [main] INFO   jTPCC : Term-00, C value for C_LAST during load: 192                                                          Term-00, 14:25:29,121 [Thread-5] INFO   jTPCC : Term-00, mTOTAL: 1116984    Memory Usage: 95MB / 730MB                                                     14:25:29,121 [Thread-5] INFO   jTPCC : Term-00,                                                                                                   14:25:29,121 [Thread-5] INFO   jTPCC : Term-00, Measured tpmC (NewOrders) = 15185.13
14:25:29,121 [Thread-5] INFO   jTPCC : Term-00, Measured tpmTOTAL = 33746.42
14:25:29,121 [Thread-5] INFO   jTPCC : Term-00, Session Start     = 2024-03-17 14:20:28
14:25:29,121 [Thread-5] INFO   jTPCC : Term-00, Session End       = 2024-03-17 14:25:29
14:25:29,122 [Thread-5] INFO   jTPCC : Term-00, Transaction Count = 168883
TPC-C Result
Measured tpmC (NewOrders)   : 15185.13
Measured tpmTOTAL           : 33746.42
Session Start               : 2024-03-17 14:20:28
Session End                 : 2024-03-17 14:25:29
Transaction Count           : 168883

Recover ok
Trace ID: eeb13702-e424-11ee-aaa8-1c697a639d50
If you want to view detailed obd logs, please run: obd display-trace eeb13702-e424-11ee-aaa8-1c697a639d50

手動部署 BenchmarkSQL 主要步驟:

git clone https://github.com/obpilot/benchmarksql-5.0.git
cd benchmarksql-5.0/run
vi props.ob  # 修改成實際的連接信息,設置主要運行參數
sh runSQL.sh props.ob sql.oceanbase/tableCreates.sql  # 創建表結構
sh runSQL.sh props.ob sql.oceanbase/indexCreates.sql  # 創建索引
sh runLoader.sh props.ob # 裝載數據
sh runBenchmark.sh props.ob # 執行測試

跑tpcc時數據庫的負載情況如下:

查找 TOP SQL

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

SELECT/*+ PARALLEL(15)*/ SQL_ID,PLAN_ID, COUNT(*) AS QPS, AVG(t1.elapsed_time) RT 
                FROM oceanbase.GV$OB_SQL_AUDIT t1 WHERE tenant_id = 1002 AND 
                IS_EXECUTOR_RPC = 0 AND time_to_usec(DATE_SUB(current_timestamp, INTERVAL 10 MINUTE) ) AND
                request_time < time_to_usec (now()) 
                GROUP BY t1.sql_id ORDER BY QPS DESC LIMIT 10;
+----------------------------------+---------+------+----------+
| SQL_ID                           | PLAN_ID | QPS  | RT       |
+----------------------------------+---------+------+----------+
| A460265EC2F0763A15DD27CE9E4E2200 |    4518 | 4771 |  59.6087 |
| 7229213613983BC5FDA15AD11EC70D01 |    3206 | 1440 | 340.2958 |
| E1F2BDA1D7391B757859ED3704E5AFB7 |    3213 | 1440 | 125.9278 |
| 2B5697196EDFE9E97FA3384F581178F5 |    3214 | 1440 |  87.7139 |
| FFFCA4D67EA0A788813031B8BBC3B329 |       0 |  301 |  34.6412 |
| 54B5A5861DAF78F52D9ADFBEE83D35B5 |    3202 |  152 |  94.4276 |
| 7F4A525CC8F849F6527F4911CC4BC348 |    3189 |  152 | 121.2303 |
| BB01ECB7605AE31CBC65E6949D84B235 |    3195 |  152 | 220.9079 |
| E86A0CA8BE3F21A2FBC9F1F9855075A1 |    3184 |  152 | 824.8158 |
| FC3FED8CCB2946DE54F1C5BA3656023C |    3181 |  152 | 501.9671 |
+----------------------------------+---------+------+----------+
10 rows in set (0.012 sec)

查詢某段時間內平均 執行時間 排在 TOP N 的 SQL(可根據執行時間定位慢SQL):

SELECT/*+ PARALLEL(15)*/ SQL_ID,PLAN_ID, COUNT(*) AS QPS, AVG(t1.elapsed_time) RT   
                FROM oceanbase.GV$OB_SQL_AUDIT t1   
                WHERE tenant_id = 1002 AND IS_EXECUTOR_RPC = 0    
                AND request_time > time_to_usec(DATE_SUB(current_timestamp, INTERVAL 10 MINUTE) )
                GROUP BY t1.sql_id ORDER BY RT DESC LIMIT 10;

根據sql_id找到對應的SQL文本:

obclient [(none)]> select query_sql from oceanbase.GV$OB_PLAN_CACHE_PLAN_STAT where sql_id='A460265EC2F0763A15DD27CE9E4E2200';
+---------------------------------------------------------------------------+
| query_sql                                                                 |
+---------------------------------------------------------------------------+
| SELECT i_price, i_name, i_data     FROM bmsql_item     WHERE i_id = 14972 |
+---------------------------------------------------------------------------+
1 row in set (0.005 sec)

分析執行計劃

我們獲取平均執行時間最長的三條SQL,分析他們的實際執行計劃和解析執行計劃。

先找出TOP SQL:

obclient [(none)]> SELECT/*+ PARALLEL(15)*/ SQL_ID,PLAN_ID, COUNT(*) AS QPS, AVG(t1.elapsed_time) RT
    ->                 FROM oceanbase.GV$OB_SQL_AUDIT t1
    ->                 WHERE tenant_id = 1002 AND IS_EXECUTOR_RPC = 0
    ->                 AND request_time > time_to_usec(DATE_SUB(current_timestamp, INTERVAL 10 MINUTE) )
    ->                 GROUP BY t1.sql_id ORDER BY RT DESC LIMIT 3;
+----------------------------------+---------+------+------------+
| SQL_ID                           | PLAN_ID | QPS  | RT         |
+----------------------------------+---------+------+------------+
| 89B757D18634B472A21A0EB8EB83ECE3 |    4035 |    1 | 11747.0000 |
| F59A700FA168324279B0DBC25E19760F |    3217 |    8 |  5686.6250 |
| C9FA41444B84AF63AD4FB24B9252F1A6 |    4025 |    7 |  2694.7143 |
+----------------------------------+---------+------+------------+
3 rows in set (0.012 sec)

第一條sql_id找出對應的SQL文本,再手動獲取執行計劃:

obclient [tpcc]> explain SELECT count(*) AS low_stock FROM (    SELECT s_w_id, s_i_id, s_quantity         FROM bmsql_stock         WHERE s_w_id = 4 AND s_quantity < 17 AND s_i_id IN (            SELECT ol_i_id                 FROM bmsql_district                 JOIN bmsql_order_line ON ol_w_id = d_w_id                  AND ol_d_id = d_id                  AND ol_o_id >= d_next_o_id - 20                  AND ol_o_id < d_next_o_id                 WHERE d_w_id = 4 AND d_id = 9         )     ) ;
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| Query Plan                                                                                                                                               |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
| ================================================================================                                                                         |
| |ID|OPERATOR                            |NAME            |EST.ROWS|EST.TIME(us)|                                                                         |
| --------------------------------------------------------------------------------                                                                         |
| |0 |SCALAR GROUP BY                     |                |1       |269         |                                                                         |
| |1 |└─HASH RIGHT SEMI JOIN              |                |15      |269         |                                                                         |
| |2 |  ├─SUBPLAN SCAN                    |VIEW1           |15      |24          |                                                                         |
| |3 |  │ └─NESTED-LOOP JOIN              |                |15      |24          |                                                                         |
| |4 |  │   ├─TABLE GET                   |bmsql_district  |1       |3           |                                                                         |
| |5 |  │   └─DISTRIBUTED TABLE RANGE SCAN|bmsql_order_line|1       |21          |                                                                         |
| |6 |  └─TABLE RANGE SCAN                |bmsql_stock     |1035    |156         |                                                                         |
| ================================================================================                                                                         |
| Outputs & filters:                                                                                                                                       |
| -------------------------------------                                                                                                                    |
|   0 - output([T_FUN_COUNT(*)]), filter(nil), rowset=256                                                                                                  |
|       group(nil), agg_func([T_FUN_COUNT(*)])                                                                                                             |
|   1 - output(nil), filter(nil), rowset=256                                                                                                               |
|       equal_conds([bmsql_stock.s_i_id = VIEW1.ol_i_id]), other_conds(nil)                                                                                |
|   2 - output([VIEW1.ol_i_id]), filter(nil), rowset=256                                                                                                   |
|       access([VIEW1.ol_i_id])                                                                                                                            |
|   3 - output([bmsql_order_line.ol_i_id]), filter(nil), rowset=256                                                                                        |
|       conds(nil), nl_params_([bmsql_district.d_next_o_id(:0)]), use_batch=false                                                                          |
|   4 - output([bmsql_district.d_next_o_id]), filter([bmsql_district.d_next_o_id > bmsql_district.d_next_o_id - 20]), rowset=256                           |
|       access([bmsql_district.d_next_o_id]), partitions(p0)                                                                                               |
|       is_index_back=false, is_global_index=false, filter_before_indexback[false],                                                                        |
|       range_key([bmsql_district.d_w_id], [bmsql_district.d_id]), range[4,9 ; 4,9],                                                                       |
|       range_cond([bmsql_district.d_w_id = 4], [bmsql_district.d_id = 9])                                                                                 |
|   5 - output([bmsql_order_line.ol_i_id]), filter(nil), rowset=256                                                                                        |
|       access([bmsql_order_line.ol_i_id]), partitions(p0)                                                                                                 |
|       is_index_back=false, is_global_index=false,                                                                                                        |
|       range_key([bmsql_order_line.ol_w_id], [bmsql_order_line.ol_d_id], [bmsql_order_line.ol_o_id], [bmsql_order_line.ol_number]), range(MIN ; MAX),     |
|       range_cond([bmsql_order_line.ol_w_id = 4], [bmsql_order_line.ol_d_id = 9], [bmsql_order_line.ol_o_id >= :0 - 20], [bmsql_order_line.ol_o_id < :0]) |
|   6 - output([bmsql_stock.s_i_id]), filter([bmsql_stock.s_quantity < 17]), rowset=256                                                                    |
|       access([bmsql_stock.s_i_id], [bmsql_stock.s_quantity]), partitions(p0)                                                                             |
|       is_index_back=false, is_global_index=false, filter_before_indexback[false],                                                                        |
|       range_key([bmsql_stock.s_w_id], [bmsql_stock.s_i_id]), range(4,MIN ; 4,MAX),                                                                       |
|       range_cond([bmsql_stock.s_w_id = 4])                                                                                                               |
+----------------------------------------------------------------------------------------------------------------------------------------------------------+
36 rows in set (0.088 sec)

它的真實執行計劃:

select * from oceanbase.V$OB_PLAN_CACHE_PLAN_EXPLAIN where tenant_id=1002 and  plan_id=3217 ;

執行計劃重要信息:

  • OPERATOR,算子的名稱,比如全表掃描TABLE SCAN,真實執行計劃會加上PHY_前綴
  • NAME,算子操作的對象名稱,比如表名、索引名等
  • ROWS,真實執行計劃裏面代表實際數據行數,explain裏面代表估算的行數(EST.ROWS)
  • COST,算子執行時間,單位是微秒,explain裏面的EST.TIME爲估算時間
  • PROPERTY,算子執行的詳細過程

這裏在查詢執行計劃時有一個坑要注意,oceanbase.V$OB_PLAN_CACHE_PLAN_EXPLAIN視圖只能在SQL執行的observer節點上查詢,因爲PLAN CACHE是按節點緩存的,通過obproxy登錄的話往往查不到想要的數據,要直連observer。

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