前置条件

  • 包含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。

OceanBase初体验之查看OceanBase的执行计划的更多相关文章

  1. SQL Server如何查看存储过程的执行计划

    有时候,我们需要查看存储过程的执行计划,那么我们有什么方式获取存储过程的历史执行计划或当前的执行计划呢? 下面总结一下获取存储过程的执行计划的方法. 1:我们可以通过下面脚本查看存储过程的执行计划,但 ...

  2. PL/SQL Developer 查看查询的执行计划

    https://zhuanlan.zhihu.com/p/65771352 通过 PL/SQL Developer 查看查询的执行计划 1 什么是执行计划 执行计划是一条查询语句在 Oracle 中的 ...

  3. mysql explain 查看sql语句执行计划概述

    mysql explain 查看sql语句执行计划概述 id:选择标识符select_type:表示查询的类型.table:输出结果集的表partitions:匹配的分区type:表示表的连接类型po ...

  4. 查看Oracle SQL执行计划的常用方式

    在查看SQL执行计划的时候有很多方式 我常用的方式有三种 SQL> explain plan for 2 select * from scott.emp where ename='KING'; ...

  5. 查看oracle的执行计划

    基于ORACLE的应用系统很多性能问题,是由应用系统SQL性能低劣引起的,所以,SQL的性能优化很重要,分析与优化SQL的性能我们一般通过查看该SQL的执行计划,本文就如何看懂执行计划,以及如何通过分 ...

  6. [转] 多种方法查看Oracle SQL执行计划

    本文转自:http://falchion.iteye.com/blog/616234 一.在线查看执行计划表 如果PLAN_TABLE表不存在,执行$ORACLE_HOME/rdbms/admin/u ...

  7. 详细查看数据库SQL执行计划

    DBCC DROPCLEANBUFFERS 清除数据缓存DBCC FREEPROCCACHE  清除执行计划缓存 SET SHOWPLAN_XML ON 此语句导致 SQL Server 不执行 Tr ...

  8. [转]使用mysql profiles 来查看sql 语句执行计划

    From : http://blog.csdn.net/radkitty/article/details/4632289 要使用该功能,mysql的版本必须在5.0.37版本以上.否则只能使用expl ...

  9. 使用explain查看mysql查询执行计划

    explain语句: 字段解释: type:     all(全表扫描)     ref() possible_keys:     预测使用什么列做为索引 key:     实际使用的key     ...

  10. 查看ORACLE的实际执行计划

    ORACLE的执行计划分为预估执行计划和实际执行计划.其中,你用Toad.PL/SQL Developer.SQL Developer.EXPLAIN PLAN FOR或者SET ATUOTRACE ...

随机推荐

  1. 数据湖-Hudi/IceBerg

  2. Python使用pyecharts库构建股票交易 可交互可视化展示——基于Tushare大数据开放社区

    Python使用pyecharts库构建股票交易 可交互可视化展示--基于Tushare大数据社区 1.前言 大家好!欢迎各位访问我的博客,今天给大家分享的是 python使用pyecharts库构建 ...

  3. Debian安装KDE的方法

    安装KDE指令 sudo apt install kde-plasma-desktop--最小安装:仅安装桌面环境以及基础软件 sudo apt install kde-standard --标准安装 ...

  4. IDEA或Android Studio用书签功能标识代码

    原文地址:IDEA或Android Studio用书签功能标识代码 | Stars-One的杂货小窝 在之前的维护工作中,有时候翻源码的时候,找到了一个文件的某行关键,之后一层层进去之后,又突然不记得 ...

  5. MindSponge分子动力学模拟——自建力场(2024.03)

    技术背景 在MindSponge教程合集中我们已经介绍了很多使用MindSponge进行分子动力学模拟的方法,这里主要介绍在MindSponge中自定义一个力场.在传统的MD软件中,如果你希望去开发一 ...

  6. 阿里二面:Java中锁的分类有哪些?你能说全吗?

    引言 在多线程并发编程场景中,锁作为一种至关重要的同步工具,承担着协调多个线程对共享资源访问秩序的任务.其核心作用在于确保在特定时间段内,仅有一个线程能够对资源进行访问或修改操作,从而有效地保护数据的 ...

  7. 记一次查询优化,mybatis查询oracle卡,直接拿sql数据库查询很快

    调整前 <select id="getList" resultMap="BaseResultMap" parameterType="java.u ...

  8. java实战字符串+栈5:解码字符

    题目: 有形如 (重复字符串)<重复次数n> 的片段,解码后相当于n个重复字符串连续拼接在一起,求展开后的字符串.  求解: public static String zipString( ...

  9. Finereport11 类Excel筛选

    微信公众号:次世代数据技术 关注可了解更多的教程.问题或建议,请公众号留言或联系本人; 微信号:weibw162 本教程视频讲解可以关注本人B站账号进行观看:weibw162 一.需求描述 在使用FI ...

  10. 7 CSS选择器优先级

    7 选择器优先级 所谓CSS优先级,即是指CSS样式在浏览器中被解析的先后顺序.样式表中的特殊性描述了不同规则的相对权重. /* !important > 行内样式>ID选择器 > ...