【OceanBase系列】—— 常用 SQL

发布于:2024-04-28 ⋅ 阅读:(30) ⋅ 点赞:(0)

作者简介:

花名:绪宁,OceanBase 数据库解决方案架构师

对使用OB过程中常用的一些SQL进行了整理,对应的版本是 4.x。

集群信息

查看版本

show variables like 'version_comment';

查看集群ID和集群名

show parameters like '%cluster%';

查看当前服务器信息

select * from DBA_OB_SERVERS;

查看集群的zone信息

SELECT * FROM dba_ob_zones;

查看集群支持的字符集

select * from information_schema.collations;

租户信息

查看租户创建基本信息

show create tenant xxx;

查看对应 Unit 的配置。

SELECT * FROM oceanbase.dba_ob_units;

查看租户对应的资源池(可以加 tenant_id 筛选)。

SELECT * FROM oceanbase.dba_ob_resource_pools;

查看租户基本信息。

SELECT * FROM oceanbase.dba_ob_tenants;

当前集群内的租户

select tenant_id,tenant_name,primary_zone,compatibility_mode from oceanbase.__all_tenant;

RS 相关

切换rs leader

alter system switch rootservice leader zone='z1';

查看 RS 任务

select * from __all_rootservice_event_history order by 1 desc limit 10;

查看 rs 列表

show parameters like '%rootservice_list%';

查看 rs leader,WITH_ROOTSERVER=yes

SELECT * FROM oceanbase.DBA_OB_SERVERS;

资源分配

资源分配查询

服务器资源分配

select * from GV$OB_SERVERS;

各租户资源分配

select t1.name resource_pool_name, t2.`name` unit_config_name, 
t2.max_cpu, t2.min_cpu, 
round(t2.memory_size/1024/1024/1024,2) mem_size_gb,
round(t2.log_disk_size/1024/1024/1024,2) log_disk_size_gb, t2.max_iops, 
t2.min_iops, t3.unit_id, t3.zone, concat(t3.svr_ip,':',t3.`svr_port`) observer,
t4.tenant_id, t4.tenant_name
from __all_resource_pool t1
join __all_unit_config t2 on (t1.unit_config_id=t2.unit_config_id)
join __all_unit t3 on (t1.`resource_pool_id` = t3.`resource_pool_id`)
left join __all_tenant t4 on (t1.tenant_id=t4.tenant_id)
order by t1.`resource_pool_id`, t2.`unit_config_id`, t3.unit_id;

查看 observer 内存、磁盘配置大小

select zone,svr_ip,svr_port,name,value 
from __all_virtual_sys_parameter_stat 
where 
name in ('memory_limit','memory_limit_percentage','system_memory','datafile_size','datafile_disk_percentage') 
order by svr_ip,svr_port;

容量使用统计

默认情况下统计的是三/多副本的大小,可以通过增加 role 来获取单副本大小。

统计租户的大小

select t.tenant_name,
round(sum(t2.data_size)/1024/1024/1024,2) as data_size_gb,
round(sum(t2.required_size)/1024/1024/1024,2) as required_size_gb
from dba_ob_tenants t,cdb_ob_table_locations t1,cdb_ob_tablet_replicas t2
where t.tenant_id=t1.tenant_id
and t1.svr_ip=t2.svr_ip
and t1.tenant_id=t2.tenant_id
and t1.ls_id=t2.ls_id
and t1.tablet_id=t2.tablet_id
-- and t1.role='leader'
group by t.tenant_name
order by 3 desc;

统计库的大小

select t1.database_name,
round(sum(t2.data_size)/1024/1024/1024,2) as data_size_gb,
round(sum(t2.required_size)/1024/1024/1024,2) as required_size_gb
from dba_ob_tenants t,cdb_ob_table_locations t1,cdb_ob_tablet_replicas t2
where t.tenant_id=t1.tenant_id
and t1.svr_ip=t2.svr_ip
and t1.tenant_id=t2.tenant_id
and t1.ls_id=t2.ls_id
and t1.tablet_id=t2.tablet_id
-- and t1.role='leader'
and t.tenant_name='test1'
group by t1.database_name
order by 3 desc;

统计表/索引的大小

select t1.table_name,
round(sum(t2.data_size)/1024/1024/1024,2) as data_size_gb,
round(sum(t2.required_size)/1024/1024/1024,2) as required_size_gb
from dba_ob_tenants t,cdb_ob_table_locations t1,cdb_ob_tablet_replicas t2
where t.tenant_id=t1.tenant_id
and t1.svr_ip=t2.svr_ip
and t1.tenant_id=t2.tenant_id
and t1.ls_id=t2.ls_id
and t1.tablet_id=t2.tablet_id
-- and t1.role='leader'
and t.tenant_name='test1'
and t1.database_name='sbtest'
and t1.table_name='sbtest1'
group by t1.table_name
order by 3 desc;

统计表对应的分区大小

select t1.table_name,t1.partition_name,
round(sum(t2.data_size)/1024/1024/1024,2) as data_size_gb,
round(sum(t2.required_size)/1024/1024/1024,2) as required_size_gb
from dba_ob_tenants t,cdb_ob_table_locations t1,cdb_ob_tablet_replicas t2
where t.tenant_id=t1.tenant_id
and t1.svr_ip=t2.svr_ip
and t1.tenant_id=t2.tenant_id
and t1.ls_id=t2.ls_id
and t1.tablet_id=t2.tablet_id
and t1.role='leader'
and t.tenant_name='test1'
and t1.database_name='sbtest'
and t1.table_name='sbtest1_part'
group by t1.table_name,t1.partition_name;

内存占用

租户内存参数:

show parameters where name in ('memstore_limit_percentage','freeze_trigger_percentage');

租户内存持有:

select TENANT_ID,SVR_IP,SVR_PORT,HOLD/1024/1024/1024,FREE/1024/1024/1024 
from oceanbase.GV$OB_TENANT_MEMORY 
where tenant_id =1002;

租户内存模块占用:

select * from V$OB_MEMORY where tenant_id=1002;

memstore占用:

select * from V$OB_MEMSTORE where tenant_id=1002;

总体实际占用的memory 大小以及大小限制:

select * from oceanbase.GV$OB_SERVERS;

memory_limit字段代表实际的memory_limit大小。

MEM_CAPACITY 是 memory_limit - system_memory

  • 内存资源:包括两个配置,MIN_MEMORY和MAX_MEMORY,他们含义如下:
    • MIN_MEMORY:表示为租户分配的最小内存规格,observer上,所有租户的MIN_MEMORY的总和不能超过物理可用内存大小MEM_CAPACITY
    • MAX_MEMORY:表示为租户分配的最大内存规格,observer上,所有租户的MAX_MEMORY的总和不能超过物理可用内存的超卖值:MEM_CAPACITY * resource_hard_limit

表分区和日志流分布

表及分区

查看当前所有表详情

select * from __all_table

查看所有分区详情

select * from __all_part

查看表及分区leader分布

select * from oceanbase.DBA_OB_TABLE_LOCATIONS where ROLE='LEADER'  and table_name='xxx'

查看表及分区的预估数据量

select * from OCEANBASE.DBA_TAB_STATISTICS

日志流

日志流分布

select SVR_IP,ROLE,count(*) from CDB_OB_LS_LOCATIONS group by SVR_IP,ROLE;

查看日志流状态

select * from GV$OB_LOG_STAT;

查看日志流详情

select svr_ip,svr_port,tenant_id,ls_id,replica_type,ls_state,tablet_count from __all_virtual_ls_info;

常用hint

OceanBase Hint 用法

  • 支持不带参数,如 /*+ FUNC */
  • 支持带参数,如 /*+ FUNC(param) */
  • 多个hint可以写到同一个注释中,用逗号分隔,如/*+ FUNC1, FUNC2(param) */
  • SELECT语句的hint必须近接在关键字SELECT之后,其他词之前。如:SELECT /*+ FUNC */ …
  • UPDATE, DELETE 语句的 hint 必须紧接在关键字 UPDATE,DELETE 之后

Hint 参数

Hint 相关参数名称、语义和语法如下表所示。

名称 语法 语义
NO_REWRITE NO_REWRITE 禁止 SQL 改写。
READ_CONSISTENCY READ_CONSISTENCY (WEAK[STRONGFROZEN]) 读一致性设置(弱/强)。
INDEX_HINT /*+ INDEX(table_name index_name) */ 设置表索引。
QUERY_TIMEOUT QUERY_TIMEOUT(INTNUM) 设置超时时间。
LOG_LEVEL LOG_LEVEL([']log_level[']) 设置日志级别,当设置模块级别语句时候,以第一个单引号(')作为开始,第二个单引号(')作为结束;例如'DEBUG'。
LEADING LEADING([qb_name] TBL_NAME_LIST) 设置联接顺序。
ORDERED ORDERED 设置按照 SQL 中的顺序进行联接。
FULL FULL([qb_name] TBL_NAME) 设置表访问路径为主表等价于 INDEX(TBL_NAME PRIMARY)。
USE_PLAN_CACHE USE_PLAN_CACHE(NONE[DEFAULT]) 设置是否使用计划缓存:NONE:表示不使用计划缓存。DEFAULT:表示按照服务器本身的设置来决定是否使用计划缓存。
USE_MERGE USE_MERGE([qb_name] TBL_NAME_LIST) 设置指定表在作为右表时使用 Merge Join。
USE_HASH USE_HASH([qb_name] TBL_NAME_LIST) 设置指定表在作为右表时使用 Hash Join。
NO_USE_HASH NO_USE_HASH([qb_name] TBL_NAME_LIST) 设置指定表在作为右表时不使用 Hash Join。
USE_NL USE_NL([qb_name] TBL_NAME_LIST) 设置指定表在作为右表时使用 Nested Loop Join。
USE_BNL USE_BNL([qb_name] TBL_NAME_LIST) 设置指定表在作为右表时使用 Block Nested Loop Join
USE_HASH_AGGREGATION USE_HASH_AGGREGATION([qb_name]) 设置聚合算法为 Hash。例如 Hash Group By 或者 Hash Distinct。
NO_USE_HASH_AGGREGATION NO_USE_HASH_AGGREGATION([qb_name]) 设置 Aggregate 方法不使用 Hash Aggregate,使用 Merge Group By 或者 Merge Distinct。
USE_LATE_MATERIALIZATION USE_LATE_MATERIALIZATION 设置使用晚期物化。
NO_USE_LATE_MATERIALIZATION NO_USE_LATE_MATERIALIZATION 设置不使用晚期物化。
TRACE_LOG TRACE_LOG 设置收集 Trace 记录用于 SHOW TRACE 展示。
QB_NAME QB_NAME( NAME ) 设置 Query Block 的名称。
PARALLEL PARALLEL(INTNUM) 设置分布式执行并行度。
TOPK TOPK(PRECISION MINIMUM_ROWS) 设置模糊查询的精度和最小行数。 其中 PRECSION 为整型,取值范围[0,100],表示模糊查询的行数百分比;MINIMUM_ROWS 为最小返回行数。

sql audit

查询资源占用最多的SQL

select SQL_ID, avg(ELAPSED_TIME), 
avg(QUEUE_TIME), 
avg(ROW_CACHE_HIT + BLOOM_FILTER_CACHE_HIT + BLOCK_CACHE_HIT + DISK_READS) avg_logical_read, 
avg(execute_time) avg_exec_time, 
count(*) cnt, 
avg(execute_time - TOTAL_WAIT_TIME_MICRO ) avg_cpu_time, 
avg( TOTAL_WAIT_TIME_MICRO ) avg_wait_time, 
WAIT_CLASS, avg(retry_cnt) from v$OB_SQL_AUDIT 
group by 1 
order by avg_exec_time * cnt desc 
limit 10;

最近100s某个租户的TOP SQL耗时监控

select /*+read_consistency(weak),query_timeout(100000000)*/ SQL_ID,count(1),avg(ELAPSED_TIME),avg(EXECUTE_TIME),avg(QUEUE_TIME),avg(AFFECTED_ROWS),avg(GET_PLAN_TIME) 
from gv$ob_sql_audit  
where time_to_usec(now(6))-request_time <1000000000 
and tenant_name='test_tenant' 
group by SQL_ID order by avg(ELAPSED_TIME)*count(1) desc limit 20;

某个时间段请求次数排在 TOP-N 的 SQL

select SQL_ID, count(*) as QPS, avg(t1.elapsed_time) RT
from oceanbase.gv$ob_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中消耗CPU最多的sql(或者不做聚合直接查询)

select sql_id, substr(query_sql, 1, 200) as query_sql, 
      sum(elapsed_time - queue_time) sum_t, count(*) cnt, 
      avg(get_plan_time), avg(execute_time)    
from oceanbase.gv$ob_sql_audit     
where   tenant_id = 1001      
       and request_time > (time_to_usec(now()) - 10000000)     
       and request_time < time_to_usec(now())
group by sql_id order by sum_t desc   limit 10;

巡检、运维相关

自增值

获取每张表的自增值(下一个自增值)

select 
  a.table_name,b.AUTO_INCREMENT_VALUE 
from 
  oceanbase.__all_table a, oceanbase.DBA_OB_AUTO_INCREMENT b 
where 
  a.table_id=b.AUTO_INCREMENT_KEY 
  and a.autoinc_column_id=b.COLUMN_ID 
  and a.TABLE_NAME='t3';

DDL

查询DDL进度

mysql> select * from oceanbase.gv$session_longops\G;
  1. sid:现在没有填值,为默认的 -1。
  2. trace_id: OBServer 程序日志的ID,可以用该ID来搜索相关的日志文件。
  3. opname:建索引时,会展示 create index 信息。
  4. target:建索引时,展示正在创建的索引名。
  5. svr_ip: 调度任务在哪个 OBServer 执行。
  6. svr_port:调度任务在哪个 OBServer 执行。
  7. start_time:索引构建开始时间,这里只精确到日期,跟 Oracle 是兼容的。
  8. elapsed_seconds: 索引构建执行的时间,单位为秒。
  9. time_remaining: 兼容 Oracle 的字段,暂时还没有实现剩余时间预测的能力。
  10. last_update_time: 统计信息收集的时间,也是精确到日期,跟 Oracle 是兼容的。
  11. message:里面包含了多个信息,ENANT_ID为租户 ID,TASK_ID为DDL 的任务 ID,STATUS 为 DDL 执行到的状态,REPLICA BUILD 指的是数据补全阶段,索引数据补全主要分为扫描主表数据,排序,写入到索引表阶段,三个阶段处理的行数分别对应于ROW_SCANNED, ROW_SORTED 和 ROW_INSERTED,因排序阶段可能会进行多轮归并,所以ROW_SORTED 的行数通常比 ROW_SCANNED 和 ROW_INSERTED 要多。