如何用SQL语句来查询表或索引的行存/列存存储方式|OceanBase 用户问题集锦

一、问题背景

自OceanBase 4.3.0版本起,支持了列存引擎,允许表和索引以行存、纯列存或行列冗余的形式创建,且这些存储方式可以自由组合。除了使用 show create table命令来查看表和索引的存储类型外,也有用户询问如何通过SQL语句来查询表或索引的存储方式。那么,具体该如何操作呢?

1735278087

二、测试表

说明:这里仅列举了部分组合,还有其他的组合应该也是类似的,不再赘述,欢迎测试,拍砖。

-- 行存表,行存索引
create table  t1(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t1_c2(c2)) partition by hash(c1) partitions 3;
create table  t2(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t2_c2(c2)) partition by hash(c1) partitions 3 with column group(all columns);
create table  t3(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t3_c2(c2) with column group(all columns)) partition by hash(c1) partitions 3 with column group(all columns);

-- 行存表,纯列存索引
create table  t4(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t4_c2(c2) with column group(each column)) partition by hash(c1) partitions 3;

-- 行存表,行列混合索引
create table  t5(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t5_c2(c2) with column group(each column,all columns)) partition by hash(c1) partitions 3;

-- 纯列存表,行存索引
create table  t6(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t6_c2(c2) with column group(all columns)) partition by hash(c1) partitions 3 with column group(each column);
create table  t7(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t7_c2(c2)) partition by hash(c1) partitions 3 with column group(each column);

-- 行列混合表,行列混合索引
create table  t8(c1 int,c2 int,c3 int,c4 int,c5 int,primary key(c1),key idx_t8_c2(c2) with column group(each column,all columns)) partition by hash(c1) partitions 3 with column group(each column,all columns);

三、摸索

从列存相关的语法上可以看出,引入列存后新增加了 with column group (xxx) 的关键字,可以尝试搜一下哪些表的列上涉及了 column_group 相关的字段,从下面的结果看目前并没有标准表或者视图提供这样的信息。

MySQL [oceanbase]> select distinct table_name from __all_virtual_table 
where table_id in (select distinct table_id from __all_virtual_column 
                   where column_name like '%column_group%');
+--------------------------------------------+
| table_name                                 |
+--------------------------------------------+
| __all_table_history                        |
| __all_column_group                         |
| __all_column_group_history                 |
| __all_column_group_mapping                 |
| __all_column_group_mapping_history         |
| __all_virtual_core_all_table               |
| __all_virtual_table                        |
| __all_virtual_table_history                |
| __all_virtual_column_group                 |
| __all_virtual_column_group_mapping         |
| __all_virtual_column_group_history         |
| __all_virtual_column_group_mapping_history |
+--------------------------------------------+
12 rows in set (0.97 sec)

从 __all_virtual_column_group 表的 column_group_type 字段,凭感觉可以标识。

MySQL [oceanbase]> desc __all_virtual_column_group;
+-------------------+--------------+------+-----+---------+-------+
| Field             | Type         | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+-------+
| tenant_id         | bigint(20)   | NO   | PRI | NULL    |       |
| table_id          | bigint(20)   | NO   | PRI | NULL    |       |
| column_group_id   | bigint(20)   | NO   | PRI | NULL    |       |
| gmt_create        | timestamp(6) | NO   |     | NULL    |       |
| gmt_modified      | timestamp(6) | NO   |     | NULL    |       |
| column_group_name | varchar(389) | NO   |     |         |       |
| column_group_type | bigint(20)   | NO   |     | NULL    |       |
| block_size        | bigint(20)   | NO   |     | NULL    |       |
| compressor_type   | bigint(20)   | NO   |     | NULL    |       |
| row_store_type    | bigint(20)   | NO   |     | NULL    |       |
+-------------------+--------------+------+-----+---------+-------+
10 rows in set (0.02 sec)

从代码 src/share/schema/ob_schema_struct.h 找到 column_group 类型的枚举值。

enum ObColumnGroupType : uint8_t
{
  DEFAULT_COLUMN_GROUP = 0,
  ALL_COLUMN_GROUP,
  ROWKEY_COLUMN_GROUP,
  SINGLE_COLUMN_GROUP,
  NORMAL_COLUMN_GROUP,
  MAX_COLUMN_GROUP
};

四、结论

经过测试发现如何标识 表或者索引是行存/纯列存/行列冗余的方式存储,这里之所以给 "结论"使用引号扩起来,原因:

1、受限于自己测试的 case 可能不完善,存在错误的情况,欢迎一起测试,交流。

2、随着版本的迭代,针对枚举值可能会有调整/比如增删等,应以实际版本中的枚举值为准。

  • 某个租户下当同一个 table_id 的 column_group_type 包含3 但是不包含1,输出:纯列存表
  • 某个租户下当同一个 table_id 的 column_group_type 包含1 和 3,输出:行列混合表
  • 其他情况均输出 :纯行存表

五、查询sql和结果

说明:可以按需调整 tenant_name/database_name

sql_1(混合查询)

select 
    t1.tenant_name,
    t2.database_name,
    case 
        when t2.table_type = 'user table' then t2.table_name
        when t2.table_type = 'index' then t2.index_name
    end as table_name,
    t2.table_id,
    t2.data_table_id,
    t2.table_type,
    case
        when t2.table_type = 'user table' then
            case
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0 then '纯列存表'
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) > 0 then '行列混合表'
                else '纯行存表'
            end
        when t2.table_type = 'index' then
            case
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0 then '纯列存索引'
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) > 0 then '行列混合索引'
                else '纯行存索引'
            end
    end as storage_type,
    coalesce(t3.table_name, null) as main_table_name
from 
    __all_virtual_column_group cg
join 
    dba_ob_tenants t1 on cg.tenant_id = t1.tenant_id
join 
    cdb_ob_table_locations t2 on cg.tenant_id = t2.tenant_id and cg.table_id = t2.table_id
left join 
    cdb_ob_table_locations t3 on t2.data_table_id = t3.table_id
where 
    t1.tenant_name in ('test1','test7')
    and t2.database_name in ('row_column_db','db100','db600')
    and t2.table_type in ('user table', 'index')
group by 
    t1.tenant_name, t2.database_name, t2.table_name, t2.table_id, t2.data_table_id, t2.table_type, main_table_name
order by t1.tenant_name,t2.database_name, t2.table_name;

sql1_查询结果
+-------------+---------------+------------+----------+---------------+------------+--------------------+-----------------+
| tenant_name | database_name | table_name | table_id | data_table_id | table_type | storage_type       | main_table_name |
+-------------+---------------+------------+----------+---------------+------------+--------------------+-----------------+
| test1       | row_column_db | t1         |   596454 |          NULL | USER TABLE | 纯行存表           | NULL            |
| test1       | row_column_db | t2         |   596462 |          NULL | USER TABLE | 纯行存表           | NULL            |
| test1       | row_column_db | t3         |   596470 |          NULL | USER TABLE | 纯行存表           | NULL            |
| test1       | row_column_db | t4         |   596478 |          NULL | USER TABLE | 纯行存表           | NULL            |
| test1       | row_column_db | idx_t1_c2  |   596455 |        596454 | INDEX      | 纯行存索引         | t1              |
| test1       | row_column_db | idx_t2_c2  |   596463 |        596462 | INDEX      | 纯行存索引         | t2              |
| test1       | row_column_db | idx_t3_c2  |   596471 |        596470 | INDEX      | 纯行存索引         | t3              |
| test1       | row_column_db | idx_t4_c2  |   596479 |        596478 | INDEX      | 纯列存索引         | t4              |
| test7       | db100         | t8         |   500070 |          NULL | USER TABLE | 行列混合表         | NULL            |
| test7       | db100         | idx_t8_c2  |   500071 |        500070 | INDEX      | 行列混合索引       | t8              |
| test7       | db600         | t5         |   500045 |          NULL | USER TABLE | 纯行存表           | NULL            |
| test7       | db600         | t6         |   500053 |          NULL | USER TABLE | 纯列存表           | NULL            |
| test7       | db600         | t7         |   500061 |          NULL | USER TABLE | 纯列存表           | NULL            |
| test7       | db600         | idx_t5_c2  |   500046 |        500045 | INDEX      | 行列混合索引       | t5              |
| test7       | db600         | idx_t6_c2  |   500054 |        500053 | INDEX      | 纯行存索引         | t6              |
| test7       | db600         | idx_t7_c2  |   500062 |        500061 | INDEX      | 纯行存索引         | t7              |
+-------------+---------------+------------+----------+---------------+------------+--------------------+-----------------+
16 rows in set (3.45 sec)

sql2_查询纯列存的表/索引

select 
    t1.tenant_name,
    t2.database_name,
    case 
        when t2.table_type = 'user table' then t2.table_name
        when t2.table_type = 'index' then t2.index_name
    end as table_name,
    t2.table_id,
    t2.data_table_id,
    t2.table_type,
    case
        when t2.table_type = 'user table' then
            case
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0 then '纯列存表'
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) > 0 then '行列混合表'
                else '纯行存表'
            end
        when t2.table_type = 'index' then
            case
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0 then '纯列存索引'
                when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                     and sum(case when cg.column_group_type = 1 then 1 else 0 end) > 0 then '行列混合索引'
                else '纯行存索引'
            end
    end as storage_type,
    coalesce(t3.table_name, null) as main_table_name
from 
    __all_virtual_column_group cg
join 
    dba_ob_tenants t1 on cg.tenant_id = t1.tenant_id
join 
    cdb_ob_table_locations t2 on cg.tenant_id = t2.tenant_id and cg.table_id = t2.table_id
left join 
    cdb_ob_table_locations t3 on t2.data_table_id = t3.table_id
where 
    t1.tenant_name in ('test1','test7')
    and t2.database_name in ('row_column_db','db100','db600')
    and t2.table_type in ('user table', 'index')
group by 
    t1.tenant_name, t2.database_name, t2.table_name, t2.table_id, t2.data_table_id, t2.table_type, main_table_name
having
    (t2.table_type = 'user table' and sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0)
    or
    (t2.table_type = 'index' and sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0)
order by 
    t1.tenant_name, t2.database_name, t2.table_name;

sql2_查询结果
+-------------+---------------+------------+----------+---------------+------------+-----------------+-----------------+
| tenant_name | database_name | table_name | table_id | data_table_id | table_type | storage_type    | main_table_name |
+-------------+---------------+------------+----------+---------------+------------+-----------------+-----------------+
| test1       | row_column_db | idx_t4_c2  |   596479 |        596478 | INDEX      | 纯列存索引      | t4              |
| test7       | db600         | t6         |   500053 |          NULL | USER TABLE | 纯列存表        | NULL            |
| test7       | db600         | t7         |   500061 |          NULL | USER TABLE | 纯列存表        | NULL            |
+-------------+---------------+------------+----------+---------------+------------+-----------------+-----------------+
3 rows in set (3.48 sec)

sql3_查询行列冗余的表/索引

SELECT 
    tenant_name,
    database_name,
    table_name,
    table_id,
    data_table_id,
    table_type,
    storage_type,
    main_table_name
FROM (
    select 
        t1.tenant_name,
        t2.database_name,
        case 
            when t2.table_type = 'user table' then t2.table_name
            when t2.table_type = 'index' then t2.index_name
        end as table_name,
        t2.table_id,
        t2.data_table_id,
        t2.table_type,
        case
            when t2.table_type = 'user table' then
                case
                    when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                         and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0 then '纯列存表'
                    when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                         and sum(case when cg.column_group_type = 1 then 1 else 0 end) > 0 then '行列混合表'
                    else '纯行存表'
                end
            when t2.table_type = 'index' then
                case
                    when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                         and sum(case when cg.column_group_type = 1 then 1 else 0 end) = 0 then '纯列存索引'
                    when sum(case when cg.column_group_type = 3 then 1 else 0 end) > 0 
                         and sum(case when cg.column_group_type = 1 then 1 else 0 end) > 0 then '行列混合索引'
                    else '纯行存索引'
                end
        end as storage_type,
        coalesce(t3.table_name, null) as main_table_name
    from 
        __all_virtual_column_group cg
    join 
        dba_ob_tenants t1 on cg.tenant_id = t1.tenant_id
    join 
        cdb_ob_table_locations t2 on cg.tenant_id = t2.tenant_id and cg.table_id = t2.table_id
    left join 
        cdb_ob_table_locations t3 on t2.data_table_id = t3.table_id
    where 
        t1.tenant_name in ('test1','test7')
        and t2.database_name in ('row_column_db','db100','db600')
        and t2.table_type in ('user table', 'index')
    group by 
        t1.tenant_name, t2.database_name, t2.table_name, t2.table_id, t2.data_table_id, t2.table_type, main_table_name
) subquery
WHERE 
    storage_type IN ('行列混合表', '行列混合索引')
ORDER BY tenant_name, database_name, table_name;

sql3_查询结果
+-------------+---------------+------------+----------+---------------+------------+--------------------+-----------------+
| tenant_name | database_name | table_name | table_id | data_table_id | table_type | storage_type       | main_table_name |
+-------------+---------------+------------+----------+---------------+------------+--------------------+-----------------+
| test7       | db100         | idx_t8_c2  |   500071 |        500070 | INDEX      | 行列混合索引       | t8              |
| test7       | db100         | t8         |   500070 |          NULL | USER TABLE | 行列混合表         | NULL            |
| test7       | db600         | idx_t5_c2  |   500046 |        500045 | INDEX      | 行列混合索引       | t5              |
+-------------+---------------+------------+----------+---------------+------------+--------------------+-----------------+
3 rows in set (3.33 sec)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/951013.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

重生之我在异世界学编程之算法与数据结构:深入堆篇

大家好,这里是小编的博客频道 小编的博客:就爱学编程 很高兴在CSDN这个大家庭与大家相识,希望能在这里与大家共同进步,共同收获更好的自己!!! 本文目录 正文一、堆的基本概念二、堆的存储表示三…

【网络】深入了解HTTPS协议

HTTPS协议: HTTPS 也是⼀个应用层协议 HTTPS本质上就是在HTTP的基础上加了一个加密层,抛开加密之后,剩下的内容跟HTTP一样; HTTP 协议内容都是按照文本的方式明文传输的. 这就导致在传输过程中出现一些被篡改的情况. 例如 &…

RabbitMQ基本介绍及简单上手

(一)什么是MQ MQ(message queue)本质上是队列,满足先入先出,只不过队列中存放的内容是消息而已,那什么是消息呢? 消息可以是字符串,json也可以是一些复杂对象 我们应用场…

sys.dm_exec_connections:查询与 SQL Server 实例建立的连接有关的信息以及每个连接的详细信息(客户端ip)

文章目录 引言I 基于dm_exec_connections查询客户端ip权限物理联接时间范围dm_exec_connections表see also: 监视SQL Server 内存使用量资源信号灯 DMV sys.dm_exec_query_resource_semaphores( 确定查询执行内存的等待)引言 查询历史数据库客户端ip应用场景: 安全分析缺乏…

vscode如何离线安装插件

在没有网络的时候,如果要安装插件,就会麻烦一些,需要通过离线安装的方式进行。下面记录如何在vscode离线安装插件。 一、下载离线插件 在一台能联网的电脑中,下载好离线插件,拷贝到无法联网的电脑上。等待安装。 vscode插件商店地址:https://marketplace.visualstudio.co…

基于ADAS 与关键点特征金字塔网络融合的3D LiDAR目标检测原理与算法实现

一、概述 3D LiDAR目标检测是一种在三维空间中识别和定位感兴趣目标的技术。在自动驾驶系统和先进的空间分析中,目标检测方法的不断演进至关重要。3D LiDAR目标检测作为一种变革性的技术,在环境感知方面提供了前所未有的准确性和深度信息. 在这里&…

【玩转全栈】----Django连接MySQL

阅前先赞,养好习惯! 目录 1、ORM框架介绍 选择建议 2、安装mysqlclient 3、创建数据库 4、修改settings,连接数据库 5、对数据库进行操作 创建表 删除表 添加数据 删除数据 修改(更新)数据: 获取数据 1、OR…

基于Android的疫苗预约系统

博主介绍:java高级开发,从事互联网行业多年,熟悉各种主流语言,精通java、python、php、爬虫、web开发,已经做了多年的设计程序开发,开发过上千套设计程序,没有什么华丽的语言,只有实…

基于 Apache Commons Pool 实现的 gRPC 连接池管理类 GrpcChannelPool 性能分析与优化

基于 Apache Commons Pool 实现的 gRPC 连接池管理类 GrpcChannelPool 性能分析与优化 1. 输出关键信息的代码示例 日志记录方法 使用以下代码记录连接池的关键信息,帮助分析连接池的状态和性能瓶颈: import org.apache.commons.pool2.impl.GenericO…

矩阵碰一碰发视频的视频剪辑功能源码搭建,支持OEM

在短视频创作与传播领域,矩阵碰一碰发视频结合视频剪辑功能,为用户带来了高效且富有创意的内容产出方式。这一功能允许用户通过碰一碰 NFC 设备触发视频分享,并在分享前对视频进行个性化剪辑。以下将详细阐述该功能的源码搭建过程。 一、技术…

CClinkIEfield Basic转Modbus TCP网关模块连接三菱FX5U PLC

捷米特JM-CCLKIE-TCP是自主研发的一款CCLINK IE FB从站功能的通讯网关。该产品主要功能是将各种 MODBUS-TCP 设备接入到 CCLINK IE FB网络中。 捷米特JM-CCLKIE-TCP网关连接到CCLINK IE FB总线中做为从站使用,连接到 MODBUS-TCP 总线中做为主站或从站使用。 为了打破…

农产品智慧物流系统

本文结尾处获取源码。 本文结尾处获取源码。 本文结尾处获取源码。 一、相关技术 后端:Java、JavaWeb / Springboot。前端:Vue、HTML / CSS / Javascript 等。数据库:MySQL 二、相关软件(列出的软件其一均可运行) I…

设计模式-结构型-桥接模式

1. 什么是桥接模式? 桥接模式(Bridge Pattern) 是一种结构型设计模式,它旨在将抽象部分与实现部分分离,使它们可以独立变化。通过这种方式,系统可以在抽象和实现两方面进行扩展,而无需相互影响…

后台管理系统引导功能的实现

引导是软件中经常见到的一个功能,无论是在后台项目还是前台或者是移动端项目中。 那么对于引导页而言,它是如何实现的呢?通常情况下引导页是通过 聚焦 的方式,高亮一块视图,然后通过文字解释的形式来告知用户该功能的作…

现场展示deepseek VS openAI o1模型大对比

DeepSeek-V3 模型的发布在 AI 领域引起了广泛关注。作为一款拥有 6850 亿参数的混合专家(MoE)语言模型,DeepSeek-V3 在多个基准测试中表现出色,甚至超越了一些闭源模型。其在 Aider 代码能力排行榜上的正确率达到 48.4%&#xff0…

Golang的并发编程框架比较

# Golang的并发编程框架比较 中的并发编程 在现代软件开发中,处理高并发的能力愈发重要。Golang作为一门支持并发编程的编程语言,提供了丰富的并发编程框架和工具,使得开发者能够更轻松地处理并发任务。本文将介绍Golang中几种常用的并发编程…

SSL,TLS协议分析

写在前面 工作中总是会接触到https协议,也知道其使用了ssl,tls协议。但对其细节并不是十分的清楚。所以,就希望通过这篇文章让自己和读者朋友们都能对这方面知识有更清晰的理解。 1:tls/ssl协议的工作原理 1.1:设计的…

网络安全-XSS跨站脚本攻击(基础篇)

漏洞扫描的原理 1.跨站脚本攻击介绍 xss跨站脚本攻击: xSS 全称(Cross site Scripting )跨站脚本攻击,是最常见的Web应用程序安全漏洞之一,位于OWASP top 10 2013/2017年度分别为第三名和第七名,XSS是指攻…

SpringBoot之核心配置

学习目标: 1.熟悉Spring Boot全局配置文件的使用 2.掌握Spring Boot配置文件属性值注入 3.熟悉Spring Boot自定义配置 4.掌握Profile多环境配置 5.了解随机值设置以及参数间引用 1.全局配置文件 Spring Boot使用 application.properties 或者application.yaml 的文…

【Word_笔记】Word的修订模式内容改为颜色标记

需求如下:请把修改后的部分直接在原文标出来,不要采用修订模式 步骤1:打开需要转换的word后,同时按住alt和F11 进入(Microsoft Visual Basic for Appliations) 步骤2:插入 ---- 模块 步骤3&…