本文主要介绍查询 OceanBase 数据库中常用的运维 SQL 语句。
查看服务器资源配置
可以通过以下 SQL 查看当前的服务器资源配置。
其中,__all_server
表记录了了各 OBServer 的状态,__all_virtual_server_stat
记录了各 OBServer 的 CPU、内存与磁盘使用量。
obclient> SELECT a.zone,concat(a.svr_ip,':',a.svr_port) observer, cpu_total, (cpu_total-cpu_assigned) cpu_free, round(mem_total/1024/1024/1024) mem_total_gb, round((mem_total-mem_assigned)/1024/1024/1024) mem_free_gb, usec_to_time(b.last_offline_time) last_offline_time, usec_to_time(b.start_service_time) start_service_time, b.status
FROM __all_virtual_server_stat a JOIN __all_server b ON (a.svr_ip=b.svr_ip AND a.svr_port=b.svr_port)
ORDER BY a.zone, a.svr_ip;
+-------+---------------------+-----------+----------+--------------+-------------+----------------------------+----------------------------+--------+
| zone | observer | cpu_total | cpu_free | mem_total_gb | mem_free_gb | last_offline_time | start_service_time | status |
+-------+---------------------+-----------+----------+--------------+-------------+----------------------------+----------------------------+--------+
| zone1 | xxx.xx.xxx.xxx:2882 | 62 | 59.5 | 30 | 23 | 1970-01-01 08:00:00.000000 | 2021-06-10 15:54:12.800830 | active |
| zone2 | xxx.xx.xxx.xxx:2882 | 62 | 59.5 | 30 | 23 | 1970-01-01 08:00:00.000000 | 2021-06-10 15:54:12.816101 | active |
| zone3 | xxx.xx.xxx.xxx:2882 | 62 | 59.5 | 30 | 23 | 1970-01-01 08:00:00.000000 | 2021-06-10 15:54:12.817759 | active |
+-------+---------------------+-----------+----------+--------------+-------------+----------------------------+----------------------------+--------+
3 rows in set (0.01 sec)
查看资源池配置
可以通过以下 SQL 查看当前的资源池配置。
其中,__all_resource_pool
表记录了资源池的信息,__all_unit_config
记录了资源单元的配置,__all_unit
记录了资源单元的列表。
obclient> SELECT t1.name resource_pool_name, t2.`name` unit_config_name, t2.max_cpu, t2.min_cpu, round(t2.max_memory/1024/1024/1024) max_mem_gb, round(t2.min_memory/1024/1024/1024) min_mem_gb, 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;
查看 RootService 执行的管理任务
可以通过以下 SQL 查看 RootService 最近执行的管理任务。
__all_rootservice_event_history
用于记录集群级的历史事件,如合并、Server 上下线、负载均衡任务执行等。配置项 ob_event_history_recycle_interval
控制该表中记录历史事件的保留时间,默认为 7 天。
有关 ob_event_history_recycle_interval
配置项的详细信息,请参见《OceanBase 数据库 参考指南》中的 系统配置项 章节。
obclient> SELECT DATE_FORMAT(gmt_create, '%b%d %H:%i:%s') gmt_create_ , module, event, name1, value1, name2, value2, rs_svr_ip
FROM __all_rootservice_event_history
WHERE 1 = 1
ORDER BY gmt_create DESC
LIMIT 20;
如何查看用户表
可以通过以下 SQL 查看指定租户中所有用户表。
其中,gv$tenant
视图记录了租户信息,gv$database
记录了数据库信息,gv$table
记录了表信息,__all_virtual_meta_table
表记录了表与分区信息。$tenant_id
表示租户 ID。
obclient> SELECT t1.tenant_id,t1.tenant_name,t2.database_name,t3.table_id,t3.table_Name,t3.tablegroup_id,t3.part_num,t4.partition_Id,t4.zone,t4.svr_ip,t4.role, round(t4.data_size/1024/1024) data_size_mb
FROM `gv$tenant` t1
JOIN `gv$database` t2 ON (t1.tenant_id = t2.tenant_id)
JOIN gv$table t3 ON (t2.tenant_id = t3.tenant_id AND t2.database_id = t3.database_id AND t3.index_type = 0)
LEFT JOIN `__all_virtual_meta_table` t4 ON (t2.tenant_id = t4.tenant_id AND ( t3.table_id = t4.table_id OR t3.tablegroup_id = t4.table_id ) AND t4.role IN (1))
WHERE t1.tenant_id = $tenant_id
ORDER BY t3.tablegroup_id, t4.partition_Id, t3.table_name ;
查看指定用户 SQL 命令执行情况
可以通过以下 SQL 查看指定用户执行的 SQL 命令的执行情况。
其中 TENANT_ID
表示租户 ID,USER_NAME
表示用户名,IP_ADDRESS
表示执行 SQL 的节点的 IP 地址。
obclient> SELECT /*+ read_consistency(weak) ob_querytimeout(100000000) */ substr(usec_to_time(request_time),1,19) request_time_, s.svr_ip, s.client_Ip, s.sid,s.tenant_id, s.tenant_name, s.user_name, s.db_name, s.query_sql, s.affected_rows, s.return_rows, s.ret_code, s.event, s.elapsed_time, s.queue_time, s.execute_time, round(s.request_memory_used/1024/1024,2) req_mem_mb, plan_type, is_executor_rpc, is_inner_sql
FROM gv$sql_audit s
WHERE s.tenant_id=<TENANT_ID> AND user_name='<USER_NAME>' AND svr_ip IN ('<IP_ADDRESS>')
ORDER BY request_time DESC
LIMIT 100;
查看物理机资源使用情况
可以通过以下 SQL 查看指定租户在物理机上的资源使用情况。
其中 gv$memstore
视图记录了租户的内存使用情况,$tenant_id
表示租户 ID。
obclient> SELECT tenant_id, ip, round(active/1024/1024) active_mb, round(total/1024/1024) total_mb, round(freeze_trigger/1024/1024) freeze_trg_mb, round(mem_limit/1024/1024) mem_limit_mb
, freeze_cnt , round((active/freeze_trigger),2) freeze_pct, round(total/mem_limit, 2) mem_usage
FROM `gv$memstore`
WHERE $tenant_id IN (1001)
ORDER BY tenant_id, ip;
查看合并进度
可以通过以下 SQL 查看合并进度。
其中查询结果中的 merge_process
列表示当前合并进度的百分比。
obclient> SELECT ZONE,svr_ip,major_version,min_version,ss_store_count,merged_ss_store_count,modified_ss_store_count,merge_start_time,merge_finish_time,merge_process
FROM __all_virtual_partition_sstable_image_info;
查看分区信息
可以通过以下 SQL 查询指定租户的分区信息。
其中 __all_virtual_partition_info
记录了分区信息,$tenant_id
表示租户 ID。
obclient> SELECT table_id, partition_idx, usec_to_time(min_log_service_ts), TIME_TO_SEC( now())-TIME_TO_SEC(usec_to_time(min_log_service_ts)) delta_time
FROM __all_virtual_partition_info
WHERE tenant_id=$tenant_id;
想了解更多干货,可通过下方扫码关注
可扫码添加上智启元官方客服微信👇
I cherish how your character shines through in your writing. It establishes an immediate bond.