如何查询集群下多表的分区行数

在sr支持show partitions from xxx来查询单表分区的行数,目前还争对集群下所有库表的最近两天分区行数做统计,不知道有什么方法

SHOW PARTITIONS | StarRocks

当前并没有针对于集群下(多表)所有库表的最近两天分区的行数进行统计。这个可以写自动化的脚本来实现 一样的逻辑

  1. 枚举所有数据库和表
    SQL 语句 SHOW DATABASES; 和对每个数据库执行 SHOW TABLES;
  2. 获取每个表的分区信息
    SHOW PARTITIONS FROM [Table Name]; 来获取其分区信息。
  3. 筛选最近两天的分区
    根据分区的命名规则来识别最近两天的分区
  4. 计算行数统计信息
    对于每个符合条件的分区,累加其行数以获得总行数。

Python 脚本示例供参考 具体你要再改改

import pymysql
import datetime

# 连接配置,请根据实际情况调整
conn = pymysql.connect(host='your_starrocks_host', user='user', password='password', database='information_schema', port=9030)

def query_with_result(conn, sql):
    with conn.cursor() as cursor:
        cursor.execute(sql)
        return cursor.fetchall()

def main():
    databases = query_with_result(conn, "SHOW DATABASES;")
    for db in databases:
        db_name = db[0]
        tables = query_with_result(conn, f"USE {db_name}; SHOW TABLES;")
        for table in tables:
            table_name = table[0]
            partitions = query_with_result(conn, f"SHOW PARTITIONS FROM {table_name};")
            # 这里添加逻辑来过滤最近两天的分区并计算行数
            # 注意:你需要根据你的分区命名规则来调整过滤逻辑
            print(f"Database: {db_name}, Table: {table_name}, Partitions: {partitions}")
            # 具体的分区筛选和行数统计逻辑需要根据实际情况实现

if __name__ == "__main__":
    main()