OceanBaseV3 中統(tǒng)計(jì)信息相關(guān)的系統(tǒng)表很多,一些表明明在文檔中有介紹识藤,但實(shí)際是空的搭盾,本文對(duì)統(tǒng)計(jì)信息相關(guān)的表進(jìn)行總結(jié),先說(shuō)結(jié)論:
- 不建議在 sys 租戶下查看統(tǒng)計(jì)信息,只能查看合并收集的統(tǒng)計(jì)信息秉颗,無(wú)法查看手工收集的統(tǒng)計(jì)信息
- MySQL租戶建議通過(guò) __all_table_stat_v2痢毒、__all_column_stat_v2、__all_histogram_stat_v2 查看統(tǒng)計(jì)信息蚕甥,注意只有在手工收集統(tǒng)計(jì)信息后才會(huì)更新
- Oracle 租戶通過(guò) ALL_TAB_STATISTICS哪替、ALL_TAB_COL_STATISTICS、ALL_TAB_HISTOGRAMS 查看手工收集的統(tǒng)計(jì)信息
1. 統(tǒng)計(jì)信息的收集
先簡(jiǎn)單介紹統(tǒng)計(jì)信息的收集機(jī)制:
- 3.2.x 之前的版本只能通過(guò)每日合并收集統(tǒng)計(jì)信息:只能增量收集菇怀,delete 刪除的數(shù)據(jù)不會(huì)被感知到凭舶,準(zhǔn)確率較低
- 3.2.x 版本后支持手工收集、自動(dòng)收集爱沟。
雖然每日合并收集的統(tǒng)計(jì)信息不是特別準(zhǔn)確帅霜,但不必過(guò)分擔(dān)心優(yōu)化器會(huì)選錯(cuò)執(zhí)行計(jì)劃,因?yàn)橛写鎯?chǔ)層估行接口:很多情況下呼伸,在生成執(zhí)行計(jì)劃的階段身冀,可以直接根據(jù) query range 從存儲(chǔ)層返回需要掃描的行數(shù),不使用統(tǒng)計(jì)信息蜂大,從而得到正確的執(zhí)行計(jì)劃闽铐。
MySQL 租戶手工收集統(tǒng)計(jì)信息的方法:
--手工收集 sbtest1 表的 id,k 字段直方圖統(tǒng)計(jì)信息,桶個(gè)數(shù)為8個(gè)
analyze table sbtest.sbtest1 UPDATE HISTOGRAM ON id,k WITH 8 BUCKETS;
Oracle 租戶手工收集統(tǒng)計(jì)信息的方法:
盡量用 dbms_stats 包收集奶浦,不要用 analyze 命令收集:
--收集用戶 USERA 的表 T1 的統(tǒng)計(jì)信息兄墅,并行度為 64,只收集數(shù)據(jù)分布不均勻的列的直方圖澳叉。
call dbms_stats.gather_table_stats('USERA','T1',degree=>'64',granularity=>'all',method_opt=>'FOR ALL COLUMNS SIZE SKEWONLY');
--收集所有字段的統(tǒng)計(jì)信息(不收集直方圖需要在 method_opt 中指定 size 1隙咸,如果不指定,默認(rèn)直方圖桶的個(gè)數(shù)是 256)
call dbms_stats.gather_table_stats('USERA','T1',method_opt=>'for all columns size 1');
--收集指定字段的統(tǒng)計(jì)信息(不收集直方圖)
call dbms_stats.gather_table_stats('USERA','T1',method_opt=>'for columns c3 size 1');
--刪除統(tǒng)計(jì)信息
call dbms_stats.delete_table_stats('USERA', 'T1');
2. sys租戶查看統(tǒng)計(jì)信息
查看表級(jí)統(tǒng)計(jì)信息:
-- 會(huì)輸出分區(qū)信息
select
b.table_name,
a.tenant_id,
a.partition_id,
a.role,
a.row_count,
a.data_size,
a.gmt_modified
from
__all_virtual_meta_table a
join gv$table b on a.table_id = b.table_id
where
b.table_name = 't1'
and b.tenant_id = 1003
and b.database_name = 'usera'
and a.role = 1;
-- 如果有多個(gè)分區(qū)成洗,可以這樣匯總
select
b.table_name,
sum(a.row_count)
from
__all_virtual_meta_table a
join gv$table b on a.table_id = b.table_id
where
b.table_name = 't1'
and b.tenant_id = 1003
and b.database_name = 'usera'
and a.role = 1
group by
b.table_name;
查看列級(jí)統(tǒng)計(jì)信息:
select
b.table_name,
a.tenant_id,
a.partition_id,
c.column_name,
a.num_distinct,
a.num_null,
a.gmt_modified
from
__all_virtual_column_statistic a
join gv$table b on a.table_id = b.table_id
join __all_virtual_column c on a.table_id = c.table_id
and a.column_id = c.column_id
where
b.table_name = 't1'
and b.tenant_id = 1003
and b.database_name = 'usera'
and c.column_name ='c1';
3. MySQL 租戶查看統(tǒng)計(jì)信息
查看表級(jí)統(tǒng)計(jì)信息:
select b.table_name,a.partition_id,a.row_cnt,a.avg_row_len,a.macro_blk_cnt,a.micro_blk_cnt,a.gmt_modified
from oceanbase.__all_table_stat_v2 a
join oceanbase.__all_table_v2 b on a.table_id=b.table_id;
+------------+--------------+---------+-------------+---------------+---------------+----------------------------+
| table_name | partition_id | row_cnt | avg_row_len | macro_blk_cnt | micro_blk_cnt | gmt_modified |
+------------+--------------+---------+-------------+---------------+---------------+----------------------------+
| t4_part_g | -1 | 999999 | 242 | 100 | 14099 | 2022-12-29 18:02:05.241812 |
| t4_part_g | 0 | 9999 | 242 | 1 | 141 | 2022-12-29 18:02:05.241812 |
| t4_part_g | 1 | 90000 | 242 | 9 | 1268 | 2022-12-29 18:02:05.241812 |
| t4_part_g | 2 | 50000 | 242 | 5 | 705 | 2022-12-29 18:02:05.241812 |
| t4_part_g | 3 | 50000 | 242 | 5 | 705 | 2022-12-29 18:02:05.241812 |
| t4_part_g | 4 | 50000 | 242 | 5 | 705 | 2022-12-29 18:02:05.241812 |
| t4_part_g | 5 | 50000 | 242 | 5 | 705 | 2022-12-29 18:02:05.241812 |
...
查看列級(jí)統(tǒng)計(jì)信息五督,__all_column_stat_v2表:
- 分區(qū)表 partition_id=-1 即為表整體的統(tǒng)計(jì)信息,否則表示每個(gè)分區(qū)的信息瓶殃;非分區(qū)表 partition_id=0充包。如果不想看分區(qū)級(jí)別的信息,可以用 object_type=1 篩選遥椿。
- sample_size 采樣大小
- bucket_cnt 直方圖桶的個(gè)數(shù)
- 合并后不更新基矮,只有手工收集統(tǒng)計(jì)信息才更新
select b.table_name,a.partition_id,c.column_name,a.sample_size,a.bucket_cnt,a.distinct_cnt,a.null_cnt,a.gmt_modified
from __all_column_stat_v2 a
join __all_table_v2 b on a.table_id=b.table_id
join __all_column c on a.table_id=c.table_id and a.column_id=c.column_id
where a.object_type=1;
+------------+--------------+-------------+-------------+------------+--------------+----------+----------------------------+
| table_name | partition_id | column_name | sample_size | bucket_cnt | distinct_cnt | null_cnt | gmt_modified |
+------------+--------------+-------------+-------------+------------+--------------+----------+----------------------------+
| t4_part_g | -1 | id | 15369 | 30 | 930724 | 0 | 2022-12-29 18:02:06.141816 |
| t4_part_g | -1 | k | 15369 | 30 | 175614 | 0 | 2022-12-29 18:02:06.141816 |
| t4_part_g | -1 | c | 15369 | 30 | 976979 | 0 | 2022-12-29 18:02:06.141816 |
| t4_part_g | -1 | pad | 15369 | 30 | 987873 | 0 | 2022-12-29 18:02:06.141816 |
| sbtest1 | 0 | id | 4146 | 8 | 9816 | 0 | 2023-01-09 15:16:01.523297 |
| sbtest1 | 0 | k | 4146 | 8 | 7546 | 0 | 2023-01-09 15:16:01.523297 |
+------------+--------------+-------------+-------------+------------+--------------+----------+----------------------------+
6 rows in set (0.029 sec)
直方圖查看:
select b.table_name,a.partition_id,c.column_name,a.endpoint_num,a.endpoint_normalized_value,a.gmt_modified
from __all_histogram_stat_v2 a
join __all_table_v2 b on a.table_id=b.table_id
join __all_column c on a.table_id=c.table_id and a.column_id=c.column_id
where b.table_name='sbtest1' and partition_id<=0;
4. Oracle 租戶查看統(tǒng)計(jì)信息
Oracle 租戶的統(tǒng)計(jì)信息和 Oracle 保持一致,需要手工收集才可以查看冠场,沒(méi)什么套路:
- ALL_TAB_STATISTICS:表級(jí)統(tǒng)計(jì)信息
- ALL_TAB_COL_STATISTICS:查看列級(jí)統(tǒng)計(jì)信息
- ALL_TAB_HISTOGRAMS:查看直方圖統(tǒng)計(jì)信息